Skip to content

Instantly share code, notes, and snippets.

@shabdar
Created September 18, 2012 22:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shabdar/3746384 to your computer and use it in GitHub Desktop.
Save shabdar/3746384 to your computer and use it in GitHub Desktop.
Finds and lists the missing files in a destination folder compared to the ones in a source folder with sub-solders. (Windows PowerShell Script)
'set paths
sourcePath = "\\source\"
destPath = "\\dest\"
'initiate the file system object
set fso = CreateObject("Scripting.FileSystemObject")
if fso.FolderExists(sourcePath) then
'first get all the sub-folder
set folder = fso.GetFolder(sourcePath)
set folders = folder.SubFolders
dim count, list, none
list = ""
count = 0
'go through each subfolder under source folder and find the files that are
'missing from the destination folder
for each sf in folders
newPath = sourcePath + sf.name
set newSf = fso.GetFolder(newPath)
'generate a list of missing files for each folder
list = list & "=> \" & sf.name & ":" & vbCrLf
none = true 'by default assume no files missing
for each file in newSf.Files
if not(fso.FileExists(destPath & file.Name)) then
none = false 'well, there's at least one mising file
list = list & file.name & vbCrLf 'add the missing filename to list
count = count + 1 'add to the total number of files missing
end if
next
'show if there is no file missing for this folder
if none then list = list & "--NO MISSING FILES HERE!--" & vbCrLf
'add an extra empty line to make the output cleaner
list = list & vbCrLf
next
'save the list to a file
output = count & " files from the source folder are missing in the dest folder:"
output = String(len(output), "-") & vbCrLf & output & vbCrLf &_
String(len(output), "-") & String (2, vbCrLf) & list
set outFile = fso.CreateTextFile(".\" & "missing_files.txt", true)
outFile.WriteLine(output)
outFile.Close
end if
'process complete; update the user
Wscript.echo "Done! " & count &_
" files are missing. See the 'missing_files.txt' for a complete list"
'... or open the text file straighaway
'Set WshShell = WScript.CreateObject("WScript.Shell")
'WshShell.Run "%windir%\notepad " & "missing_files.txt"
Wscript.Quit
@hetzel21
Copy link

hetzel21 commented Sep 8, 2016

First of all, thank you for the script. I find it very useful for a task I need to take care of. This task consist of auditing folder for missing files. For instance, I have 20 folders that should have 3 forms with the names starting with 2875N_, 4433_, and 4394_. I would like to adapt your script to work so that if a form (2875_) is inexistent then it throws into a .csv. I have already tried to modify your script but my beginner knowledge limits me. For the sake of the argument, we could use this location: "C:\MasterFolder" (the 20 folders are within that folder. Any help is very appreciated. Thank you in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment