Timestamp format problem

Acer Aspire 5920g
August 20, 2010 at 11:27:30
Specs: Microsoft Windows 7 Professional, 2.201 GHz / 2046 MB
Hello guys i have encountered a problem whereby the timedate rename script does not work for certain computer with different date format. Currently it works for computer with date format set to M/d/YYYY. However i want the rename function to work for computer with date format set to dd/MM/yyy..will appreciate if you guys can give me some tips..thanks!


ren C:\final.txt Audit_Report."(%date:~10%-%date:~4,2%-%date:~7,2%%time:~0,2%h%time:~3,2%m%time:~6,2%s%)".txt


See More: Timestamp format problem

Report •


#1
August 20, 2010 at 12:04:55
SET Day=%DATE:~0,2%
SET Month=%DATE:~3,2%
SET Year=%DATE:~6,4%
SET NewDate=%Day%-%Month%-%Year%

SET Hour=%TIME:~0,2%
SET Minute=%TIME:~3,2%
SET Second=%TIME:~6,2%
SET NewTime=%Hour%h%Minute%m%Second%s

RENAME C:\final.txt Audit_Report."(%NewDate% %NewTime%)".txt


Report •

#2
August 21, 2010 at 01:08:08
This should run regardless of the date format in use, not fully tested:

@echo off
cls
setlocal 

set fil=%temp%\fil.vbs

(
echo Newdate = Date
echo Yyyy = DatePart("YYYY", Newdate^)
echo   Mm = DatePart("M"   , Newdate^)
echo   Dd = DatePart("D"   , Newdate^)

echo Wscript.Echo Yyyy^&" "^&Mm^&" "^&Dd
)>%fil%

FOR /F "tokens=1-3" %%A in ('cscript //nologo %fil%') do (
        set year=%%A&set month=%%B&set day=%%C
	)
if %month% lss 10 set month=0%month%
if %day% lss 10 set day=0%day%

set newdate=%year%-%month%-%day%-
>%fil% echo wscript.echo time

for /f "tokens=1-3 delims=:/." %%1 in ('cscript //nologo %fil%') do (
    set newtime=%%1h%%2m%%3s
)
ren C:\final.txt Audit_Report."(%newdate%%newtime%)".txt

del %fil%



Report •

#3
August 21, 2010 at 02:42:29
@Wahine, i would suggest not using hybrids, since with vbscript, its enough to do the job. Mixing batch syntax with vbscript is messy, and you have to take care for interchanging syntaxes. Furthermore, vbscript syntax is way more readable than batch. It is absolutely not necessary to use batch at all.

GNU win32 packages | Gawk


Report •

Related Solutions

#4
August 21, 2010 at 10:19:02
thanks Matt and Wahine, both works! I am gonna study da codes now


Report •

#5
August 21, 2010 at 10:23:59
Not a problem! If you have any questions, feel free to ask!

Report •

#6
August 21, 2010 at 18:54:51
ghostdog - Furthermore, vbscript syntax is way more readable than batch. It is absolutely not necessary to use batch at all.

We could really get into a discussion on those points. Reviewing the posts about scripting (which I have NOT done) I wouldn't hesitate to bet a few greens that the majority are requesting help with batch scripting. As to VBS being more readable than batch, it's all in the eye of the beholder.

My personal preference is batch scripting if the requirement can be met in batch. To that end I tender a non-hybrid, full batch script to meet the OP's request. Again not fully tested.

@echo off
cls
setlocal enabledelayedexpansion

:: Get date format.
for /f "skip=1 tokens=1-5 delims=() " %%1 in ('echo.^|date') do (
    set dateformat=%%5
)
for /f "tokens=1-3 delims=-" %%1 in ("%dateformat%") do (
    set first=%%1
    set secnd=%%2
    set third=%%3
)

:: Get current date.
for /f "tokens=1-5* delims= " %%1 in ('echo.^|date') do (
    set currdate=%%6&goto cont
)

:cont
:: Get current date into dd mm yy variables.
for /f "tokens=1-2* delims=1./-" %%1 in ("%currdate%") do (
    set %first%=%%1&set %secnd%=%%2&set %third%=%%3
)
set newdate=%yy%-%mm%-%dd%&set newdate=!newdate: =!-

:: Get current time.
for /f "tokens=1-4*" %%1 in ('echo.^|time') do (
    set currtime=%%5&goto cont1
)

:cont1
set newtime=!currtime:~0,2!h!currtime:~3,2!m!currtime:~6,2!s

ren C:\final.txt Audit_Report."(%newdate%%newtime%)".txt
exit /b


Report •

#7
August 21, 2010 at 20:14:58
>>majority are requesting help with batch scripting.

its true, but most that ask here doesn't know about better alternatives. Unless they really specify that they cannot use any other tools including vbscript, and that they needed PURE batch commands, anything solution that solves the problem without too much hassle, is possible.

again, your batch will not take into account the different date formats from different environment. There are arcane ways to make it independent of locale, by some call to the registry, or doing something with debug.exe using just batch (cmd). But for this case, vbscript is the better alternative if one cannot use a better downloaded tool because using Date() is more intuitive than arcane batch workarounds.

GNU win32 packages | Gawk


Report •

#8
August 21, 2010 at 20:33:33
@Wahine

I'm still using xp and can't speak for latter systems, but the only foolproof way to get the date in batch is to write to the registry.


The date format can be typed into(as can the delimiter) for interesting results.


Have a look at this screenshot:

http://flickcabin.com/public/view/f...


Edit:

Forgot to mention that is excluding outside utilities...


Report •

#9
August 21, 2010 at 22:06:23
>> but the only foolproof way to get the date in batch is to write to the registry.

You did not mention one must be careful when playing with the registry in order not to corrupt the system.

GNU win32 packages | Gawk


Report •

#10
August 21, 2010 at 22:50:48
@ghostdog

That's true, but the bigger issue is that changing a system wide setting, even if for a very short period, is rather extreme to get something as simple as the date in a reliable format.


Report •

#11
August 22, 2010 at 01:42:36
Ghostdog - again, your batch will not take into account the different date formats from different environment.

Judago - The date format can be typed into(as can the delimiter) for interesting results.

Thanks for your comments. Setting the date format to ddd/ddd still allowed my original script to run without problem (that's response #2) so I'm speculating that VBS accesses the registry date format, tho' I don't know where that is stored. The short-date format can be found at HKCU>Control Panel>International sShortdate (url below). The strictly batch script uses the short-date format so will not run when that is ddd/ddd but will when combinations of day month year are found.

I wonder what the chances are of finding a date set to anything other than some combination of day month and year.

Let's not hijack the OP's thread any further, he seems well set with the help he's already been given.

http://a.imageshack.us/img826/5455/...

Thanks again


Report •


Ask Question