Using the Date to name Files, Folders or Directories
There are so many requests for a way to rearrange the date string in the alt.msdos.batch newsgroup for use in naming files and such that in self defense I have worked to fully address this issue once and for all. I had previously included a method here that didn't work for all country codes, but was independent of the language used in the DOS/Win 9x version. If you want that version it is still available, without comment, as PDate.bat. However, I have recently developed another approach which I believe is as close to being univeral as I will ever be able to accomplish.

:: UniPDate.bat - A universal date parsing routine. Returns the date in :: YYYYMMDD format anywhere in the world. Uses no external :: utilities. Valid for DOS 5.0 thru Win 98 (not Win NT). :: Tom Lavedas <lavedas@deltaelectronics.com> :: http:www.pressroom.com/~tglbatch/ @echo %dbgp% off %2 if [%1]==[] %0 %/ Default Return Var Name is /% DateName :: > {a}.bat echo @prompt set {Y}=$_for$q%%%%%%%%v$qin$q($d)$qdo$qset$q{T}$q%%%%%%%%v > {b}.bat %comspec% /e:2048 /c {a}.bat > {a}. call {b}.bat > {a}.bat echo %0 %1 goto:2nd %%2 echo %{T}%y | del {a} /p | %comspec% /e:2048/k > nul set {T}=> nul for %%v in ({b}.bat del goto:End) do call %%v {?}.bat :2nd if %3==(Y/N)?y goto Done for %%v in (- / //) do if %3==(Y/N)?%%v goto Delim if %3==(Y/N)?, goto Delim for %%v in (0 1 2 3 4 5 6 7 8 9) do if %3==(Y/N)?%%v set {Y}=%{Y}%%%v goto End :Done echo. | date 13/1/1998 > {a}.bat copy {a}.bat+,, . > nul if exist {a}.bat echo set %1=%{Y}%%{D}%%{M}%> {b}.bat %; Europe, etc ;% if not exist {a}.bat echo set %1=%{Y}%%{M}%%{D}%>>{b}.bat %; US ;% exit :Delim set {M}=%{D}% set {D}=%{Y}% set {Y}= :End
Unfortunately, it is a complicated procedure and I just don't have time to offer an explanation right now. You're welcome to use it as is, or dope out its inner workings on your own. With a bit of tinkering the same approach can be used to parse the TIME string. I have done it, but don't have time to incorporate it here now.

To use UniPDate, simply CALL it from your procedure with or without the name of a variable to contain the result as the single command line parameter. If no name is given, the output is returned in the variable DateName. Note that the date is returned in YYYYMMDD format whether the standard format for your system configuration is MM-DD-YYYY, DD/MM/YYYY, DD-MM-YYYY or even DD,MM,YYYY. It can easily be altered to return any part or each part of the date in separate variables. This exercise is left to the student.

I recently had occasion to revisit this procedure and significantly revise and compress it. However, this particular version does not incorporate the rearrangement of the parts of the date. To do that, replace the underscore chacrater near the end of the code with a space or other DOS delimiter character and use the approach illustrated in UniPDate to reassemble the pieces in the manner desired.

Obviously, though much shorter, the procedure is also much more difficult to understand (and to explain. Sorry, I can't really take the time to unpack and describe all of its features and techniques.

You do need to know that the procedure takes two optional arguments, a variable name to store the result into (default is DateName, like UniPDate) and a second argument of almost any non-delimiter, which when present flags the desire to replace the normal hyphen or slash in the date string with the underscore character.


:: UniDateB.bat - A way to store today's date into the environment :: :: Tom lavedas <lavedas@pressroom.com> :: http://www.pressroom.com/~tglbatch @echo %dbgp% off %3 if '%1'=='' %0 DateName ) %3 if '%2'=='' %0 %1 ) > {A} %3 echo @prompt if '%%%%2==' %%%%0=$D$_echo %%%%2.y$g{C}.bat$_> {A}.bat %3 %comspec% /e:2048 /c {A} > {B}.bat %3 for %%v in (call del) do %%v {B}.bat %3 echo %0 %1 %2 :: %%2 > {A}.bat %3 del {A}.bat /p < {C}.bat | %3 %comspec% /e:2048 /k> nul for %%v in (echo exit) do if %4"==(Y/N)?." %%v set %1=%{T}%>> {B}.bat for %%v in (%2 / // -) do if %4"==(Y/N)?%%v" set {T}=%{T}%_ for %%v in (0 1 2 3 4 5 6 7 8 9) do if %4"==(Y/N)?%%v" set {T}=%{T}%%%v %3 for %%v in ({B}.bat del) do call %%v {?}.bat
And to be complete, here is a similar Time parsing routine.

:: UniTimeB.bat - A way to parse and store the current time string into the environment :: Uses internal commands only. For DOS and Win95. :: Tom Lavedas <lavedas@pressroom.com> :: http://www.pressroom.com/~tglbatch/ @echo %dbgp% off %3 if '%1'=='' %0 TimeName . % Default variable name % > {a}.bat %3 if '%2'=='' %0 %1 . % Default format - no fractions % > {c} %3 echo @prompt $t%2y > {b}.bat %3 echo %0 %1 %2 :: %%2 > {c}.bat %3 %comspec%/e:2048/c{b}.bat |%3 del {c} /p|%3 %comspec%/e:2048/k>nul for %%v in (cho xit) do if %4"==(Y/N)?%2" e%%v set %1=%{T}%>> {a}.bat for %%v in (%2 : .) do if %4"==(Y/N)?%%v" set {T}=%{T}%_ for %%v in (0 1 2 3 4 5 6 7 8 9) do if %4"==(Y/N)?%%v" set {T}=%{T}%%%v %3 for %%v in ({a}.bat del) do call %%v {?}.bat



Top | General Notes | Homepage