# Solved: another DOS backup script question



## cesullivan (Apr 23, 2009)

Hi all,

I want to create a bat file to move files to a new directory. It will be run as a scheduled task some time before midnight. I want it to go to the correct directory and make a new directory that contains today's date in a YYMMDD format. Then it will move all the files to that directory.

Can I take today's date from the %DATE% variable and use it to create a directory?

These is sort of what I'd like to do:

CD C:\data entry stuff\TC30
mkdir 20090423
move *.* 20090423\*.*

20090423 will be created each day and depends on the date.

Thanks.
Cathy


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!

Yes, you can do that. It will depend on the exact format your system uses for the *%date%* variable, which depends on your regional settings.
You can also make it independent of regional settings.

One question, if an error occurs, how would you want it displayed?
Easy to put the error in a text file.
When the task runs will a user be logged on? If so, you can just open the file, so it will be seen the next morning.
If no one is logged on, you can put the file in the Startup folder, so it will open when anyone logs onto the PC.

This version assumes that %date% is *Day MM/DD/YYYY* (Doesn't matter if the *Day* part is present or not).
OS is XP, and no-one is logged on when it runs, so the error file is placed in the All Users Startup folder.

```
@Echo Off
Set _Date=%date:~-10%
Set _Dest=%_Date:~-4%%_Date:~0,2%%_Date:~3,2%
Set _Source=C:\data entry stuff\TC30
Set _Start=C:\Documents and Settings\All Users\Start Menu\Programs\Startup
PushD %_Source%
If Not Exist %_Dest% (
MD %_Dest%
) Else (
>>"%_Start%\Error.txt" Echo The folder %_Dest% Already Exists
>>"%_Start%\Error.txt" Echo No files were copied. Please contact the System Administrator
Goto :EOF
)
Move *.* %_Dest%
PopD
Goto :EOF
```
This version is the same except regional settings don't matter.
If you copy and paste, be sure to retype the line in red. You'll have to position the cursor right after the *=*, and then press TAB then SPACE.
I've attached the file in a zip you can download so you don't have to edit that line.

```
@Echo Off
:: Set the next variable equal to tab followed by a space
:: If you copy this code, you may have to manually edit this line
[COLOR=Red][B]Set _TabSpace=[/B][/COLOR]     
Set _Source=C:\data entry stuff\TC30
Set _Start=C:\Documents and Settings\All Users\Start Menu\Programs\Startup
PushD %_Source%
Call :_GetDate
If Not Exist %_fdate% (
MD %_fdate%
) Else (
>>"%_Start%\Error.txt" Echo The folder %_fdate% Already Exists
>>"%_Start%\Error.txt" Echo No files were copied. Please contact the System Administrator
Goto :EOF
)
Move *.* %_fdate%
PopD
Goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::                    Subroutines
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_GetDate
:: This batch file will always display the same results,
:: independent of "International" settings.
:: This batch file uses REG.EXE from the NT Resource Kit
:: (already installed with WinXP and Vista)
:: to read the "International" settings from the registry.
:: Date is returned as yyyymmdd in variable _fdate
:: Modified by The Outcaste from SortDate Written by Rob van der Woude
:: http://www.robvanderwoude.com
::
If NOT [%1]==[] Set Date=%1
If "%date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
:: Delims= is a TAB followed by a space in the next two lines
For /F "TOKENS=2* DELIMS=%_TabSpace%" %%A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
For /F "TOKENS=2* DELIMS=%_TabSpace%" %%A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B
IF %_iDate%==0 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%D%%B%%C
IF %_iDate%==1 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%D%%C%%B
IF %_iDate%==2 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%B%%C%%D
Goto:EOF
```
HTH

Jerry


----------



## cesullivan (Apr 23, 2009)

it works like a charm! Too bad there isn't more call for DOS batch programming. It seems very powerful, if you can figure it out.

I'm using the second one. 

Cathy


----------



## TheOutcaste (Aug 8, 2007)

Glad it's working for you.

Forgot to mention that you'll have to manually delete the error.txt file or it will re-appear at every log in, though you probably already knew that.
* Start | All Programs | Startup*, right click it and click *Delete*.

The Command Prompt doesn't have many of the more advanced functions of other languages, but most can be duplicated. May take a line or 50 more than other languages, but it's still quite useful.

If your issue has been resolved you (and ONLY you) can mark this thread Solved by using the Mark Solved button at the Top Left of this thread (above the first post) 










Jerry


----------

