Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Date stamping batch file "copy" file

Posted on 2003-03-28
3
Medium Priority
?
383 Views
Last Modified: 2013-12-28
I read several articles on date stamping the output of a batch file "copy" cmd....but was unsure how to apply the fix given. The following:
++++++++++++++++++++++++++++++++++++++++++++
The syntax you gave would work fine on 2000 but not NT4 as the date/time are not environment variables within NT4.
One caveat to this is you need a space after the / delimiter or it won't skip the day, thus:
for /f "tokens=2-4 delims=/ " %%i in ("%DATE%") do set d=%%k%%i%%j
your line for the time looks fine unaltered.
With NT4, as billious has commented you need to use TIME /T and DATE /T to output the date/time, surrounded by single quotes instead of doubles.
+++++++++++++++++++++++++++++++++++++++++++++
This is to advanced for me. How would apply this if I wanted to use copy c:\text.txt c:\(date).txt?

Could someone help me?

0
Comment
Question by:kedpayne2
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
pbarrette earned 2000 total points
ID: 8227232
Hi kedpayne2,

To get the current date in NT, this is the correct command:
---------
FOR /F "TOKENS=2-5 DELIMS=/ " %%F IN ('DATE /T') DO (
 SET MM=%%F
 SET DD=%%G
 SET YYYY=%%H
)
---------

This command parses the output of the "DATE /T" command and places the results into variables which reflect the current date.

To copy your TEXT.TXT file, you would have a batchfile which contained the following:
:: ------EXAMPLE.BAT---------
@ECHO OFF
FOR /F "TOKENS=2-5 DELIMS=/ " %%F IN ('DATE /T') DO (
 SET MM=%%F
 SET DD=%%G
 SET YYYY=%%H
)
SET CURDATE=%MM%%DD%%YYYY%
COPY C:\TEXT.TXT C:\%CURDATE%.TXT
:: ------EXAMPLE.BAT---------

If you just wanted to rename the file, you could use "REN" instead of the "COPY" command.

pb
0
 
LVL 7

Expert Comment

by:billious
ID: 8229208
One little nit-pick:

FOR /F "TOKENS=2-4 DELIMS=/ " %%F IN ('DATE /T') DO (
SET MM=%%F
SET DD=%%G
SET YYYY=%%H
)

Note "2-4", not "2-5" ('though 2-5 would work)

Oh - and this assumes mm/dd/yyyy format. For dd/mm/yy format
FOR /F "TOKENS=2-4 DELIMS=/ " %%F IN ('DATE /T') DO (
SET MM=%%G
SET DD=%%F
SET YYYY=%%H
)

Equivalently, since 'time/t' output is hh:mm then the command
FOR /F "TOKENS=1-2 DELIMS=: " %%F IN ('TIME /T') DO (
SET MN=%%F
SET HH=%%G
)

sets HH as current-hour and MN as current-minutes

Aside to pb:

Microsoft have conveniently changed the output of "date/t" to OMIT the dayname for XP. Just thought you'd like to know.

...Bill


0
 
LVL 10

Expert Comment

by:pbarrette
ID: 8247168
Hi kedpayne2,

Glad to help.

Bill,

You're right about the "2-4" thing, I was copying out from memory and just added the 2nd token + 3 tokens total..

I use XP at home and hadn't noticed the missing dayname. I don't have my telnet server up today so I can't check myself, but I usually parse out %TIME% on 2K/XP so I haven't run into that issue yet.

pb
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Server 2003 introduced persistent Volume Shadow Copies and made 2003 a must-do upgrade.  Since then, it's been a must-implement feature for all servers doing any kind of file sharing.
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question