|
Answer» Hi,
I am going to use batch FILE to rename group of folders with timestamp so that I can perform backup and hosue-keeping tasks.
My current regional and language setting for date/time is dd/MM/yyyy and h:mm:ss respectively.
Therfore the name of folder would be xxxx_dd-MM-yyyy[hh.mm']
Here is the code:
@echo off for /f "tokens=1-3 delims=/- " %%a in ("%date%") do set XDate=%%a-%%b-%%c for /f "tokens=1-3 delims=: " %%d in ("%time%") do set Xtime=[%%d.%%e'] set TimeStamp=%XDate%%XTime% mkdir "%CD%\xxxx_%TimeStamp%" move/y test*.TXT "%CD%\xxxx_%TimeStamp%" > nul
However, if I changed to different date/time format, the name would be changed accordingly. for example: yyyy/MM/dd will create a folder name with format of xxxx_yyyy-mm-dd[hh.mm']
Is it possible to create a timstamp as dd-MM-yyyy[hh.mm'] from different date/time setting ?
Thanks, Thomaswell if you are asking what i think you are, you would take Code: [Select]for /f "tokens=1-3 delims=/- " %%a in ("%date%") do set XDate=%%a-%%b-%%c and rearrange %%a %%b %%c accordingly %%a = day %%b = month %%c = year
correct me if im not understanding your question?Sorry, it is not I want.
Actually, my batch code will be used in other PC clients but I don't know what Date/Time setting they are using.
Therefore, I'm afraid that the generated Date/Time format would not be what I expected.
I think using system variables ("%Date%") and ("%Time%") only get the Date/Time format according to the setting in Regional and Language setting.
I wonder whether there is a way to get the fields of yyyy, MMM, and dd from system clock so that I can format to a proper format.
Thanks, Thomas ok, with an intense 10 minutes of searching google, i have came upon this: which at the end, gives out the year(yyyy) month(mm) day(dd) and day of week(dow) Code: [Select] @echo off if %1/==:/ goto %2 if NOT %1/==/?/ goto Begin for %%C in (echo. goto:End) do %%C :Begin -------------------------------------------------------------- echo. | date | FIND "(mm" > NUL if NOT errorlevel 1 %0 : %OS%Parse MM DD %0 : %OS%Parse DD MM :Windows_NTParse ---------------------------------------------------- for /F "tokens=1-4 delims=/.- " %%A in ('date /T') do if %%D!==! ( set %3=%%A&set %4=%%B&set YYYY=%%C ) else ( set DOW=%%A&set %3=%%B&set %4=%%C&set YYYY=%%D) goto End :Parse -------------------------------------------------------------- for %%C in (md cd) do %%C @[emailprotected] echo @prompt set _D=$D$_> ~tmp1.bat %COMSPEC% /e:2048 /c ~tmp1.bat > ~tmp2.bat call ~tmp2 echo %_D% | FIND "/" > NUL if NOT errorlevel 1 goto Slash lfnfor on > "%_D%.-" ren "%_D%.-" "??? ?? ?? ????" for %%F in ("??? ?? ?? ????") do set _D=%%F lfnfor off :Slash echo set DOW=%%%3%%>~tmp1.bat for %%S in ("%3=%%%4%%" "%4=%%YYYY%%" "YYYY=%%1") do echo set %%S>>~tmp1.bat for %%S in (%_D%) do call ~tmp1 %%S echo %_D% | FIND "/" > NUL if errorlevel 1 goto Cleanup echo @prompt set %4=$%%%4%%$_set YYYY=$%YYYY%$_ > ~tmp1.bat %COMSPEC% /e:2048 /c ~tmp1.bat > ~tmp2.bat call ~tmp2 :Cleanup for %%C in ("set _D=" cd.. "deltree /y @[emailprotected] > NUL") do %%C :End ---------------------------------------------------------------- echo YYYY=%YYYY% MM=%MM% DD=%DD% DOW=%DOW% pauseYou can get the day, month and year in a standard format, independent of the local settings, in Visual Basic Script. It is quite easy to do this from a batch file. A one-line VBS script can be created "on-the-fly", used to get the day, month and year, and then deleted. the VBS Day(Date) and Month(Date) FUNCTIONS return a single figure if the day or month is before the 10th so we can add a leading ZERO in these cases to get the dd mm and yyyy format.
Code: [Select]@echo off echo Wscript.echo eval(WScript.Arguments(0))>"%temp%\evaluate.vbs" For /f %%D in ( ' cscript //nologo "%temp%\evaluate.vbs" "day(date)" ' ) do set /a d=%%D For /f %%M in ( ' cscript //nologo "%temp%\evaluate.vbs" "month(date)" ' ) do set /a m=%%M For /f %%Y in ( ' cscript //nologo "%temp%\evaluate.vbs" "year(date)" ' ) do set yyyy=%%Y del "%temp%\evaluate.vbs" if %d% LSS 10 (set dd=0%d%) else (set dd=%d%) if %m% LSS 10 (set mm=0%m%) else (set mm=%m%)
echo dd %dd% echo mm %mm% echo yyyy %yyyy% echo dd-mm-yyyy %dd%-%mm%-%yyyy% echo dd/mm/yyyy %dd%/%mm%/%yyyy% echo yyyymmdd %yyyy%%mm%%dd%Thank you for your suggestions.
Chris(BatchFileBasics) codes still does not fit my requirement. I had tested with different Date/Time setting but generated different format. for example: it gives YYYY=Sun MM=09 DD=08 DOW=16 for d/MM/yy but it gives YYYY=16 MM=08 DD=2009 DOW= for yyyy/MM/dd
Any ideal?
Salmon's code works FINE, but my clients' working environment does not allow to use VBscripts.
Thanks, Thomas
|