Share via


Touching every file in every subdirectory

A friend recently asked me how to touch every file in a folder and subfolders.  Let's say he has an executible called "dosomething.exe" and wants to run that program against every file in the parent folder and subfolders.  Here's a quick script to do such a thing.  There's not much to the VBScript but I am posting because I know what it is like to be out in the field and need something quickly.  While this can be done multiple ways, I wrote this in VBScript because I did not know the client OS version and only had notepad handy at the time.  :)

 

On Error Resume Next ' Ignore errors such as access denied
Dim ObjShell, objFSO, objFolder, objStartFolder
Dim colFiles

Set ObjShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files

objStartFolder = "C:\jingalls" ' This is the parent directory

For Each objFile in colFiles
Next

ShowSubfolders objFSO.GetFolder(objStartFolder)

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
            wscript.echo Subfolder.Path & "\" & objFile.Name
 set objWshScriptExec = objShell.Exec("%COMSPEC% /c c:\utils\dosomething.exe " & Subfolder.Path & "\" & objFile.Name)
 Set objStdOut = objWshScriptExec.StdOut
 strOutput = objStdOut.ReadAll
  WScript.Echo strOutput
        Next
        ShowSubFolders Subfolder
    Next
End Sub

Set objShell = Nothing
Set objFSO = Nothing
set objFolder = Nothing
set objStartFolder = Nothing
set colFiles = Nothing