Created
September 18, 2012 22:30
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.