| 1. |
Solve : batch file editing itself help.? |
|
Answer» Ok so I want to have a batch file on my computer that when run it copies all the files from one location to another as a form of backup. Then what do I do to write/get the file? I do not know what your local date format is, but basically you can do it like this Code: [Select]echo off REM write set today=%date% echo %today%>old-date.txt REM read set /p olddate=<old-date.txt echo the old date was %olddate% Yeah...I'm not that fantastic at batch files...never DONE more than the basic stuff with them. What I want is a batch file to do this: It will be in local disk C: named backup.bat Then... Code: [Select]xcopy C:\ D:\ /D:06-25-2010/K/H/R/Y/E/V/S/Exclude:C:\Windows\ And that will be run every friday by task schedular. The problem is, I don't want it to waste time copying files that have already been copied and have not been changed since the last backup. This is why I want the batch file (or something else) to keep track of the date (which is in MM-DD-YYYY format) but I don't know how to go about doing this...I kind of understand what you said, can you explain it a little clearer in terms of my batch file? Thanks a lot, sharfthis should work (note i have not tested this ) Code: [Select]echo off if not exist lastrun.txt goto copy for /f "tokens=1-3" %%a in (lastrun.txt) do ( set OMM=%%a set ODD=%%b set OYY=%%c ) set DD=%date:~7,2% set MM=%date:~4,2% set YY=%date:~10,4% if %YY% LSS %OYY% goto exit if %MM% LSS %OMM% if %YY% GTR %OYY% goto copy if %MM% GTR %OMM% goto copy if %MM% EQU %OMM% if %DD% GTR %ODD% goto copy goto exit :copy echo.%MM% %DD% %YY% > lastrun.txt xcopy C:\ D:\ /D:%MM%-%DD%-%YY% /K /H /R /Y /E /V /S /Exclude:C:\Windows\ :exit exitlol it just tries to start and shuts down (after creating lastrun.text) which is blank.On its first run lastrun.txt won't exist so it will go to the :copy label, but it won't know the values of %MM% %DD% %YY% which are defined after the goto, so the /D: parameter wiull be blank so the xcopy command will crash Quote note i have not tested this Indeed. Does it every time though.Please show what you get when you type this at the command prompt Code: [Select]echo %date%fixed it Code: [Select]echo off if not exist lastrun.txt goto copy for /f "tokens=1-3" %%a in (lastrun.txt) do ( set OMM=%%a set ODD=%%b set OYY=%%c ) set DD=%date:~7,2% set MM=%date:~4,2% set YY=%date:~10,4% if %YY% LSS %OYY% goto exit if %MM% LSS %OMM% if %YY% GTR %OYY% goto copy if %MM% GTR %OMM% goto copy if %MM% EQU %OMM% if %DD% GTR %ODD% goto copy goto exit :copy set DD=%date:~7,2% set MM=%date:~4,2% set YY=%date:~10,4% echo.%MM% %DD% %YY% > lastrun.txt xcopy C:\ D:\ /D:%MM%-%DD%-%YY%/K/H/R/Y/E/V/S/Exclude:C:\windows\ pause :exit exit Even "fixed" it still does the same thing. When I echo date I get "Sun 06/27/2010"I am not SURE why you need all this stuff. I don't think it does anything USEFUL. In any case the %OMM% %ODD% %OYY% variables aren't actually used for anything. Code: [Select]if %YY% LSS %OYY% goto exit if %MM% LSS %OMM% if %YY% GTR %OYY% goto copy if %MM% GTR %OMM% goto copy if %MM% EQU %OMM% if %DD% GTR %ODD% goto copy As the script stands, the first time it is run, lastrun.txt does not exist, so the batch writes todays' month, day and year values into lastrun.txt. Then it uses those same values (!) for xcopy's "copy files newer than" date. When it is run later, the lastrun.txt exists, and the earlier date is read, but is not used for anything. Why don't you just create a starter lastrun.txt in Notepad, with a suitable date earlier than now, e.g. 06-25-2010 and save it in the folder where the batch script is then just use this Code: [Select]echo off for /f "tokens=1-3 delims=" %%a in (lastrun.txt) do set lastdate=%%a xcopy C:\ D:\ /D:%lastdate%/K/H/R/Y/E/V/S/Exclude:C:\windows\ set DD=%date:~7,2% set MM=%date:~4,2% set YY=%date:~10,4% echo %MM%-%DD%-%YY%>lastrun.txt |
|