Need for Script (Batch file).


#1

Need for Script (Batch file).

I need a batch file script that does this.

I have two locations (directories) in the computer: A and B.

Location A is where I will put the batch file. Location B is %appdata%\MyFolder (is not the real name).

Each of them has files and folders in it.

I want to make a batch file which, when started, will do this:

  1. Copy specific files and folders(including their contents) from A (the batch file’s location) to B, without overwriting any files and folders in B that have the same names as those in A. In the batch file I should specify the files and folders to be copied from A to B.

  2. After the batch file has finished copying the specified files and folders(including their contents) from A to B, it should wait for a specified number of seconds, for example 3.

  3. After waiting for the number of seconds, the batch file should now do the reverse of step 1: that is, it will copy files and folders(including their contents) from B to A, without overwriting any files and folders in A that have the same names as those in B. In the batch file I should specify the files and folders to be copied from B to A.

  4. After the batch file has copied the specified files and folders(including their contents) from B to A, it should wait for a specified number of seconds, for example 3.

  5. Start again at Step 1, that is, copy from A to B.

  6. It should do this endlessly (without using up too much memory and freezing or crashing the computer) as long as a file called MyProgram.exe (which is in %appdata%\MyFolder) is running. If MyProgram.exe is not running or has been closed, the batch file should finish any copying that it is doing, wait for a number of seconds which I specify ( e.g. 3), and if MyProgram.exe is still not running, the batch file should close itself.

  7. While doing all the above, the batch file should be invisible: no window should show; it should not ask the user any questions; it should not overwrite any existing files or folders in either location while copying; no error messages should appear (for example, if I mentioned a file or folder that does not exist).

  8. If the batch file is already running, another instance of it should NOT be allowed to open (e.g. if someone presses the file again). But no message should be shown, because the batch file works silently and invisibly.

What is the code for such a batch file? (I use Windows 7 32-bit, if that is important, but I want the batch file to work on any Windows version.)

Since I am going to specify certain variables, such as the files and folders to be copied between A and B, and how long the batch file waits before copying each time, my helper can indicate where I should insert those variables in his or her code.

I give thanks for any useful help.


#2

Copying files is simple enough: the XCOPY command can copy files without overwriting existing files. It can also copy files based on criteria, like “the file does not exist in the destination” or “the file is newer than the destination.”

“Waiting” in a batch file is simple. Windows 10 includes the TIMEOUT command, so you can use that to delay processing:

image

Repeating steps in a batch file is also simple, the GOTO command will transfer control to a label:

:top
echo "hello"
GOTO top

Hiding a batch file can be done by running it from task scheduler.

However, preventing someone from starting a second copy is more complicated… Usually, “single instance” Windows programs enforce that by looking for a running task with the same name, then transferring control to the existing task and shutting down the new one.

Batch files also don’t have a provision for a clean shutdown, so if your program is in the middle of copying files when the system shuts off, your files will be corrupted.

I’d say it makes more sense to write this process as a Windows program and design it so that it sits in the system tray (the spot near the clock, in the bottom right corner). The program can use Timer objects to delay and perform your file copies, and you can use FileSystemWatcher objects to wait for the files to be updated over in the %appdata% directory.

Finally, it’s a bad idea to have a completely invisible app - it should either have a system tray icon (already discussed) or be implemented as a System Service. You NEED a way to safely shut down the program in the event it goes haywire or has another problem.