RENAMING A FILE TO INCLUDE TIME

J

JOE VAUGHAN

I'm renaming a file with the following syntax.
RENAME pharm_comp_eldetail.txt tpaid_ars.%date:~10,2%%
date:~12,2%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%

the problem is the time doesn't reflect the leading zero
for the hour when the file rename is prior to a two
digit hour. I get a syntax error. However, if I wait
until the hour is a two digit (10 AM vs 8 AM), the sytax
error goes away. If I could get my system to reflect a
two digit hour for any time before 10 AM, I believe the
error would go away. When I type "time", I want it to
reflect 08:00:00 instead of 8:00:00. I've changed my
regional settings to reflect the hh:mm:ss instead of
h:mm:ss, but that's not translating over to command line
prompt "time" which still reflects the single digit for
the hour prior to 10 AM.

Any ideas how?
 
P

Pegasus \(MVP\)

JOE VAUGHAN said:
I'm renaming a file with the following syntax.
RENAME pharm_comp_eldetail.txt tpaid_ars.%date:~10,2%%
date:~12,2%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%

the problem is the time doesn't reflect the leading zero
for the hour when the file rename is prior to a two
digit hour. I get a syntax error. However, if I wait
until the hour is a two digit (10 AM vs 8 AM), the sytax
error goes away. If I could get my system to reflect a
two digit hour for any time before 10 AM, I believe the
error would go away. When I type "time", I want it to
reflect 08:00:00 instead of 8:00:00. I've changed my
regional settings to reflect the hh:mm:ss instead of
h:mm:ss, but that's not translating over to command line
prompt "time" which still reflects the single digit for
the hour prior to 10 AM.

Any ideas how?

Here is an alternative and more flexible solution:

@echo off
for /F "tokens=2-4 delims=/ " %%a in ('echo %date%') do set date_=%%a%%b%%c
for /F "tokens=1-2 delims=:." %%a in ('echo %time%') do set hour=%%a& set
min=%%b
if %hour% LSS 10 (set time_=0%hour%%min%) else (set time_=%hour%%min%)

echo Datestamp is %date_% %time_%
 
J

joe Vaughan

I appreciate so much someone taking the time to assist.
But I'm still at a loss on this.

Two things, first, I don't have a clue what this means or
does. Second, it didn't work as you can see from the
text below.


C:\>@echo off
for /F "tokens=2-4 delims=/ " %%a in ('echo %date%') do
set date_=%%a%%b%%c
%%a was unexpected at this time.
for /F "tokens=1-2 delims=:." %%a in ('echo %time%') do
set hour=%%a& set
%%a was unexpected at this time.
min=%%b
'min' is not recognized as an internal or external
command,
operable program or batch file.
if %hour% LSS 10 (set time_=0%hour%%min%) else (set
time_=%hour%%min%)

echo Datestamp is %date_% %time_%
Datestamp is %date_% 0%hour%%min%
 
P

Pegasus \(MVP\)

joe Vaughan said:
I appreciate so much someone taking the time to assist.
But I'm still at a loss on this.

Two things, first, I don't have a clue what this means or
does. Second, it didn't work as you can see from the
text below.


C:\>@echo off
for /F "tokens=2-4 delims=/ " %%a in ('echo %date%') do
set date_=%%a%%b%%c
%%a was unexpected at this time.
for /F "tokens=1-2 delims=:." %%a in ('echo %time%') do
set hour=%%a& set
%%a was unexpected at this time.
min=%%b
'min' is not recognized as an internal or external
command,
operable program or batch file.
if %hour% LSS 10 (set time_=0%hour%%min%) else (set
time_=%hour%%min%)

echo Datestamp is %date_% %time_%
Datestamp is %date_% 0%hour%%min%

The script failed to work because the lines in my reply
wrapped and you did not unwrap them. Here they are
again; this time I will number them. You must unwrap
the lines, then delete the line numbers before you run
the batch file.

Line 1 =@echo off
Line 2 = for /F "tokens=2-4 delims=/ " %%a in ('echo %date%') do set
date_=%%a%%b%%c
Line 3 =for /F "tokens=1-2 delims=:." %%a in ('echo %time%') do set
hour=%%a& set min=%%b
Line 4 =if %hour% LSS 10 (set time_=0%hour%%min%) else (set
time_=%hour%%min%)

Line 5 =echo Datestamp is %date_% %time_%

Line2 will echo %date% to the screen. My coding will
grab tokens 2, 3 and 4 from the screen output and
re-assemble them into %date_%. I accept forward
slashes and spaces as delimiters between tokens.

Line 3 does much the same thing with the time. Here
I accept colons and full stops as delimiters.

Line 4, of course, adds the leading 0 to those hours
that are less than 10.

To get detailed help on what I did, type

for /?

at a command prompt, and pay particular attention
to the sections that talk about "tokens", "delims"
and the paragraph headed

FOR /F ["options"] %variable IN ('command') DO command [command-parameters]
 
J

joe vaughan

Thanks. As you can see, I'm just a beginner. I'm
picking up some books on NT scripting to help me
understand your solution and broaden my knowledge. I
appreciate you assistance. And thanks for taking the
time to explain it. I'll give it a try. But more
importantly, I'll get some NT shell books and begin the
learning process.

-----Original Message-----

joe Vaughan said:
I appreciate so much someone taking the time to assist.
But I'm still at a loss on this.

Two things, first, I don't have a clue what this means or
does. Second, it didn't work as you can see from the
text below.


C:\>@echo off
for /F "tokens=2-4 delims=/ " %%a in ('echo %date%') do
set date_=%%a%%b%%c
%%a was unexpected at this time.
for /F "tokens=1-2 delims=:." %%a in ('echo %time%') do
set hour=%%a& set
%%a was unexpected at this time.
min=%%b
'min' is not recognized as an internal or external
command,
operable program or batch file.
if %hour% LSS 10 (set time_=0%hour%%min%) else (set
time_=%hour%%min%)

echo Datestamp is %date_% %time_%
Datestamp is %date_% 0%hour%%min%

do
set date_=%%a%%b%%c do
set hour=%%a& set

The script failed to work because the lines in my reply
wrapped and you did not unwrap them. Here they are
again; this time I will number them. You must unwrap
the lines, then delete the line numbers before you run
the batch file.

Line 1 =@echo off
Line 2 = for /F "tokens=2-4 delims=/ " %%a in ('echo % date%') do set
date_=%%a%%b%%c
Line 3 =for /F "tokens=1-2 delims=:." %%a in ('echo % time%') do set
hour=%%a& set min=%%b
Line 4 =if %hour% LSS 10 (set time_=0%hour%%min%) else (set
time_=%hour%%min%)

Line 5 =echo Datestamp is %date_% %time_%

Line2 will echo %date% to the screen. My coding will
grab tokens 2, 3 and 4 from the screen output and
re-assemble them into %date_%. I accept forward
slashes and spaces as delimiters between tokens.

Line 3 does much the same thing with the time. Here
I accept colons and full stops as delimiters.

Line 4, of course, adds the leading 0 to those hours
that are less than 10.

To get detailed help on what I did, type

for /?

at a command prompt, and pay particular attention
to the sections that talk about "tokens", "delims"
and the paragraph headed

FOR /F ["options"] %variable IN ('command') DO command [command-parameters]


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top