# Solved: VBScript Delete Folder within a Directory



## computerman29642 (Dec 4, 2007)

I am trying to come up with a VBScript that will delete a folder within a directory without specifying a name. Does anyone know how this is done?


----------



## IT-Support (Nov 30, 2009)

I know the answer to this one. Just finished writing a script that happens to have this code in.

In your declarations you will need

Imports System.IO

In your code

Dim folderToDelete as String = "C:\Program Files\Some Folder\Folder To Delete"

If Directory.Exists(folderToDelete) Then
Directory.Delete(folderToDelete)
End If


----------



## computerman29642 (Dec 4, 2007)

IT-Support thank you for the response.

I did not want to delete the directory, and I need a code that does not require the name of teh folder within the directory to be specified.


----------



## IT-Support (Nov 30, 2009)

Sorry dude. Can you give me some more info on what your app does? I assume the user can browse to a location then? If so you could put the name of the directory of the folder they click on in a hidden textbox and reference it that way. Or do you want to loop through all folders in a directory getting the names?


----------



## computerman29642 (Dec 4, 2007)

Here is the basics of what I am trying to do. I am creating a VBScript that will create a backup (copy) of a folder in one location to another.

So, each time the VBScript runs, I would like for it to delete the old backup folder (there will only be one folder in the backup folder), and then copy the folder from the other location to the backup folder.

The folder that has been copied will be renamed something like _Folder Name_Date_ in the backup folder.

IT-Support, does this explanation help?


----------



## IT-Support (Nov 30, 2009)

Yes that does make sense, what version of VB are using? Just so i can give you the correct code. Are there going to be any subfolders inside the main one being copied?


----------



## computerman29642 (Dec 4, 2007)

Yes, there will be subfolders.

What do you mean by version of VB? I am just creating a TXT file, and renaming teh file with VBS extension. I do not actually have VB software.


----------



## IT-Support (Nov 30, 2009)

Oh ok. I thought you were using Visual Studio.

Dim SourcePath
SourcePath = "C:\stuffToBackUp"
Dim DestinationPath
DestinationPath = "D:\BackUp"

Const OverWriteFiles = True
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(DestinationPath) Then
Else
Set objFolder = objFSO.CreateFolder(DestinationPath)
End If

objFSO.CopyFolder SourcePath , DestinationPath , OverWriteFiles


----------



## computerman29642 (Dec 4, 2007)

I tweaked teh code just a little to add the current date to the destination folder, but it is not working correctly. Can you help me figure out why?

Dim SourcePath
SourcePath = "C:\Tester"
Dim DestinationPath
DestinationPath = "C:\Testing\"
Dim strDate
strDate = Right("0" & DatePart("m",Date), 2) & "-" & Right("0" & DatePart("d",Date), 2) & "-" & DatePart("yyyy",Date)

Const OverWriteFiles = True
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(DestinationPath) Then
Else
Set objFolder = objFSO.CreateFolder(DestinationPath)
End If

objFSO.CopyFolder SourcePath , DestinationPath & "_" & strDate, OverWriteFiles

*BTW, The folder that is copied to the backup folder should keep the same name as the source folder just with the current date added to the end.*


----------



## IT-Support (Nov 30, 2009)

Dim SourcePath
SourcePath = "C:\Tester"
Dim DestinationPath

Dim strDate
strDate = Right("0" & DatePart("m",Date()), 2) & "-" & Right("0" & DatePart("d",Date()), 2) & "-" & DatePart("yyyy",Date())

DestinationPath = "C:\Testing" & "_" & strDate

Const OverWriteFiles = True
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(DestinationPath) Then
Else
Set objFolder = objFSO.CreateFolder(DestinationPath)
End If

objFSO.CopyFolder SourcePath, DestinationPath, OverWriteFiles

That should do it.


----------



## computerman29642 (Dec 4, 2007)

The destination folder is not being placed in the folder I ceated in the destination path due to the date being added. The FolderExist portion of the code is making a new folder to be created.


----------



## IT-Support (Nov 30, 2009)

Dim SourcePath
SourcePath = "C:\Tester"
Dim DestinationPath

Dim strDate
strDate = Right("0" & DatePart("m",Date()), 2) & "-" & Right("0" & DatePart("d",Date()), 2) & "-" & DatePart("yyyy",Date())

DestinationPath = "C:\Testing\backup" & "_" & strDate

Const OverWriteFiles = True
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(DestinationPath) Then
Else
Set objFolder = objFSO.CreateFolder(DestinationPath)
End If

objFSO.CopyFolder SourcePath, DestinationPath, OverWriteFiles

That better?


----------



## computerman29642 (Dec 4, 2007)

No. It is still doing the samething. Also, how does your code handle getting rid of the older backup folder?


----------



## IT-Support (Nov 30, 2009)

That code should create a folder inside C:\testing called backup_03-08-2010. As long as there is a folder called testing in C:\ if you need to create the testing folder too then you'll need to add:

If objFSO.FolderExists("C:\Testing") Then
Else
Set objFolder = objFSO.CreateFolder("C:\Testing")
End If

You could add another line to automatically delete yesterdays backup

Dim dteNow
dteNow = DateAdd("d", Date(), -1)

Dim deleteFolder
deleteFolder = "C:\Testing\backup_" & Right("0" & DatePart("m",dteNow), 2) & "-" & Right("0" & DatePart("d",dteNow), 2) & "-" & DatePart("yyyy",dteNow)

If objFSO.FolderExists(deleteFolder) Then
Else
Set objFolder = objFSO.DeleteFolder(deleteFolder)
End If


----------



## computerman29642 (Dec 4, 2007)

What would I do if I just wanted the source folder to be copied in the destination folder without the subfolder?


----------



## IT-Support (Nov 30, 2009)

I just realised that last bit is slightly wrong, here's everything if you wanted the backup folder to go to C:\testing_03-08-2010

Dim SourcePath
SourcePath = "C:\Tester"
Dim DestinationPath

Dim strDate
strDate = Right("0" & DatePart("m",Date()), 2) & "-" & Right("0" & DatePart("d",Date()), 2) & "-" & DatePart("yyyy",Date())

DestinationPath = "C:\Testing" & "_" & strDate

Const OverWriteFiles = True
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(DestinationPath) Then
Else
Set objFolder = objFSO.CreateFolder(DestinationPath)
End If

objFSO.CopyFolder SourcePath, DestinationPath, OverWriteFiles

Dim dteNow
dteNow = DateAdd("d", Date(), -1)

Dim deleteFolder
deleteFolder = "C:\Testing_" & Right("0" & DatePart("m",dteNow), 2) & "-" & Right("0" & DatePart("d",dteNow), 2) & "-" & DatePart("yyyy",dteNow)

If objFSO.FolderExists(deleteFolder) Then
Set objFolder = objFSO.DeleteFolder(deleteFolder)
End If


----------



## computerman29642 (Dec 4, 2007)

IT-Support I believe I have the code working. I am going to test the delete code now. 

Thank you very much.


----------



## IT-Support (Nov 30, 2009)

No worries mate, let me know if you still get stuck.


----------



## computerman29642 (Dec 4, 2007)

I tried the deletion code, and the older file does delete but I get an error message.

Line: 34
Char: 1
Error: Object required: 'objFSO.deleteFolder(...)'
Code: 800A01A8
Source: Microsoft VBScript runtime error

Just so you know, I used the code that creates the subfolder (backup) within the destination folder.


----------



## IT-Support (Nov 30, 2009)

Change

If objFSO.FolderExists(deleteFolder) Then
Set objFolder = objFSO.DeleteFolder(deleteFolder)
End If

to

If objFSO.FolderExists(deleteFolder) Then
objFSO.DeleteFolder(deleteFolder)
End If


----------



## computerman29642 (Dec 4, 2007)

That worked. Thank you very much IT-Support.


----------



## IT-Support (Nov 30, 2009)

Excellent


----------

