Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I have written a batch file that will write the entire contents of drive C: to a text file. I have two problems:
1) The script is told to create a new directory with the date as the current date and then create the text file with the time as the name. The problem is that the mkdir command doesn't like the %date% or %time% variables and returns an error that the syntax is incorrect. How would I get around that?
2)Everytime the script runs on an existing file (such as file.txt), it replaces all the existing text in the document with the new data. Is there a way I can change the command to add to the text file?
My script so far is this:
@ECHO OFF
C:
cd \
cd HDD
mkdir %date%
cd \
dir /OD /S > C:\%date%\%time%.txt
exit
Thanks for your help

To append text to an existing file use the >> redirector instead of >.
About %Date% and %Time% their format is not directly suitable for the MD command, but I need you post exactly what is displayed by typing
Echo D=%Date% T=%Time%
as formats are country dependent.

David,
The second part is easy.
To append to an existing file, use:
[command]>>file
instead of
[command]>file
The first part is a little harder.
Probably the reason the %TIME% and %DATE% vars in directory or file names does not work as hoped is that they contain illegal characters.
Colons and slashes are no good and a few other chars should be avoided.
To quickly learn which chars to avoid, try renaming a file in explorer to something containing a colon.
So depending on how your date / time formats are set [in control panel] you may get errors.
If this is the prob, you have two choices.
Change the date format to something acceptable for file / folder naming.
Or use the 'partial string' facility of XP.
Like this:
set myDATE=%DATE:~-10%
Then use myDATE.
You can guess the rest.
HTH
M2

![]() |
Homework Question!! =)
|
Password Help for VB 6
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |