#1 04 Aug 2009 20:46

terramir
Member
Registered: 04 Aug 2009
Posts: 12

stampme.cmd anyway to make it use 2 digit month and day and am/pm

here's the script I'm using right now, it's actually used as a call and eventually I'm gonna get rid of the echo display because I'll be running this script 18 times an hour as a call to a batch file that uses wget to save 18 files off the web every hour then I move them to a specific folder and rename them with this script call to prevent overwriting. sorting the file by date becomes problimatic in single digit days and months because the october november and december mon ths will be shown first
so any help would be appriciated.

  @ECHO off
   SETLOCAL
   IF [%1] NEQ [] goto s_start

   :: Author - Simon Sheppard, July 2003
   :: Tested for Windows NT, 2K, XP

   ECHO STAMPME.cmd
   ECHO REName a file with the DATE/Time
   ECHO.
   ECHO SYNTAX
   ECHO       STAMPME TestFile.txt
   ECHO.
   ECHO       STAMPME "Test File.txt"
   ECHO.
   ECHO       STAMPME "c:\docs\Test File.txt"
   ECHO.
   ECHO       Will rename the file in the format "Test File-02-12-27@16-55.txt"
   ECHO.
   ECHO       In a batch file use CALL STAMPME ...

   :: To change the filename format just change around the last line below

   GOTO :eof
   
   :s_start

    SET _file=%~n1%
    SET _pathname=%~f1%
    SET _ext=%~x1%

    ::Get the date
   ::  note ISO 8601 date format would require 4 digit YYYY Year)

   FOR /f "tokens=6-8 delims=/ " %%G IN ('NET TIME \\%computername%') DO (
         SET _mm=%%G
         SET _dd=%%H
         SET _yy=%%I
     )

   :: Get the time
   FOR /f "tokens=1,2 delims=: " %%G IN ('time/t') DO (
         SET _hr=%%G
         SET _min=%%H
   )

   ECHO Today is Year: [%_yy%] Month: [%_mm%] Day: [%_dd%]
   ECHO The time is:   [%_hr%]:[%_min%]

   REN "%_pathname%" "%_yy%%_mm%%_dd%%_hr%%_min%%_file%%_ext%"

Offline

#2 05 Aug 2009 08:19

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 931

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

I don't really understand why you're using "net time" to get the date -- you're making your life more difficult than it needs to be.

You'd be better off using this script to figure out the components of the date -- it works independently of the regional date setting, or so it claims.

EDIT: Oops, that's not your script is it.  Well, it's still not using the best method to get the date components.

Last edited by bluesxman (05 Aug 2009 08:27)


cmd | *sh  | KiX | AutoIT3 | PowerShell | Puppet

Offline

#3 05 Aug 2009 17:06

avery_larry
Member
Registered: 11 Jul 2007
Posts: 266

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

I have to agree that this isn't the best script in the world.

However, to answer your question, you'll need to pad the month, day, and hour with zeros.  You can do it like this:

(untested, only showing the changed code):

  FOR /f "tokens=6-8 delims=/ " %%G IN ('NET TIME \\%computername%') DO (
         SET _mm=0%%G
         SET _dd=0%%H
         SET _yy=%%I
     )

   :: Get the time
   FOR /f "tokens=1,2 delims=: " %%G IN ('time/t') DO (
         SET _hr=0%%G
         SET _min=%%H
   )

   ECHO Today is Year: [%_yy%] Month: [%_mm:~-2%] Day: [%_dd:~-2%]
   ECHO The time is:   [%_hr:~-2%]:[%_min%]

   REN "%_pathname%" "%_yy%%_mm:~-2%%_dd:~-2%%_hr:~-2%%_min%%_file%%_ext%"

Note further that you may have issues with hour -- the code doesn't distinquish between am and pm (unless your computer is set to 24hour time).

Offline

#4 05 Aug 2009 19:20

terramir
Member
Registered: 04 Aug 2009
Posts: 12

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

bluesxman wrote:

I don't really understand why you're using "net time" to get the date -- you're making your life more difficult than it needs to be.

You'd be better off using this script to figure out the components of the date -- it works independently of the regional date setting, or so it claims.

EDIT: Oops, that's not your script is it.  Well, it's still not using the best method to get the date components.

wait a minute that link is to this post :S
well I using the script I found on the website attached just modified some display components.
also I see the padding down there I see that somehow it will display it in a way that trunicates the 0 if it's a 2 digit month or day fine now I just need a way to set a variable that will display either am or pm after it retrives it from where ever my script retieves it's time
Actually I fixed the am/pm thing but I still got a few problems look at both code pieces below

okies I fixed it so it will write AM or PM in the file and the display, however I still dun like it this is what I did.


   :: Get the time
   FOR /f "tokens=1,2,3 delims=: " %%G IN ('time/t') DO (
         SET _hr=0%%G
         SET _min=%%H
         SET _ap=%%I
   )
 

   ECHO Today is Year: %_yy% Month: %_mm:~-2% Day: %_dd:~-2%
   ECHO The time is:   %_hr:~-2%:%_min% %_ap%

   REN "%_pathname%" "%_yy%%_mm:~-2%%_dd:~-2%%_hr:~-2%%_min%%_ap%%_file%%_ext%"

It comes out as AM and PM in caps, so I tried this with no sucess :wall:
       SET _ap=%%I
   )
IF _ap=AM then SET _ap=am
IF _ap=PM then SET _ap=pm
ECHO Today is Year: %_yy% Month: %_mm:~-2% Day: %_dd:~-2%
that gave me an unexpected AM error what am I doing wrong here?



help
terramir

Last edited by terramir (05 Aug 2009 20:19)

Offline

#5 05 Aug 2009 20:41

avery_larry
Member
Registered: 11 Jul 2007
Posts: 266

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

IF %_ap%==AM then SET _ap=am
IF %_ap%==PM then SET _ap=pm

Offline

#6 05 Aug 2009 22:24

terramir
Member
Registered: 04 Aug 2009
Posts: 12

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

avery_larry wrote:

IF %_ap%==AM then SET _ap=am
IF %_ap%==PM then SET _ap=pm

actually turns out then is not a recognized batch command it's implied in if I just read up so the lines turn out to be
IF %_ap==AM SET _ap=am
IF %_ap==PM SET _ap=pm
and that works tongue
terramir
I'm wondering if I Could remove anything displayed out of the source code and maybe even integrate this into the other batchfile I'm calling this from. well we'll see how long it takes me for a final version.
thanks for all your help
If I got any more troubles I'll give ya guys a yell.
terramir

Offline

#7 06 Aug 2009 09:34

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 931

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

terramir wrote:

wait a minute that link is to this post :S

Double oops, that link was meant to be pointing at this: http://ss64.com/nt/syntax-getdate.html

IF %_ap==AM SET _ap=am
IF %_ap==PM SET _ap=pm

I guarantee you that that code as posted will not work.  This should:

set _ap=%_ap:AM=am%
set _ap=%_ap:PM=pm%

cmd | *sh  | KiX | AutoIT3 | PowerShell | Puppet

Offline

#8 06 Aug 2009 19:31

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 750
Website

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

I've just uploaded a better script here, its basically the GetDate and GetTime scripts combined
http://ss64.com/nt/syntax-stampme.html

Will look at including the am/pm option when I get more time, I tend to just set 24 hour time in the control panel

Also a powershell version
http://ss64.com/ps/syntax-stampme.html

Offline

#9 06 Aug 2009 22:11

terramir
Member
Registered: 04 Aug 2009
Posts: 12

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

bluesxman wrote:
terramir wrote:

wait a minute that link is to this post :S

Double oops, that link was meant to be pointing at this: http://ss64.com/nt/syntax-getdate.html

IF %_ap==AM SET _ap=am
IF %_ap==PM SET _ap=pm

I guarantee you that that code as posted will not work.  This should:

set _ap=%_ap:AM=am%
set _ap=%_ap:PM=pm%

Actually the code does work quite contrary to your guarantee.

But I'm still having problems with the windows file order at times the am's and pm's get mixed up because they are of the same date and retrival times differ depending on my connection  so a file retrived at 12:11 am may get mixed in with files retrived between 12:10 pm and 12:30 pm. yes at times my connection crawls and at other times the files are all retrived with-in a minute.
So I'm thinking it would be nice to have a code that converts my am/pm clock to 24 hours but I ran my head against the :wall: with two things so far A: I can't add 12 hours to the _hr variable
and B: I dunno if "IF and commands actually work the only way top test that is after midnight (or change your clock but I haven't solved the adding 12 issue yet.
  :: Get the time
   FOR /f "tokens=1,2,3 delims=: " %%G IN ('time/t') DO (
         SET _hr=0%%G
         SET _min=%%H
     SET _ap=%%I
   
   )

If %_ap%==AM AND _hr==12 SET _hr=00 (I dunno if that would even work)
If %_ap%==PM SET _hr=%_hr%+12(the syntax of the command arrrrrrrrrrrrrrrrg :wall::pc:

ECHO Today is Year: %_yy% Month: %_mm:~-2% Day: %_dd:~-2%
   ECHO The time is:   %_hr:~-2%:%_min%

   REN "%_pathname%" "%_yy%%_mm:~-2%%_dd:~-2%%_hr:~-2%%_min%%_file%%_ext%"
If I could get this to work it would solvwe alot of my problems!!!!

help
terramir

Offline

#10 07 Aug 2009 16:11

avery_larry
Member
Registered: 11 Jul 2007
Posts: 266

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

It would be interesting to see the rest of your code, because this:

IF %_ap==AM SET _ap=am
IF %_ap==PM SET _ap=pm

when inside a batch file, does not do anything on any type of dos that I've ever used.

For the 12 - 24 conversion, you must do 2 different things.  First you figure out the am/pm and convert, and then you need to pad the leading zeros.  That'll change the first for loop where we're already padding zeros like this:

 :: Get the time
   FOR /f "tokens=1,2,3 delims=: " %%G IN ('time/t') DO (
     if /i "%%I"=="PM" (
        set /a _hr = %%G + 12
        ) else (
           set _hr=0%%G
     )
     SET _min=%%H
   )

ECHO Today is Year: %_yy% Month: %_mm:~-2% Day: %_dd:~-2%
ECHO The time is:   %_hr:~-2%:%_min% 

   REN "%_pathname%" "%_yy%%_mm:~-2%%_dd:~-2%%_hr:~-2%%_min%%_file%%_ext%"

Offline

#11 08 Aug 2009 20:12

terramir
Member
Registered: 04 Aug 2009
Posts: 12

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

avery_larry wrote:

It would be interesting to see the rest of your code, because this:

IF %_ap==AM SET _ap=am
IF %_ap==PM SET _ap=pm

when inside a batch file, does not do anything on any type of dos that I've ever used.

For the 12 - 24 conversion, you must do 2 different things.  First you figure out the am/pm and convert, and then you need to pad the leading zeros.  That'll change the first for loop where we're already padding zeros like this:

Here's the code that runs in windows xp as a call to a batchfile

 @ECHO off
   SETLOCAL
   IF [%1] NEQ [] goto s_start

   :: Author - Simon Sheppard, July 2003
   :: Tested for Windows NT, 2K, XP 
   :: Modified by terramir august 2009

   ECHO STAMPME.cmd
   ECHO REName a file with the DATE/Time
   ECHO.
   ECHO SYNTAX
   ECHO       STAMPME TestFile.txt
   ECHO.
   ECHO       STAMPME "Test File.txt"
   ECHO.
   ECHO       STAMPME "c:\docs\Test File.txt"
   ECHO.
   ECHO       Will rename the file in the format "200908071137amTest File.txt"
   ECHO.
   ECHO       In a batch file use CALL STAMPME ...

   :: To change the filename format just change around the last line below

   GOTO :eof
   
   :s_start

    SET _file=%~n1%
    SET _pathname=%~f1%
    SET _ext=%~x1%

    ::Get the date
   ::  note ISO 8601 date format would require 4 digit YYYY Year)

   FOR /f "tokens=6-8 delims=/ " %%G IN ('NET TIME \\%computername%') DO (
         SET _mm=0%%G
         SET _dd=0%%H
         SET _yy=%%I
     )

   :: Get the time
   FOR /f "tokens=1,2,3 delims=: " %%G IN ('time/t') DO (
         SET _hr=0%%G
         SET _min=%%H
     SET _ap=%%I
   )
  IF %_ap%==AM SET _ap=am 
IF %_ap%==PM Set _ap=pm

   ECHO Today is Year: %_yy% Month: %_mm:~-2% Day: %_dd:~-2%
   ECHO The time is:   %_hr:~-2%:%_min% %_ap%

   REN "%_pathname%" "%_yy%%_mm:~-2%%_dd:~-2%%_hr:~-2%%_min%%_ap%%_file%%_ext%"

And what is does is convert the AM to am and the PM to pm
terramir

Last edited by terramir (08 Aug 2009 20:13)

Offline

#12 10 Aug 2009 14:15

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 931

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

In your most recent post:

IF %_ap%==AM SET _ap=am 
IF %_ap%==PM Set _ap=pm

Not the same as what you posted earlier:

IF %_ap==AM SET _ap=am 
IF %_ap==PM Set _ap=pm

But putting the nit-picking aside and addressing with your AM/PM sorting trouble...

I personally only ever use the 24 hour clock, so never face such a problem.  However, if I were working against that antiquated system wink I'd probably do something like this (untested):

FOR /f "tokens=1,2,3 delims=: " %%G IN ('time/t') DO (
    SET _hr=0%%G
    SET _min=%%H
    SET _ap=%%I
)

REM remove zero padding from _hr
set /a _hr=(%_hr:~-2,1% * 10) + %_hr:~-1%

REM if it's "PM", turn it into its 24 hour equivalent
if /i "%_ap%" EQU "PM" set /a "_hr=(_hr + 12) %% 24"

REM put the zero padding back on
set _hr=0%_hr%
set _hr=%_hr:~-2%

cmd | *sh  | KiX | AutoIT3 | PowerShell | Puppet

Offline

#13 10 Aug 2009 15:39

terramir
Member
Registered: 04 Aug 2009
Posts: 12

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

looks good except that the paddin should just wait till the final step and there's nothing to turn 12am into 00 hrs
i'll give it a try laters when my keyboard starts workig again [his is typed ith osk]
terramir

Last edited by terramir (10 Aug 2009 15:40)

Offline

#14 10 Aug 2009 16:12

avery_larry
Member
Registered: 11 Jul 2007
Posts: 266

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

I slightly messed up my 12 to 24 hour conversion.  I forgot that 12:00 am needs to be 00 (not 12) and 12:00 pm needs to be 12 (not 24).  To that end:

 :: Get the time
   FOR /f "tokens=1,2,3 delims=: " %%G IN ('time/t') DO (
     if /i "%%I"=="PM" (
        set /a _hr = %%G + 12
        if %%G==12 set _hr=12
        ) else (
           set _hr=0%%G
           if %%G==12 set _hr=00
     )
     SET _min=%%H
   )

ECHO Today is Year: %_yy% Month: %_mm:~-2% Day: %_dd:~-2%
ECHO The time is:   %_hr:~-2%:%_min% 

   REN "%_pathname%" "%_yy%%_mm:~-2%%_dd:~-2%%_hr:~-2%%_min%%_file%%_ext%"

Offline

#15 11 Aug 2009 21:28

terramir
Member
Registered: 04 Aug 2009
Posts: 12

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

looks good so far I just noticed today you replied, I'm recording some tv shows so I'll have to wait till 3pm local to start messing with the system clock and test it at various times
Ty
I'll report back laterz
terramir

Offline

#16 12 Aug 2009 02:08

terramir
Member
Registered: 04 Aug 2009
Posts: 12

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

Works just fine including the 00 hours after midnight.
now to optimize the other code I'm working on it's 4kb just because I dunno how to loop etc.
and replace things
. terramir

So consider this one solved
Thank you guys very much

Last edited by terramir (12 Aug 2009 02:09)

Offline

#17 13 Aug 2009 05:40

terramir
Member
Registered: 04 Aug 2009
Posts: 12

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

Ehhh spoke too soon

avery_larry wrote:

I slightly messed up my 12 to 24 hour conversion.  I forgot that 12:00 am needs to be 00 (not 12) and 12:00 pm needs to be 12 (not 24).  To that end:

 :: Get the time
   FOR /f "tokens=1,2,3 delims=: " %%G IN ('time/t') DO (
     if /i "%%I"=="PM" 
        set /a _hr = %%G + 12
        if %%G==12 set _hr=12
        ) else (
           set _hr=0%%G
           if %%G==12 set _hr=00
     )
     SET _min=%%H
   )

ECHO Today is Year: %_yy% Month: %_mm:~-2% Day: %_dd:~-2%
ECHO The time is:   %_hr:~-2%:%_min% 

   REN "%_pathname%" "%_yy%%_mm:~-2%%_dd:~-2%%_hr:~-2%%_min%%_file%%_ext%"

this code messes up between the hours of 8 and 9 pm with the following error: Invalid number blah blah blah constants and then it shows the correct date but the time is blank hours and correct minutes after 10 it sort of works again but the files get named : 200908122035a.jpg and that's with the clock set to 10:35pm a dn considering it's 1035pm  according to the clock and the date is august 12th 2009 it should read 200908122235a.jpg. Hope that gives someone a clue how to fix it
help (reverting back to am/pm for now even though that will put my file order in a mess)
terramir

Offline

#18 13 Aug 2009 21:52

avery_larry
Member
Registered: 11 Jul 2007
Posts: 266

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

Try double checking all the SET commands to make sure there isn't a space at the end of the line.

Offline

#19 13 Aug 2009 21:59

avery_larry
Member
Registered: 11 Jul 2007
Posts: 266

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

What do you get when you:

echo %time%

??

on my machine, %time% is already in 24 hour format, where 'time /t' is in 12 hour format.

To that end:

:: Get the time
   FOR /f "tokens=1,2,3 delims=:. " %%G IN ("%time%") DO (
     set "_hr=0%%G"
     SET "_min=%%H"
)

ECHO Today is Year: %_yy% Month: %_mm:~-2% Day: %_dd:~-2%
ECHO The time is:   %_hr:~-2%:%_min% 

   REN "%_pathname%" "%_yy%%_mm:~-2%%_dd:~-2%%_hr:~-2%%_min%%_file%%_ext%"

Last edited by avery_larry (13 Aug 2009 22:00)

Offline

#20 14 Aug 2009 04:33

terramir
Member
Registered: 04 Aug 2009
Posts: 12

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

avery_larry wrote:

What do you get when you:

echo %time%

??

on my machine, %time% is already in 24 hour format, where 'time /t' is in 12 hour format.

To that end:

that seems to do the trick now the big question should I keep this code as a call or should I integrate it, I dunno if it's worth the effort, btw how do you suppress the cmd prompt window from popping up?
terramir

Offline

#21 14 Aug 2009 16:16

avery_larry
Member
Registered: 11 Jul 2007
Posts: 266

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

Do you mean 'start /b'  ?  That will start a program without a new window.

Offline

#22 24 Aug 2009 21:10

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 750
Website

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

avery_larry wrote:

What do you get when you:

echo %time%

??

on my machine, %time% is already in 24 hour format, where 'time /t' is in 12 hour format.

To that end:

:: Get the time
   FOR /f "tokens=1,2,3 delims=:. " %%G IN ("%time%") DO (
     set "_hr=0%%G"
     SET "_min=%%H"
)

ECHO Today is Year: %_yy% Month: %_mm:~-2% Day: %_dd:~-2%
ECHO The time is:   %_hr:~-2%:%_min% 

   REN "%_pathname%" "%_yy%%_mm:~-2%%_dd:~-2%%_hr:~-2%%_min%%_file%%_ext%"

Building on this idea I've now updated the GetTime.cmd example

Although the Default time delimiter, in Windows XP and above is either . or : the users can change the delimiter to just about any character they like. And you know theres always that one guy, the one who writes everything in green ink, who will do this!

So to make this version even more robust it reads the time delimiter from the HKCU registry.

  ::GetTime.cmd
  @echo off
  SETLOCAL

  :: Get the time separator
  FOR /F "TOKENS=3" %%D IN ('REG QUERY ^"HKEY_CURRENT_USER\Control Panel\International^" /v sTime ^| find ^"REG_SZ^"') DO (
        SET _time_sep=%%D)

  FOR /f "tokens=1,2,3 delims=:.%_time_sep%" %%G IN ("%time%") DO (
  set "_hr=%%G"
  set "_min=%%H"
  set "_sec=%%I"
  )

  :: Strip any leading spaces
  set _hr=%_hr: =%

  :: Ensure the hours have a leading zero
  if 1%_hr% LSS 20 set _hr=0%_hr%

  ECHO The time is:   %_hr%:%_min%:%_sec% 
  ENDLOCAL&SET _time=%_hr%:%_min%

http://ss64.com/nt/syntax-gettime.html

A further wrinkle is that time delimiters can use more than one character 09--30--12 am or 09,,30,,12 am are both valid time formats, why Microsoft thought that was a worthwhile option to allow I can only wonder. Luckily the FOR command considers consecutive delimiters to count as just one, something many would consider a bug, but in this case it means any duplicate delimiters disappear in the script above (it always returns HH:MM).

One remaining thing that might cause a few issues would be using 'Special' characters as a Time Delimiter, I doubt theres an easy fix for this but here's the results of running echo %time% with a few unconventional time separators:

HH"mm
18"32"12.38

HH|mm
'33' is not recognized as an internal or external command,
operable program or batch file.

HH^mm
183227.30

HH^^mm
18^33^51.96

HH`mm
18`35`46.87

HH>mm
With the above delimiter, echo %time% generates a text file containing the Hour, the filename is the seconds and milliseconds.

Interestingly Powershell can handle silly Time Delimiters like this without any problem, another advantage of treating everything as an object:
PS C:\> get-date

Monday, August 24, 2009 18>37>15

Offline

#23 25 Aug 2009 15:06

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 931

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

This is a cheap and cheerful method for breaking down the time components, without needing to delve into the registry (though as you've noted, Simon, certain delimiters will probably give it a headache):

for /f "tokens=1-3 delims=1234567890 " %%a in ("%time%") do set "delims=%%a%%b%%c"
for /f "tokens=1-4 delims=%delims%" %%a in ("%time%") do (
    set hh=%%a
    set mm=%%b
    set ss=%%c
    set ms=%%d
)

Last edited by bluesxman (25 Aug 2009 15:06)


cmd | *sh  | KiX | AutoIT3 | PowerShell | Puppet

Offline

#24 25 Aug 2009 20:28

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 750
Website

Re: stampme.cmd anyway to make it use 2 digit month and day and am/pm

^ Ah yes I like that approach, much neater, I think we have a winner.

Offline

Board footer

Powered by FluxBB