Computing.Net > Forums > Windows XP > Batch File that adds Date

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch File that adds Date

Reply to Message Icon

Name: Wazzup618
Date: March 23, 2005 at 07:58:16 Pacific
OS: Windows/XP
CPU/Ram: I4/512M
Comment:

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 line

I 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.



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: March 23, 2005 at 08:13:24 Pacific
Reply:

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.


0

Response Number 2
Name: Mechanix2Go
Date: March 23, 2005 at 08:17:19 Pacific
Reply:

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



M2

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


0

Response Number 3
Name: Mechanix2Go
Date: March 23, 2005 at 08:26:08 Pacific
Reply:

Hi IVO,

I guess I type too slow.


M2

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


0

Response Number 4
Name: IVO
Date: March 23, 2005 at 08:50:52 Pacific
Reply:

Well done M2G, train you the baby; I have to go now and I know he is in good hands.


0

Response Number 5
Name: Mechanix2Go
Date: March 23, 2005 at 09:05:58 Pacific
Reply:

IVO,

bona sera


M2

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


0

Related Posts

See More



Response Number 6
Name: Wazzup618
Date: March 23, 2005 at 09:06:46 Pacific
Reply:

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.


0

Response Number 7
Name: Mechanix2Go
Date: March 23, 2005 at 09:09:42 Pacific
Reply:

Hi Wazzup,

Did you understand the point about using only parts of the TIME & DATE vars and avoiding illegal chars?


M2

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


0

Response Number 8
Name: Wazzup618
Date: March 23, 2005 at 09:13:44 Pacific
Reply:

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.


0

Response Number 9
Name: Wazzup618
Date: March 23, 2005 at 09:22:02 Pacific
Reply:

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


0

Response Number 10
Name: Mechanix2Go
Date: March 23, 2005 at 09:28:37 Pacific
Reply:

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.txt

set 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?


M2

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


0

Response Number 11
Name: Wazzup618
Date: March 23, 2005 at 10:59:18 Pacific
Reply:

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.txt

otherwise, 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


0

Response Number 12
Name: Wazzup618
Date: March 23, 2005 at 11:59:49 Pacific
Reply:

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?


0

Response Number 13
Name: Mechanix2Go
Date: March 23, 2005 at 19:59:24 Pacific
Reply:

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



M2

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


0

Response Number 14
Name: Wazzup618
Date: March 24, 2005 at 07:29:15 Pacific
Reply:

@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%.err

end: return.



0

Response Number 15
Name: Wazzup618
Date: March 24, 2005 at 07:34:59 Pacific
Reply:

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?


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Windows XP Forum Home


Sponsored links

Ads by Google


Results for: Batch File that adds Date

batch file that del all in folder? www.computing.net/answers/windows-xp/batch-file-that-del-all-in-folder/163673.html

Batch File www.computing.net/answers/windows-xp/batch-file/180382.html

Batch file to delete by date www.computing.net/answers/windows-xp/batch-file-to-delete-by-date-/135330.html