metalx1000

VBS Loop through Files and Sub Folders

Apr 18th, 2020
1,219
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Loops through all sub directories and writes to all files
  2. Set objFSO = CreateObject("Scripting.FileSystemObject")
  3. On Error Resume Next
  4. objStartFolder = "c:\MyDir"
  5.  
  6. Set objFolder = objFSO.GetFolder(objStartFolder)
  7. Set colFiles = objFolder.Files
  8.  
  9. For Each objFile in colFiles
  10.   On Error Resume Next
  11.   Set File = objFSO.CreateTextFile(objFile,True)
  12.   File.Write "..."
  13.   File.Close
  14.   On Error GoTo 0
  15. Next
  16.  
  17. ShowSubfolders objFSO.GetFolder(objStartFolder)
  18. Sub ShowSubFolders(Folder)
  19.   For Each Subfolder in Folder.SubFolders
  20.     On Error Resume Next
  21.     Set objFolder = objFSO.GetFolder(Subfolder.Path)
  22.     Set colFiles = objFolder.Files
  23.     For Each objFile in colFiles
  24.       On Error Resume Next
  25.       Set File = objFSO.CreateTextFile(objFile,True)
  26.       File.Write "..."
  27.       File.Close
  28.       On Error GoTo 0
  29.     Next
  30.     ShowSubFolders Subfolder
  31.     On Error GoTo 0
  32.     Next
  33. End Sub
RAW Paste Data