1

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 3

4

The .NET framework has System.IO.FileSystemWatcher, which can be accessed from PowerShell (example).

0
1

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 Replaces and Splits 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 /?.

7
1

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) }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .