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.

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.

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...

How do I get a batch file to edit one little part of itself?

thanks, sharfUse schtasks  to schedule the batch file

C:>  schtasks  /?

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.

Ignore marvinengland who has (once again!) misunderstood the question.
Then what do I do to write/get the file?Or you could get a batch to REPLACE its own first line using FOR like this

Code: [Select]set DateAndTime=26/06/2010-21:14:00.31
echo off
set thisbatchname=%~nx0
echo old date and time %DateAndTime%
pause
echo set DateAndTime=%date%-%time%>newfile.txt
for /f "skip=1 delims=" %%L in (%thisbatchname%) do (
    echo %%L>>newfile.txt
    )
move /y "%thisbatchname%" "tempname.bat" & ren "newfile.txt" "%thisbatchname%" & del "tempname.bat"

Quote from: sharf on June 26, 2010, 02:00:24 PM

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


Discussion

No Comment Found