?
Solved

Date stamping batch file "copy" file

Posted on 2003-03-28
3
Medium Priority
?
382 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: 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

Determining the an SCCM package name from the Package ID
This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

762 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