I have to create a .BAT
file that does this:
- If
C:\myprogram\sync\data.handler
exists, exit; - If
C:\myprogram\html\data.sql
does not exist, exit; - In
C:\myprogram\sync\
delete all files and folders except (test
, test3
and test2
) - Copy
C:\myprogram\html\data.sql
to C:\myprogram\sync\
- Call other batch file with option
sync.bat myprogram.ini
.
If it was in the Bash environment it was easy for me, but I do not know how to test if a file or folder exists and if it is a file or folder.
You can use IF EXIST to check for a file:
IF EXIST "filename" ( REM Do one thing ) ELSE ( REM Do another thing )
If you want to look for a drive or directory, see http://support.microsoft.com/kb/65994 for examples
Type IF /? to get help about if, it clearly explains how to use IF EXIST.
To delete a complete tree except some folders, see the answer of this question: Windows batch script to delete everything in a folder except one
Finally copying just means calling COPY and calling another bat file can be done like this:
MYOTHERBATFILE.BAT sync.bat myprogram.ini
Here is a good example on how to do a command if a file does or does not exist:
if exist C:\myprogram\sync\data.handler echo Now Exiting && Exit if not exist C:\myprogram\html\data.sql Exit
We will take those three files and put it in a temporary place. After deleting the folder, it will restore those three files.
xcopy "test" "C:\temp" xcopy "test2" "C:\temp" del C:\myprogram\sync\ xcopy "C:\temp" "test" xcopy "C:\temp" "test2" del "c:\temp"
Use the XCOPY command:
xcopy "C:\myprogram\html\data.sql" /c /d /h /e /i /y "C:\myprogram\sync\"
I will explain what the /c /d /h /e /i /y
means:
/C Continues copying even if errors occur. /D:m-d-y Copies files changed on or after the specified date. If no date is given, copies only those files whose source time is newer than the destination time. /H Copies hidden and system files also. /E Copies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T. /T Creates directory structure, but does not copy files. Does not include empty directories or subdirectories. /T /E includes /I If destination does not exist and copying more than one file, assumes that destination must be a directory. /Y Suppresses prompting to confirm you want to overwrite an existing destination file. `To see all the commands type`xcopy /? in cmd
Call other batch file with option sync.bat myprogram.ini.
I am not sure what you mean by this, but if you just want to open both of these files you just put the path of the file like
Path/sync.bat Path/myprogram.ini
If it was in the Bash environment it was easy for me, but I do not know how to test if a file or folder exists and if it is a file or folder.
You are using a batch file. You mentioned earlier you have to create a .bat file to use this:
I have to create a .BAT file that does this: