How to make a batch file copy itself?

Help creating a batch file?

  • I have an MS Access file called data.accdb I want to create a batch file to create a folder called data archive At the end of folder I want the date it was created example data archive 7/21/09 And then copy the file data.accdb into this folder Tomorrow I want to make a new folder with the same name and new date. And I want it to delete any folder that is 5 days old. I am having a very hard time with this, can anyone help me out?

  • Answer:

    Creating is easy....here is the code @echo off set drive=<<place you want to save>> set folder=<<name of folder>> %date:~4,2%_%date:~7,2%_%date:~10,4% set backupcmd=xcopy /s /c /d /e /h /i /r /k /y echo ### Backing up directory... %backupcmd% "<<source path>>" "%drive%\%folder%" echo Your Done! @pause now the delete part is a bit harder...but at least this will get ya started.

Tom P at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Hi ! Use mkdir command to create directories or folders ... the syntax is mkdir path\directory name eg, mkdir c:\windows\mydir creates a directory mydir .... Since you want to name the directory to date, use date function, mkdir c:\windows\foldername-"%date%" run command as it is .... it will create a folder of name something like foldername-wed21 .... Use copy command to copy your file inside the folder ... syntax, copy source path destination path eg, copy D:\somewhere\data.accdb c:\windows\foldername-"%date%" copies your file inside the folder .... Next day, when you run the batch, a new folder name of current date is created .... I immediately cant think of a trick to delete 5 days old folders, will let you know if i figure out and remember you!! Good luck ! Happy scripting !

Dexter

Here is the create, I will think about the delete I found a great delete routine on experts exchange from Steve GTR, and modified my create code to make use of his delete stuff @ECHO OFF REM Modified code from Steve GTR from Experts Exchange setlocal call :GETDATEPARTS "%date%" set newArchive=%mm%-%dd%-%yy% REM Make directory @mkdir "c:\temp\Archive Folder "%newArchive% @copy "c:\datadirectory\todays.data" "c:\temp\Archive Folder "%newArchive% call :SUBTRACTDAYS 5 set oldArchive=%mm%-%dd%-%yy% REM Remove old data @rd /s /q "c:\temp\archive folder %oldArchive% goto :EOF :GETDATEPARTS set dt=%~1 set tok=1-3 if "%dt:~0,1%" GTR "9" set tok=2-4 set yyyy= for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do ( for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c ) if not "%yyyy%"=="" set yy=%yyyy% if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%)) if 1%mm% LSS 100 set mm=0%mm% if 1%dd% LSS 100 set dd=0%dd% goto :EOF :SUBTRACTDAYS set dayCnt=%1 if "%dayCnt%"=="" set dayCnt=1 REM Substract your days here set /A dd=1%dd% - 100 - %dayCnt% set /A mm=1%mm% - 100 :CHKDAY if /I %dd% GTR 0 goto DONESUBTRACT set /A mm=%mm% - 1 if /I %mm% GTR 0 goto ADJUSTDAY set /A mm=12 set /A yy=%yy% - 1 :ADJUSTDAY if %mm%==1 goto SET31 if %mm%==2 goto LEAPCHK if %mm%==3 goto SET31 if %mm%==4 goto SET30 if %mm%==5 goto SET31 if %mm%==6 goto SET30 if %mm%==7 goto SET31 if %mm%==8 goto SET31 if %mm%==9 goto SET30 if %mm%==10 goto SET31 if %mm%==11 goto SET30 REM ** Month 12 falls through :SET31 set /A dd=31 + %dd% goto CHKDAY :SET30 set /A dd=30 + %dd% goto CHKDAY :LEAPCHK set /A tt=%yy% %% 4 if not %tt%==0 goto SET28 set /A tt=%yy% %% 100 if not %tt%==0 goto SET29 set /A tt=%yy% %% 400 if %tt%==0 goto SET29 :SET28 set /A dd=28 + %dd% goto CHKDAY :SET29 set /A dd=29 + %dd% goto CHKDAY :DONESUBTRACT if /I %mm% LSS 10 set mm=0%mm% if /I %dd% LSS 10 set dd=0%dd%

Gene M

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.