Dos batch file

TheRiddl3r

Active Member
Joined
May 22, 2007
Messages
50
Im looking for a DOS file that will copy a directory into a new folder that is named as the current date in the following format : 4 May 2008

Anyone?
 

Lino

I am back
Joined
Jan 26, 2008
Messages
13,790
Im looking for a DOS file that will copy a directory into a new folder that is named as the current date in the following format : 4 May 2008

Anyone?

How good are you with manually working with the command prompt? Cause you could create your own file to to do this.
 

werner

Expert Member
Joined
Jun 27, 2005
Messages
3,400
twiddle a bit with this

rem Make Directory With Name consisting of Date
for /f "tokens=1-3 delims=/- " %%a in ('date /t') do set XDate=%%a-%%b-%%c
echo %XDate%
md "%XDate%"


change the %%a %%b %%c order in the second line to change the date format, but bear in mind it uses the date format of the machine it is run on. so if you set it correct on a s.a. machine adn then run it on a US machine the day and month will swop around. the third line is not necessary, just gives you a bit of output so you can see the expteced format....i'd add a REM to the fourth line, fiddle with the a, b and c order till it displays correctly then REM out the third line and make the fourth line active by removing the REM.
 
Last edited:

werner

Expert Member
Joined
Jun 27, 2005
Messages
3,400
of course i left out the "copy directory" part, i assume you dont have a problem with that, just the trickier date section.

let me know if you came right, i have some more examples on my work machine, but will only get back on tuesday...

i found one of my old workbooks and am just going to post this as another way of doing this


rem get the day, date and month in a format we can work with
@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C
)
@echo DAY = %Day%
@echo Month = %Month%
@echo Year = %Year%
rem this line reformats it to american date format, and creates a new textfile, with the date as the contents of the file
echo %month%-%day%-%year% > c:\windows\date.txt
 

TheRiddl3r

Active Member
Joined
May 22, 2007
Messages
50
Well, i got the first bit to work and the copying, only problem is how do you make it copy the entire contents of a directory? It just seems to ignore folders atm.

I used
copy e:\* "%XDate%"
 

werner

Expert Member
Joined
Jun 27, 2005
Messages
3,400
you need to use xcopy, not copy.

xcopy e:\* "%XDate%" /s
assuming you want to copy the whole of the e: drive.

where are your source files...all over e: or in a directory on e:?
assuming they are on e:\source...
xcopy e:\source "%XDate%" /s
will copy the e:\source directory, all files within that directory, and all directories inside the e:\source directory EXCLUDING empty directories

if you need more flexibility then do xcopy/? or have a look at a tool called robocopy.
 

TheRiddl3r

Active Member
Joined
May 22, 2007
Messages
50
Next question.

How do I make it so that it creates the folder in a different directory (Namely My Documents)
 

werner

Expert Member
Joined
Jun 27, 2005
Messages
3,400
the line
md "%XDate%"

in the batch file will make a directory, from wherever the batch file is run from.
so if you copy the batch file to MY DOCUMENTS then it will make a directory inside my documents.

otherwise, twiddle with the line mentioned to chnage the location of the output directory
you will need to change the xcopy line also, so the files are copied there as well.
i dont have a windows machine here to try it on, so cant give you the exact syntax.

but a easy way to do it is to change to the my documents directory first, before making the directory.
cd\
cd "C:\documents and settings\TheRiddl3r\my documents"
md "%XDate%"

as part of the original batch file.
 
Last edited:

bekdik

Honorary Master
Joined
Dec 5, 2004
Messages
12,860
have a look at robocopy:
Run cmd
robocopy /?


It may be useful
 

Lino

I am back
Joined
Jan 26, 2008
Messages
13,790
twiddle a bit with this

rem Make Directory With Name consisting of Date
for /f "tokens=1-3 delims=/- " %%a in ('date /t') do set XDate=%%a-%%b-%%c
echo %XDate%
md "%XDate%"


change the %%a %%b %%c order in the second line to change the date format, but bear in mind it uses the date format of the machine it is run on. so if you set it correct on a s.a. machine adn then run it on a US machine the day and month will swop around. the third line is not necessary, just gives you a bit of output so you can see the expteced format....i'd add a REM to the fourth line, fiddle with the a, b and c order till it displays correctly then REM out the third line and make the fourth line active by removing the REM.

Hey Werner thank you for sorting that out for him.
 

TheRiddl3r

Active Member
Joined
May 22, 2007
Messages
50
This worked fine until about 3 days ago...

for /f "tokens=1-3 delims=/- " %%a in ('date /t') do set XDate=%%c-%%b-%%a
echo %XDate%
cd "C:\documents and settings\Matthew\my documents\FlashDisk Backup\"
md "%XDate%"
cd "C:\documents and settings\Matthew\my documents\FlashDisk Backup\%XDate%"
xcopy e:\* /s

Now it seems to copy half the e:\ and the rest is just ignored

Help? :confused:
 
Top