Detect changed files
The .NET framework has System.IO.FileSystemWatcher, which can be accessed from PowerShell (example).
This will print out newly created files in c:\win16
. Note the double doubled backslashes in the folder path. Within 10
says to check every 10 seconds.
This copies new files in C:\Win16
to C:\Users\User\Desktop\Backup\
. Note trailing backslash. You need to edit the paths.
All the Replace
s and Split
s are getting it a a plain file path turning two backslashes to one, removing quotes, and split
discards the first part of the returned string.
Call it monitorfolder.vbs
.
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name=""c:\\\\Win16""'")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
FSO.MoveFile Replace(Replace(Split(objLatestEvent.TargetInstance.PartComponent, "=")(1), "\\", "\"), """", ""), "C:\Users\User\Desktop\Backup\"
Loop
To print it out to a console window
Shift+Right Click on the file - Copy As Path. Open Command Prompt as Administrator and
Right Click - Paste, then press Home key and prepend cscript //nologo
to the path to the vbs. Then press Ctrl+C to stop monitoring.
The output will look like this
C:\Windows\system32>cscript //nologo "C:\Users\User\Desktop\Bat+Vbs\monitorfolder.vbs"
"c:\Win16\file.txt"
"c:\Win16\Folder Property List.txt"
"c:\Win16\IHS.pdf"
"c:\Win16\nfs3.exe - Shortcut.lnk"
Note
When a file is modified or created Windows sets its Archive bit. XCopy
can copy only files with the archive bit set and then clear the bit. This is how early MS-Dos backup programs worked. It only copies new of modified files since the last backup. See attrib /?
and xcopy /?
.