How to detect changed files and move them to a folder named storage on my desktop is there any way I can do it with powershell/bat/vbscript without any third party tools?
3 Answers
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 /?
.
-
-
docs.microsoft.com/en-us/office/vba/language/reference/… Commented Aug 21, 2021 at 20:04
-
-
It is impersonating you. In the past you could be anonymous etc. This is the only useful one left. See docs.microsoft.com/en-us/dotnet/api/… This means WMI will only do what you are allowed to do. Commented Aug 22, 2021 at 5:03
-
1@AbrahamZinala As you didn't ask the question you wasted my time. Commented Aug 22, 2021 at 9:04
possible solution, run powershell script every like 10 minutes, look for created/modified files and move
Get-ChildItem -Recurse | ? { $_.CreationTime -gt $(get-date).AddMinutes(-10)} | % { Move-Item -Path $_.FullName -Destination $(Join-Path -Path $([Environment]::GetFolderPath("Desktop")) -ChildPath source) }