Windows CMD - wait for file to finish copying then run a command

0

1

I am trying to run a command after a file change is detected.. my problem is the program I use instantly acts when a file is created but not fully finished copying.

Right now I have used timeout /t 180 /nobreak before my script to temporarily fix this and then have it run my desired commands but this wont work for every file I do, some will take longer than 180sec.

What I am trying to accomplish is transferring video files over local network to a shared folder which from there the PC with the shared folder detects a file change/creation and then waits for the file to completely transfer then run .bat/.cmd

Is there any way to do this. Any help is greatly appreciated. Thanks!

John Him

Posted 2017-01-20T04:50:06.583

Reputation: 1

Answers

0

You can use for file transferring syncthing in master mode(one way sync). While it transferring file it create temporary file and only on success rename it to the actual filename. If your monitoring program will watch for particular file's extensions, it will pick it when transferring finished, so you can run your next task immediately. Similar things can be done with torrent compatible programs (restricted to your local network) where actual file will appear only of completed transfers.

P.S. Syncthing by default syncing on preset time windows, but if you need immediate transferring then you can use its companion program that calls syncthing-inotify, this way anything that were changed/created/deleted on source location will be replicated on destination PC immediately.

Alex

Posted 2017-01-20T04:50:06.583

Reputation: 5 606

I'll give syncthing a try, thank you for the suggestion Alex. I glanced at the website and that program reminds me of bittorent sync. I'm on mobile now but I'll try this. – John Him – 2017-01-20T07:47:28.917

@JohnHim Yes it similar to bitorrent-sync, but in contrast it open sourced, allows to runs own discovery and rely servers or directly p2p connections, well supported, and it runs practically on any devices and platforms – Alex – 2017-01-20T08:12:41.260

0

You should look at it from another perspective.

The script that copies the file will know when the transfer is finished, and can therefor time when the next action should start.

So you want to perform both actions from the same script.

This gives you 2 options:

Remote execution

the program PSExec (can be downloaded from microsoft) can execute a program on a targetted machine if it was started with sufficient rights. It can be used to start the program on the remote machine after the copy is finished.

Reverse copy

You are currently copying from the source to the destination. If you share the folder the file is in from the source, you can initiate the copy at the destination side too.

LPChip

Posted 2017-01-20T04:50:06.583

Reputation: 42 190

I have used PSExec in the past and it is very useful. I was trying to avoid using a program from the sending side to the network folder as it will be multiple PCs doing a transfer. I will try PSExec on one PC and test it. Thanks for the suggestion. – John Him – 2017-01-20T21:17:59.273