

InterviewSolution
Saved Bookmarks
1. |
Solve : batch file editing itself help.? |
Answer» <html><body><p>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.<br/><br/>I can easily accomplish this with xcopy and it's attributes. I then plan to have this batch file run via window's scheduled tasks.<br/><br/>My problem is that I want the batch file to only copy files and folders changed since the last time the batch file was run. I know I would use the /d:mm-dd-yyyy attribute to do this. The problem is I am having trouble wrapping my head around how to have the batch file edit itself to change the date it was last run. I was originally trying to have it overwrite itself with echo but the problem is that if I do that it will eventually run out...<br/><br/>How do I get a batch file to edit one little part of itself?<br/><br/>thanks, sharfUse schtasks to schedule the batch file<br/><br/>C:> schtasks /?<br/><br/>for the options and directionssharf, you don't have to have the batch file edit itself. Just get it, after running xcopy, to write the date to a named text file in the same folder, overwriting the old version (if it is present) and then next time it is run it picks the previous run date from that file, and uses it for the xcopy command, and then writes the new date to that file, ready for the next time. <br/><br/>Ignore marvinengland who has (once again!) misunderstood the question.<br/>Then what do I do to write/get the file?Or you could get a batch to <a href="https://interviewquestions.tuteehub.com/tag/replace-karana-ka-sharatakata-622250" style="font-weight:bold;" target="_blank" title="Click to know more about REPLACE">REPLACE</a> its own first line using FOR like this<br/><br/> Code: <a>[Select]</a>set DateAndTime=26/06/2010-21:14:00.31<br/>echo off<br/>set thisbatchname=%~nx0<br/>echo old date and time %DateAndTime%<br/>pause<br/>echo set DateAndTime=%date%-%time%>newfile.txt<br/>for /f "skip=1 delims=" %%L in (%thisbatchname%) do (<br/> echo %%L>>newfile.txt<br/> )<br/>move /y "%thisbatchname%" "tempname.bat" & ren "newfile.txt" "%thisbatchname%" & del "tempname.bat"<br/><br/> Quote from: sharf on June 26, 2010, 02:00:24 PM</p><blockquote>Then what do I do to write/get the file?<br/></blockquote> <br/>I do not know what your local date format is, but basically you can do it like this<br/><br/> Code: <a>[Select]</a>echo off<br/><br/>REM write<br/>set today=%date%<br/>echo %today%>old-date.txt<br/><br/>REM read<br/>set /p olddate=<old-date.txt<br/>echo the old date was %olddate%<br/><br/><br/><br/><br/>Yeah...I'm not that fantastic at batch files...never <a href="https://interviewquestions.tuteehub.com/tag/done-958312" style="font-weight:bold;" target="_blank" title="Click to know more about DONE">DONE</a> more than the basic stuff with them.<br/><br/>What I want is a batch file to do this:<br/>It will be in local disk C: named backup.bat<br/><br/>Then...<br/> Code: <a>[Select]</a>xcopy C:\ D:\ /D:06-25-2010/K/H/R/Y/E/V/S/Exclude:C:\Windows\<br/>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?<br/><br/>Thanks a lot, sharfthis should work (note i have not tested this )<br/><br/> Code: <a>[Select]</a>echo off<br/>if not exist lastrun.txt goto copy<br/>for /f "tokens=1-3" %%a in (lastrun.txt) do (<br/>set OMM=%%a<br/>set ODD=%%b<br/>set OYY=%%c<br/>)<br/>set DD=%date:~7,2%<br/>set MM=%date:~4,2%<br/>set YY=%date:~10,4%<br/>if %YY% <a href="https://interviewquestions.tuteehub.com/tag/lss-537887" style="font-weight:bold;" target="_blank" title="Click to know more about LSS">LSS</a> %OYY% goto exit<br/>if %MM% LSS %OMM% if %YY% GTR %OYY% goto copy<br/>if %MM% GTR %OMM% goto copy<br/>if %MM% EQU %OMM% if %DD% GTR %ODD% goto copy<br/>goto exit<br/>:copy<br/>echo.%MM% %DD% %YY% > lastrun.txt<br/>xcopy C:\ D:\ /D:%MM%-%DD%-%YY% /K /H /R /Y /E /V /S /Exclude:C:\Windows\<br/>:exit<br/>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<br/><br/><br/> Quote<blockquote>note i have not tested this</blockquote> <br/>Indeed.<br/><br/>Does it every time though.Please show what you get when you type this at the command prompt<br/><br/> Code: <a>[Select]</a>echo %date%fixed it<br/> Code: <a>[Select]</a>echo off<br/>if not exist lastrun.txt goto copy<br/>for /f "tokens=1-3" %%a in (lastrun.txt) do (<br/>set OMM=%%a<br/>set ODD=%%b<br/>set OYY=%%c<br/>)<br/>set DD=%date:~7,2%<br/>set MM=%date:~4,2%<br/>set YY=%date:~10,4%<br/>if %YY% LSS %OYY% goto exit<br/>if %MM% LSS %OMM% if %YY% GTR %OYY% goto copy<br/>if %MM% GTR %OMM% goto copy<br/>if %MM% EQU %OMM% if %DD% GTR %ODD% goto copy<br/>goto exit<br/>:copy<br/>set DD=%date:~7,2%<br/>set MM=%date:~4,2%<br/>set YY=%date:~10,4%<br/>echo.%MM% %DD% %YY% > lastrun.txt<br/>xcopy C:\ D:\ /D:%MM%-%DD%-%YY%/K/H/R/Y/E/V/S/Exclude:C:\windows\<br/>pause<br/>:exit<br/>exit<br/>Even "fixed" it still does the same thing.<br/><br/>When I echo date I get "Sun 06/27/2010"I am not <a href="https://interviewquestions.tuteehub.com/tag/sure-656539" style="font-weight:bold;" target="_blank" title="Click to know more about SURE">SURE</a> why you need all this stuff. I don't think it does anything <a href="https://interviewquestions.tuteehub.com/tag/useful-1441152" style="font-weight:bold;" target="_blank" title="Click to know more about USEFUL">USEFUL</a>. In any case the %OMM% %ODD% %OYY% variables aren't actually used for anything. <br/><br/> Code: <a>[Select]</a>if %YY% LSS %OYY% goto exit<br/>if %MM% LSS %OMM% if %YY% GTR %OYY% goto copy<br/>if %MM% GTR %OMM% goto copy<br/>if %MM% EQU %OMM% if %DD% GTR %ODD% goto copy<br/>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.<br/><br/>Why don't you just create a starter lastrun.txt in Notepad, with a suitable date earlier than now, e.g. <br/><br/>06-25-2010<br/><br/>and save it in the folder where the batch script is<br/><br/>then just use this<br/><br/> Code: <a>[Select]</a>echo off<br/>for /f "tokens=1-3 delims=" %%a in (lastrun.txt) do set lastdate=%%a<br/>xcopy C:\ D:\ /D:%lastdate%/K/H/R/Y/E/V/S/Exclude:C:\windows\<br/>set DD=%date:~7,2%<br/>set MM=%date:~4,2%<br/>set YY=%date:~10,4%<br/>echo %MM%-%DD%-%YY%>lastrun.txt<br/></body></html> | |