Imagine there is a file A:\Example\file.txt

If any change is detected in that file, it is automatically replaced with the file B:\example\file.txt, and without a file replacement dialog showing up, nor a UAC prompt appearing each time, everything just happens in the background. If no change is detected, nothing is done, the script continues to check for changes in the background, while hopefully consuming very little system resources.

I could do:

Copy-Item "A:\Example\file.txt" "B:\Example\file.txt" -Force

But this isn't recurring or running in the background all the time, and I am not sure if it can skip UAC prompts or file replacement dialogs automatically or not.

  • 1
    Please note that superuser.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?. – DavidPostill Mar 5 '17 at 20:38
  • 1
    A bit better :) – DavidPostill Mar 5 '17 at 20:48
  • @MagicallyDelicous Replacing every minute doesn't detect file changes, does it? It just replaces the file every minute regardless whether or not the file was actually changed, which isn't what I would prefer. – Buffer Over Read Mar 5 '17 at 21:10
  • @MagicallyDelicous I want it to be replaced if the file in general changes. Something like a hash-based solution might work, but I'm unsure if checking hashes each time is intensive on the system or not. Ideally I'd want the script running in the background with minimal resources. – Buffer Over Read Mar 5 '17 at 21:18
  • 1
    It's an interesting one as typically the way to do this would be to use a file system watcher. So you get called back that the file has changed and you take action, e.g. copy the new file over, of course, this triggers the same callback as you overwrite the file you're monitoring so you end up in a infinite loop. pastebin.com/NmHuigtL – HelpingHand Mar 5 '17 at 22:34

You're basically going to need a scheduled task that runs at short intervals - say, every 5 minutes - in order to detect a change. One idea that comes to mind is that you could hash the file, store the value, and then have your scheduled task hash the file again and compare the hash to the stored value. You seem to be familiar with Powershell. Verion 4.0 and above has a Get-FileHash cmdlet you could use. From there, have your script copy the file if the hashes are different.

In response to the comment, here's some sample code. StoredHash.txt is a text file which contains the hash of the file in question.

$A = Get-Content "StoredHash.txt"
$B = Get-FileHash  -Algorithm MD5 -Path "A:\Example\file.txt"
If (-Not $A = $B)   
  Copy-Item "A:\Example\file.txt" -Destination "B:\Example\file.txt"
  $B | Out-File "StoredHash.txt"   

The last line stores the new hash so that you can look for a subsequent change. I did not test this code, so it may contain errors. But it should be enough to get you started.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.