InterviewSolution
| 1. |
Solve : batch file to xcopy? |
|
Answer» we have a series of towers xp, sp2/sp3 that we back up every night to a server. i use the following code... System 32 is the correct place for Sleep.exe Anywhere on the PATH is the "correct" place for executables.thank you, echo command line works. but i need to bug you guys again for two things. First: as the echo command runs, the file writes over itself so i would have to view the file daily. i started to surf again, and to fix that problem i used another line i found on this site to rename the ORIGINAL Report.txt file: for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "Report.txt" %%f-%%e-%%d.txt this adds the date, but the problem is if the backup is run manually, and the file already exists with the current date, the error message "Duplicate file name exists, or the file cannot be found" comes up and the report is not updated. so i tried to get creative, and thought i'd add a time stamp to the rename and tried: for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "Report.txt" %%f-%%e-%%d%time%.txt (there is a line to rename the file with the system time, but then no date is readily visible, that's why i am looking for a date, time format.) no formatting of the time, just stamp the time so that the two files (if necessary) can be made on the same day. but with this line, i get the message: ...>rename "Report.txt" 2008-07-16 9:22:19.05.txt The syntax of the command is incorrect." how do i fix it so it accepts the system date time? The second thing that i would like to do is have it report in the Report.txt file what if any files were copied. the screen does show the file names that were copied, and the number of files that were copied. Can that be an "echo" line after the xcopy... line? thank you for your help. jat timestamp has spaces so filename will need quotes for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "Report.txt" "%%f-%%e-%%d%time%.txt" tried recommended line, and error is "A duplicate file name exists, or the file cannot be found." thinking that maybe having the "" around just the time stamp, did not work either. thank you for your help though. jat The reason why you get that message is because you cannot have colon characters (':') in a filename. Since the %time% variable is of the form hh:mm:ss.cs (cs are hundredths of seconds), any attempt to use it as part of a filename will fail. You need to replace each colon in %time% with an allowed character such as a dash. Code: [Select]REM Replace a substring with another REM 1. replace each a in %string% with a b REM set string=%string:a=b% REM 2. Replace cat with dog REM set string=feed the cat every day REM set string=%string:cat=dog% REM Replace 1 char with nothing (i.e. remove each occurence) REM 3. remove every character a from %string% REM set string=%string:a=% REM 1. Replace : with - in %time% set ntime=%time::=-% REM 2. Replace . with - in %ntime% set ntime=%ntime:.=-% REM Personally I think it looks better with a separator between the date and the time for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "Report.txt" "%%f-%%e-%%d %ntime%.txt" Code: [Select]REM Personally I think it would look even better with only 1 separator between date and time REM if you don't need the hundredths of seconds you can remove the last 3 chars from %time% by slicing it set ntime=%time:~0,8% set ntime=%ntime::=% for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "Report.txt" "%%f%%e%%d-%ntime%.txt" |
|