terramir
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.
Code: Select all
@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%"
#2 05 Aug 2009 08:19
bluesxman
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: fixed link]]
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 | ruby | chef
----------------------------
#3 05 Aug 2009 17:06
avery_larry
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):
Code: Select all
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%"
----------------------------
#4 05 Aug 2009 19:20
terramir
wait a minute that link is to this post :Sbluesxman 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.
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.
Code: Select all
:: 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%"

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)
----------------------------
#5 05 Aug 2009 20:41
avery_larry
Code: Select all
IF %_ap%==AM then SET _ap=am
IF %_ap%==PM then SET _ap=pm
#6 05 Aug 2009 22:24
terramir
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 beavery_larry wrote:
Code: Select all
IF %_ap%==AM then SET _ap=am IF %_ap%==PM then SET _ap=pm
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
----------------------------
#7 06 Aug 2009 09:34
bluesxman
Double oops, that link was meant to be pointing at this: http://ss64.com/nt/syntax-getdate.htmlterramir wrote:
wait a minute that link is to this post :S
I guarantee you that that code as posted will not work. This should:IF %_ap==AM SET _ap=am
IF %_ap==PM SET _ap=pm
Code: Select all
set _ap=%_ap:AM=am%
set _ap=%_ap:PM=pm%
----------------------------
#8 06 Aug 2009 19:31
Simon Sheppard
I've just uploaded a better script here, its basically the GetDate and GetTime scripts combined
https://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
https://ss64.com/ps/syntax-stampme.html
----------------------------
#9 06 Aug 2009 22:11
terramir
Actually the code does work quite contrary to your guarantee.bluesxman wrote:
Double oops, that link was meant to be pointing at this: https://ss64.com/nt/syntax-getdate.htmlterramir wrote:
wait a minute that link is to this post :S
I guarantee you that that code as posted will not work. This should:IF %_ap==AM SET _ap=am
IF %_ap==PM SET _ap=pm
Code: Select all
set _ap=%_ap:AM=am% set _ap=%_ap:PM=pm%
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
----------------------------
#10 07 Aug 2009 16:11
avery_larry
It would be interesting to see the rest of your code, because this:
Code: Select all
IF %_ap==AM SET _ap=am
IF %_ap==PM SET _ap=pm
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:
Code: Select all
:: 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%"
#11 08 Aug 2009 20:12
terramir
Here's the code that runs in windows xp as a call to a batchfileavery_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:
Code: Select all
@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%"
terramir
Last edited by terramir (08 Aug 2009 20:13)
----------------------------
#12 10 Aug 2009 14:15
bluesxman
In your most recent post:
Code: Select all
IF %_ap%==AM SET _ap=am
IF %_ap%==PM Set _ap=pm
Code: Select all
IF %_ap==AM SET _ap=am
IF %_ap==PM Set _ap=pm
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):
Code: Select all
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%
----------------------------
#13 10 Aug 2009 15:39
terramir
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)
----------------------------
#14 10 Aug 2009 16:12
avery_larry
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:
Code: Select all
:: 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%"
#15 11 Aug 2009 21:28
terramir
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
----------------------------
#16 12 Aug 2009 02:08
terramir
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)
----------------------------
#17 13 Aug 2009 05:40
terramir
Ehhh spoke too soon
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 itavery_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:
Code: Select all
:: 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%"
help (reverting back to am/pm for now even though that will put my file order in a mess)
terramir
----------------------------
#18 13 Aug 2009 21:52
avery_larry
Try double checking all the SET commands to make sure there isn't a space at the end of the line.
----------------------------
#19 13 Aug 2009 21:59
avery_larry
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:
Code: Select all
:: 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%"
----------------------------
#20 14 Aug 2009 04:33
terramir
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?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:
terramir
----------------------------
#21 14 Aug 2009 16:16
avery_larry
Do you mean 'start /b' ? That will start a program without a new window.
----------------------------
#22 24 Aug 2009 21:10
Simon Sheppard
Building on this idea I've now updated the GetTime.cmd exampleavery_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:
Code: Select all
:: 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%"
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.
Code: Select all
::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%
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
----------------------------
#23 25 Aug 2009 15:06
bluesxman
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):
Code: Select all
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
)
cmd | *sh | ruby | chef
----------------------------
#24 25 Aug 2009 20:28
Simon Sheppard
^ Ah yes I like that approach, much neater, I think we have a winner.
----------------------------
#25 26 Nov 2014 11:36
stefano.gatto
Hello,
In order to cut with any misunderstanding between US and Europe regarding the order of month and day in the dates (ie is 09/05/2014 = 9th May 2014 or %th Sept 2014???) I always prefer to spell out in letters the month. Ie I like Tue 9-Sep-2014 as the short date in Windows, so I can also see the weekday.
In this case the script above won't work, right (because of delims=1234567890)?
----------------------------
#26 26 Nov 2014 15:51
stefano.gatto
Sorry my remark was about getting day month and year out of the DATE command. Nothing to do with TIME...
I was looking for something similar to parse the DATE command, regardless of the date format, locale chosen by the user executing the script...
The best I found for the moment is:
REM The following code is to determine the yy, mm, dd independently from the user date output format setup
SET t=2&if "%date%z" LSS "A" set t=1
FOR /F "skip=1 tokens=2-4 delims=(-)" %%a in ('echo.^|date') do (
FOR /F "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
SET %%a=%%d&set %%b=%%e&set %%c=%%f))
SET LOGFILENM=WrapProdAlloc_%yy%%mm%.log
----------------------------
#27 26 Nov 2014 21:00
foxidrive
The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.
Code: Select all
@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause
#28 28 Nov 2014 01:44
Simon Sheppard
I have now updated the StampMe example page so that it works for all Locales and user formats (using WMIC)
----------------------------
#29 28 Nov 2014 02:24
foxidrive
Beauty. I like the quote that is there atm too: “The time you enjoy wasting is not wasted time” ~ Bertrand Russell