DaveWentzel.com            All Things Data

ASP VBScript and WSH Home

FileSystemObject Helpful Hints

PendingFileRenameOperations

ADSI programming...removing extra ProxyAddresses

Back to FAQs Page

 

 

using recursion to create folder trees
Dim fso As Scripting.FileSystemObject

Set fso = New Scripting.FileSystemObject

fso.CreateFolder Folder "C:\backup\master\logs"

fso.CreateFolder Folder "C:\backup\master\full"

Set fso = Nothing
This will only work if backup\master exists first.  Keep this in mind.  So, you may need to check that the higher level folders exist first, using code similar to this.
 Dim fso As Scripting.FileSystemObject

Set fso = New Scripting.FileSystemObject

If fso.FolderExists(FolderName) = False Then

    fso.CreateFolder Folder "C:\backup"

end if

If fso.FolderExists(FolderName) = False Then

    fso.CreateFolder Folder "C:\backup\master"

end if

If fso.FolderExists(FolderName) = False Then

    fso.CreateFolder Folder "C:\backup\master\logs"

    fso.CreateFolder Folder "C:\backup\master\full"  

end if

Set fso = Nothing 


The problem is that this code becomes difficult and repetitive with the more checks that you need to do.  By adding some recursion checking you can simplify this enormously.  If you are using VBScript change the New to CreateObject and remove the datatyping. 
 Sub CreateFolder (FolderName as string)
Dim fso As Scripting.FileSystemObject

Dim iBreak As Integer
On Error Resume Next
'search from right to find path

iBreak = InStrRev(FolderName, "\")

If iBreak > 0 Then

Call CreateFolder(Left$(FolderName, iBreak - 1))

End If
Set fso = New Scripting.FileSystemObject

If fso.FolderExists(FolderName) = False Then

fso.CreateFolder FolderName

End If

Set fso = Nothing
End Sub 
 
 
Rename all the files in a folder that start with a certain string to the same name without the string
' This script will remove Load_ from the filename of all files that include this string in the filename

dir = "c:\temp\"
strCheck = "LOAD_" 'leave this in uppercase

set fsObject=Wscript.CreateObject ("Scripting.FileSystemObject")
Set oFolder = fsObject.GetFolder(dir)
set oFiles = oFolder.Files
For each item in oFiles

    strFileName=item.Name
    LoadFile = InStr(strFileName, strCheck) 'this tells me if LOAD is in the string
    If LoadFile <> 0 Then
 'Replace(expression, find, replacewith[, start[, count[, compare]]])
 strFileName = Replace(strFileName, strCheck, "")
 item.name = strFileName 'Set the file name to new name
 End If
Next
 
Determining DateLastModified for a File
Dim objFSO, objFolder, objFile, colFiles
 
Set objFSO = createobject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("folder name here")
Set colFiles = objFolder.Files
 
If colFiles.count > 0 Then
 For Each objFile in colFiles
  msgbox objFile.Name & " - " & objFile.DateLastModified
 Next
Else
 msgbox "There are no files in the directory."
End If
 
Set objFSO = Nothing
Set objFolder = Nothing
Set colFiles = Nothing

Add new comment