Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I am working on a batchfile, and the logic of it is that it suppose to copy files from one folder to another.It also needs to add the date to the filenames. I have tried this:REM Set date and time variables
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET DATE=%%B
FOR /F "TOKENS=*" %%A IN ('TIME/T') DO SET TIME=%%A
(@ECHO Current date is %DATE% and time is %TIME%)COPY B:\JobSched\Log\*.log B:\Jobsched\Log\Test\*%DATE%_%TIME%.log
::and this as the main command lineI am lost now and I am not sure how to go about it. I don't know whether I can rename and then copy because just plain COPY is not letting me use these DATE and TIME variables, any response will be appreciated thank you.

First of all, you do not need to process Date and Time commands by a For /F statement as under Windows XP/2K date and time are at your hand in the standard environment variables %Date% and %Time%.
So stated the problem is related to the format they show in your country settings as separators like "/" or "," are not suited to filenames. Some (quite easy) procesing on Date and Time is needed to adapt them.
Please report what you see typing at prompt
Echo Date=%Date% Time=%Time%
and starting from that the problem can be removed.

Hi Wazzup,
For openers, the TIME & DATE vars are built into NT.
Your batch works and sets
dd-mm-yyy hh:mm
This may vary according to how the time & date prefs are set.
In any case, you cannot use illegal chars in file /directory names.
To 'see how it goes', try this:
::**
@echo off > test.txt
echo %TIME:~0,2%%TIME:~3,2%
copy test.txt %TIME:~0,2%%TIME:~3,2%.txt
::**HTH
M2If at first you don't succeed, you're about average.

Ok, I typed in:
Echo Date=%Date% Time=%Time%
and got:
Wed 03/23/2005 11:02:43.91.
I am taking a previously written program so I did not know that, that is what the previous programmer had written so I thought I may be able to use it. Thanks for your quick response.

Hi Wazzup,
Did you understand the point about using only parts of the TIME & DATE vars and avoiding illegal chars?
M2If at first you don't succeed, you're about average.

Sort of,
but could you give me an example on how I can implement it into a YYYYMMDD_HHMMSS, and I still don't understand how to implement it into the COPY command so that it renames the file. If you can't tell I am fairly new to batch files.

So basically how can I use a variable in the COPY command to input the date if I am using illegal characters?

Hi Wazzup,
The basic syntax is:
echo %DATE:~x,y%
which means to skip x chars and use the next y chars.
So since your DATE format is:
Wed 03/23/2005
To use YYYMMDD to stamp a file:
::**
@echo off > somefile.txtset wazdate=Wed 03/23/2005
echo %wazdate%
echo %wazdate:~10,4%%wazdate:~4,2%%wazdate:~7,2%
copy somefile.txt %wazdate:~10,4%%wazdate:~4,2%%wazdate:~7,2%.txt
::**
Are we getting there?
M2If at first you don't succeed, you're about average.

Sorry, had to get some lunch.
Anyway, I tried what you said and it worked.
I was wondering if there is anyway I can get around ending up having
ex:
somefile.txt20050302.txtotherwise, I will try the time on my own and see if I can get it right, I know the other option instead of putting:
copy somedir\*.txt somedir\*%wazdate:~10,4%%wazdate:~4,2%%.txt
is I could put it before the filename, but I don't know if that will organize the files the way I want it
copy somedir\*.txt somedir\%wazdate:~10,4%%wazdate:~4,2%%*.txt
Thank you so much for your help I really appreciate it

It took me a while but with your help I finally understand it:
echo %waztime:~0,2%%waztime:~3,2%%waztime~6,2%
now is there anyway I can do this without adding the extra ".txt" on the end?

Hi Wazzup,
I'm not quite with you, but close.
Can you post your EXACT code, using Dr. Nick's CODEPOST util; available here:
http://students.cs.byu.edu/~drnick/entityrecode.html
M2If at first you don't succeed, you're about average.

@echo off
::**set Loghome=B:\JobSched\Log
set ArchiveLog=B:\JobSched\Log\Test
set Errhome=B:\JobSched\error
set ArchiveError=B:\JobSched\error\Test
set waztime=%TIME%
set wazdate=%DATE%
echo %wazdate%
echo %wazdate:~10,4%%wazdate:~4,2%%wazdate:~7,2%
echo %waztime%
echo %waztime:~0,2%%waztime:~3,2%%waztime:~6,2%
::**
COPY %Loghome%\*.log %ArchiveLog%\*%wazdate:~10,4%%wazdate:~4,2%%wazdate:~7,2%_%waztime:~0,2%%waztime:~3,2%%waztime:~6,2%.log
COPY %Errhome%\*.err %ArchiveError%\*%wazdate:~10,4%%wazdate:~4,2%%wazdate:~7,2%_%waztime:~0,2%%waztime:~3,2%%waztime:~6,2%.errend: return.

Ok, so it copies "somefile.txt" from its directory and puts it in the new directory and changes it to "somefile.txt_20050324_093332.txt"
Now how do I get rid of the first .txt in the filename in this process?

![]() |
![]() |
![]() |

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