Computing.Net > Forums > Programming > Batch File Help

Batch File Help

Reply to Message Icon

Original Message
Name: shmyrbob
Date: September 7, 2005 at 15:07:45 Pacific
Subject: Batch File Help
OS: 98/2k/xp
CPU/Ram: 128-256
Comment:

I have looked through many batch file tutorials and web pages trying to figue out how I can use a batch file to create a directory with the current date and then copy some files to that directory.

Any idea's/suggestions would be greatly appreciated. Thanks



Report Offensive Message For Removal


Response Number 1
Name: Cache
Date: September 7, 2005 at 16:19:48 Pacific
Reply: (edit)

This may not be as trivial as it sounds, for a BATCH file at least.

The variable %date% can be used to output the current date but, the format of the output is where the problem starts.

The output from the command %date% would look like this: 08/09/2005

As you may know, the "/" character cannot be used in folder names. So using the output of %date% for a folder name would actually make a folder named something like "08" (without the quotes).

You would need to replace all assurances of / with a period, or similar legal char.


Report Offensive Follow Up For Removal

Response Number 2
Name: Cache
Date: September 7, 2005 at 16:23:22 Pacific
Reply: (edit)

oops lol, assurances = occurrences


Report Offensive Follow Up For Removal

Response Number 3
Name: uli_glueck
Date: September 8, 2005 at 00:16:07 Pacific
Reply: (edit)

This batch create directory for example:
08.09.2005
Replace the echo Command against the xcopy.


@echo off

date /t >date.txt


FOR /f "tokens=2 skip=2 delims= " %%a in ('find "." date.txt') do (

mkdir %%a
echo %%a

)

hope this helps
uli


Report Offensive Follow Up For Removal

Response Number 4
Name: shmyrbob
Date: September 8, 2005 at 07:27:13 Pacific
Reply: (edit)

Uli, I tried the code above and had to change the find "." to find " " to get the variable to contain the date, but it is still coming up in the xx/xx/xxxx format.

Thanks for your suggestions.


Report Offensive Follow Up For Removal

Response Number 5
Name: Mechanix2Go
Date: September 8, 2005 at 07:49:30 Pacific
Reply: (edit)

Hi uli,

That will work for 2k/xp, but not 9x.

In Nt there is a built-in %date%, so you just need to accomodate the layout.

My date layout is:

Thu 08-09-2005

So I can use this:

xcopy somefile %date:~10,4%%date:~7,2%%date:~4,2%\

[xcopy creates the directory for you]
###
I DOS and DOS based winders [3x 9x] it takes a little more doing.

This will get DAY, DATE & TIME into vars:

:: DDT-DOS.bat

echo.|date>datevar.bat
echo.|time>timevar.bat

echo set DATEvar=%%5>the.bat
call datevar

echo set DAYvar=%%4>the.bat
call datevar

echo set TIMEvar=%%4>the.bat
call timevar
:: done

As pointed out by Cache, there are some possible snags, not least is illegal chars.

Another is the verbage which is spewed out by the DATE and TIME commands.

It will vary widely with, among other things, language, version, codepage and regiomal settings.

To get a nice neat YYYYMMDD as my NT example, you need debug.

HTH



If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal


Response Number 6
Name: uli_glueck
Date: September 9, 2005 at 04:33:36 Pacific
Reply: (edit)

Oh , sorry.
My NT4 date layout is Fr 09.09.2005 (european Version).
I don“t use the command line from Win9*.

Hi M2,
my not really working solution is a bit bumpy, not as elegant as yours. ;-)
I try to do to much with the For loop. But I do my best to learn.
Thanks a lot. :-))

Uli


Report Offensive Follow Up For Removal

Response Number 7
Name: robmc5
Date: September 21, 2005 at 07:54:16 Pacific
Reply: (edit)

Really just use this....

md c:\dirname\%Date:~-4, 4%%Date:~-10, 2%%Date:~-7, 2%

copy filename c:\dirname\%Date:~-4, 4%%Date:~-10, 2%%Date:~-7, 2%


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch File Help

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes Today.
Discuss in The Lounge