Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date and Time Based folder

Status
Not open for further replies.

dagoat10

Programmer
Jun 3, 2010
74
US
Hello everyone,

I have been trying to make a date and time based folder, but i can never seem to get the time part to work. Because when you create a date based folder you can only use that folder name once.

These are the instructions i followed from another site from Tech Republic:


And what I want is to try to do something like this:
Code:
CMD.EXE /C MD "%1\%%DATE%%TIME%%"

Can anyone find a solution for me please. If you need more info just ask. I am using Windows XP
 
A couple of ideas to try out-
Put a character like a dash or underscore between the repeating percent signs - perhaps you need double sets for each parameter - CMD.EXE /C MD "%1\%%DATE%%-%%TIME%%".
A simpler idea - any folder will get date/time created stamp, whatever its name. Make a two-line command call (batch file from the days of DOS) - create the folder with a sequential name, then immediately rename that folder to include the date & time...



Fred Wagner

 
Well, I tried the first idea and it was still the date, but without the year and no time. for the second idea, is that like a script I should write?
 
It depends on what environment you're programming in - you could make two calls to the O/S - one to create the folder with a temporary name, and the second one to rename the folder using the date/time variables. You might want to consider how this system is going to be maintained - how many folders will be created, how big they'll get, when they'll be deleted/archived, etc. Otherwise you're just building a delayed-action problem.
You could run into a problem in the total number of folders at a given level, etc. What will the system look like after it's been running for a week, a month, a year ?

Fred Wagner

 
I see your point, i did happed to solve the issue, but you have given me something to think about, thanks.
 
Glad to hear you solved it, dagoat10 - would you post your solution please? just for interest.

As with the "/" in the date (in the UK, anyway), the ":" in the time are both problems (i.e. not accepted) in filenames under NTFS/DOS.

The original link suggests changing the format in the regional and language settings, which is a bit clunky.

Still with those date/time format modifications in place this batch file code does what you wanted:

Code:
@echo off
::    "JustDT.bat" puts the current Datetime into the environment
::    Written under MSDOS 7 (Win95), should work on all recent versions.
::    The trick here is to use the first word of a command output
::    as the name of a batch file. 

::    First find the line starting with "Current" that ends with the time
::    A single CR/LF is sent to TIME with ECHO. to end the TIME command
::    ** now states "The current time is:hh-mm-ss.xx" a nonblank character will do like "T"
echo.|time|find /i "T" >cu##ent.bat
::    We now have "Current time is  1-45-59.82p" (without quotes) in CU##ENT.BAT
::    Since the first word is CURRENT, we need a command with that name ready

::    Now create a batch file named CURRENT.BAT **The.bat
::    CURRENT.BAT will set the TIME variable to it's third argument (the time)**now 4th argument
echo set time=%%4>The.bat
::    We now have "set time=%3" (without the quotes) in CURRENT.BAT **set time=%4 in The.bat

::    Now CALL our CU##ENT.BAT, which will in turn run CURRENT.BAT
call cu##ent.bat

::    Display the time we worked so hard to get
::    echo %TIME%

::    Now delete both temporary batch files
del cu??ent.bat > nul
del the.bat > nul
::    Now for the date - almost the same
@echo off
::    "JustDate.bat" puts the current date into the environment
::    Adapted from justtime.bat 

::    First find the line starting with "Current" that ends with the time
::    A single CR/LF is sent to DATE with ECHO. to end the DATE command
::    ** now states "The current date is:dd-mm-yyyy" a nonblank character will do like "c"
echo.|date|find /i "c" >cu##ent.bat
::    We now have "Current date is  19-05-2011" (without quotes) in CU##ENT.BAT
::    Since the first word is The, we need a command with that name ready

::    Now create a batch file named CURRENT.BAT **The.bat
::    CURRENT.BAT will set the DATE variable to it's third argument (the DATE)**now 4th argument
echo set date=%%4>The.bat
::    We now have "set date=%3" (without the quotes) in CURRENT.BAT **set date=%4 in The.bat

::    Now CALL our CU##ENT.BAT, which will in turn run CURRENT.BAT
call cu##ent.bat

::    Now delete all the temporary batch files
del cu??ent.bat > nul
del the.bat > nul

::   Putting it all together
::   Show what we have now got as date and time:
echo %DATE%_%TIME% 
::   make a new folder called folder
md folder
::   rename it to the Date-time we have just obtained
ren folder %DATE%_%TIME%
::   erase the DATE variable
set date=
::   erase the TIME variable
set time=

::    Originally from [URL unfurl="true"]http://www.ericphelps.com[/URL] 

::    **Modified by [URL unfurl="true"]http://www.fxxxingcomputers.co.uk[/URL] for Windows 7 cmd.exe

As mentioned the basic credit goes to Eric Phelps for the dos batch code for the getting the current time into the %TIME% variable. Things have changed slightly now so his original code throws up errors.
He has another file specifically for renaming a file with the date-time, but I did not fancy working out all the problems it may throw up. It is more elegant because it changes the formatting of the date and time output on-the-fly, rather than relying on changing the system formats. It is here:

 
a little simplified version:
Code:
@echo off
set zeit=%time%
for /F "tokens=1 delims=:" %%i in ("%zeit%") do set t1=%%i
for /F "tokens=2 delims=:" %%i in ("%zeit%") do set t2=%%i
for /F "tokens=3 delims=:" %%i in ("%zeit%") do set t3=%%i
for /F "tokens=1 delims=," %%i in ("%t3%") do set t4=%%i
set EndZeit=%t1%%t2%%t4%

set Tag=%date%
for /F "tokens=1 delims=." %%i in ("%Tag%") do set d1=%%i
for /F "tokens=2 delims=." %%i in ("%Tag%") do set d2=%%i
for /F "tokens=3 delims=." %%i in ("%Tag%") do set d3=%%i
set EndDag=%d1%%d2%%d3%
 
set EndFol=%EndDag%_%EndZeit%

md folder
ren folder %EndFol%

Only problem I see is with time set at 00:00:00 to 00:59:59, as the above handles the first 00 (%t1%) as just plain 0, which would leave a blank between the underscore and the preceding 0 ...

PS: last two lines where copied from FlyBoyTim's script above... ;-)

Ben
"If it works don't fix it! If it doesn't use a sledgehammer..."
How to ask a question, when posting them to a professional forum.
Only ask questions with yes/no answers if you want "yes" or "no"
 
Code:
@echo off
echo.|time|find /i "T" >cu##ent.bat
echo set time=%%4>The.bat
call cu##ent.bat
del cu??ent.bat > nul
del the.bat > nul
@echo off
echo.|date|find /i "c" >cu##ent.bat
echo set date=%%4>The.bat
call cu##ent.bat
del cu??ent.bat > nul
del the.bat > nul
md folder
ren folder %DATE%_%TIME%
set date=
set time=

Hey Ben - I simplified mine! (but now it's "double dutch" to me, too) :)

 
Punish me for all that redundant code! [rofl]
Code:
@echo off
echo.|time|find /i "T" >Th1.bat
echo set time=%%4>The.bat
call Th1.bat
del th?.bat > nul
echo.|date|find /i "c" >Th1.bat
echo set date=%%4>The.bat
call Th1.bat
del th?.bat > nul
md fol
ren fol %DATE%_%TIME%
set date=
set time=


 
To clarify:

Code:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

[b]C:\Windows\system32>echo |time|find /i "T"[/b]

gives the output: said:
The current time is: 11-41-51.39
Enter the new time: ECHO is on.
The system cannot accept the time entered.
Enter the new time:

Code:
[b]

C:\Windows\system32>echo |time|find /i "T">Th1.bat[/b]

C:\Windows\system32>echo set time=%%4>The.bat 
                                         (The.bat contains set time=%%4)
C:\Windows\system32>call th1.bat
                   (which now outputs:
                    The current time is: 11-55-24.76 )
(args input)         ^     ^     ^    ^       ^          
                     ^     ^     ^    ^       ^
input that runs The.bat    %1....%2...%3.....[b]%4[/b]

C:\Windows\system32>set time=%4 (which becomes 11-55-24.76)

It is exactly the same for the date environment variable - but would depend on the syntax of the output of set zeit= and set dag= commands

explains it better than I could


 
FlyboyTim,

close but no cigar on the codepage, it's German... ;-)

once I changed the T to a Z it partially worked...
but fol was not renamed...

your comment, (The.bat contains set time=%%4)... on my system THE.BAT contains set time=%4... strange strange...

I am going to have to try this on an English(GB or US) version of XP...

PS: this is actually quite educational, for me at least... I am going to attempt to get your script to work for the German codepage...

PSS: changing the delim value, from a semicolon to a dash, should work for the English version (pertaining to the time at least)...
possibly the same for the date, but here use the slash instead of the period...




Ben
"If it works don't fix it! If it doesn't use a sledgehammer..."
How to ask a question, when posting them to a professional forum.
Only ask questions with yes/no answers if you want "yes" or "no"
 
Thanks guys, with the quotes at the end of the last statement i got exactly what I wanted.
 
Apologies, Ben, I can't tell my Dutch from my Deutsch :(

However, - what's the output for the German

c:\>zeit

&

c:\>dag

commands - exactly, please?

In the regional and language control applet, in UK,

for the time command output default formatting, our long time format is HH:mm:ss.tt - our decimal separator is a "." - which is OK in a filename, but I need to reformat the ":" as "-"

your long time format is HH:mm:ss,tt ?

The decimal separator "," format in German is not acceptable in a filename, so the decimal format would also need changing!

Sorry :)

The link I gave has some international date/time processing that was supposed to be universal - but mostly referred to DOS 7 (i.e. Windows 98), and the cmd.exe of Windows 7 has several differences from the command.com available now or that of DOS in the past.

If you break down the output of the various lines as I did in the earlier post, and also use the "set" command to view the environment variables, while they are still present, it helps in debugging. Don't forget to destroy the changed environment variables after checking them, and all the folders called fol that get created by batch files that don't complete! Otherwise, things may not work, when they would otherwise.


 
Now I know what I'm doing ;) - your code with a few minor modifications works fine for UK:

Code:
DTZ.BAT  - modified from BigBadBen’s code

@echo off
set zeit=%time%
for /F "tokens=1 delims=:" %%i in ("%zeit%") do set t1=%%i
for /F "tokens=2 delims=:" %%i in ("%zeit%") do set t2=%%i
for /F "tokens=3 delims=:" %%i in ("%zeit%") do set t3=%%i
for /F "tokens=1 delims=." %%i in ("%t3%") do set t4=%%i
set zeit=%t1%%t2%%t4%
set tag=%date%
for /F "tokens=1 delims=/" %%i in ("%tag%") do set d1=%%i
for /F "tokens=2 delims=/" %%i in ("%tag%") do set d2=%%i
for /F "tokens=3 delims=/" %%i in ("%tag%") do set d3=%%i
set tag=%d1%%d2%%d3%
md fol
ren fol %tag%_%zeit%
set zeit=
set tag=


Code:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

 Directory of c:\Users\Administrator\Documents\Folder

20/05/2011  19:30    <DIR>          .
20/05/2011  19:30    <DIR>          ..
20/05/2011  19:30               555 dtz.bat
               1 File(s)            555 bytes
               2 Dir(s)  15,836,893,184 bytes free

c:\Users\Administrator\Documents\Folder>dtz

c:\Users\Administrator\Documents\Folder>dir
 Volume in drive C is win7boot
 Volume Serial Number is 0CC0-D329

 Directory of c:\Users\Administrator\Documents\Folder

20/05/2011  19:31    <DIR>          .
20/05/2011  19:31    <DIR>          ..
20/05/2011  19:31    <DIR>          20052011_193114
20/05/2011  19:30               555 dtz.bat
               1 File(s)            555 bytes
               3 Dir(s)  15,836,893,184 bytes free

c:\Users\Administrator\Documents\Folder>

[rofl]

 
Flyboytim,

1. there is no need to appologize... ;-)

2. the output:

zeit = 10:53:24,24

Tag = 22.05.2011

but you already noticed that ;-)

3. yes, you are correct that the comma separator would not work in a filename, thus I had the 4th token statement work on %T3%, instead of just using it...

but again you did notice that... ;-)
(and to be quite honest, I forgot the period as a separator at the end of the TIME variable, in the English codepage)

4. haven't had time to dabble with your code, to make it work in German, yet... still at it though... ;-)


Ben
"If it works don't fix it! If it doesn't use a sledgehammer..."
How to ask a question, when posting them to a professional forum.
Only ask questions with yes/no answers if you want "yes" or "no"
 
Well, that was a bit of fun, Ben - we both learned a bit, I hope. By the way - check your environment variables with the "set" command at the command prompt:

t1,t2,t3,t4,d1,d2,d3,zeit,EndZeit,Tag,EndDag,EndFol, are probably all there taking up a few bytes of your environment space!

 
Flyboytim,

yes it has been fun and educational...

and I'll keep on modifying the code, in such a way that it would not matter which codepage or language it is in/set to...

Code:
reg query "HKCU\Control Panel\International" /s
would be the start, now I have just to figure out how to search within a text file etc...

PS: I am still at the beginnings of batch programming (greenhorn)...


Ben
"If it works don't fix it! If it doesn't use a sledgehammer..."
How to ask a question, when posting them to a professional forum.
Only ask questions with yes/no answers if you want "yes" or "no"
 
Have you tried this?

e.g: set Ctime=DATE_[%date::=-%_TIME_[%time::=-%]

MKDIR %Ctime%

when declaring a variable in batch the : towards the end of the var allows a declared char to be replaced with another like this:

set lol="lols"
set lol=%lol:s=z%
echo %lol%
lolz

hope it helps :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top