bayoff
asked on
Create subdirectory name based on date and time
Hi... For some reason the script below is failing after the assignment of DateVar... What is wrong with the assignment statement?
REM ----------------- this creates a date string
for /f "tokens=2-4 delims=/ " %%a in ('Date /T') do set DateVar1=d%%c%%a%%b
for /f "tokens=1-3 delims=: " %%a in ('TIME /T') do set TimeVar1=t%%a%%b%%c%
Set DateVar = %DateVar1%\%TimeVar1%
echo MD c:\%VarVar%
REM ----------------- this determines if the directory exists before creating one
IF NOT EXIST c:\%DateVar% MD c:\%DateVar%
REM ----------------- this creates a date string
for /f "tokens=2-4 delims=/ " %%a in ('Date /T') do set DateVar1=d%%c%%a%%b
for /f "tokens=1-3 delims=: " %%a in ('TIME /T') do set TimeVar1=t%%a%%b%%c%
Set DateVar = %DateVar1%\%TimeVar1%
echo MD c:\%VarVar%
REM ----------------- this determines if the directory exists before creating one
IF NOT EXIST c:\%DateVar% MD c:\%DateVar%
Well, the assignment works just fine, but the spaces between the = and %DateVar1% will cause MD to misbehave.
and the IF it's on.
bayoff,
You say the script is "failing after the assignment of DateVar"...
You mean the %DateVar% variable is being set correctly
but the subsequent commands fail?
The Echo command will merely echo the string following it to the screen.
In the command you posted:
echo MD c:\%VarVar%
...the portion " MD c:\%VarVar% " would be echoed,
but at that point there is no %VarVar% variable set,
so the command would merely echo " MD c:\ " to the screen.
But that line isn't needed if you're just trying to create a directory
having the same name as the %DateVar% variable...
Although your commands are specific to NT and
wouldn't work under MS-DOS or Windows 9.x
you may encounter problems attempting to test for a directory
using the IF command in that manner.
You typically can't directly test for the existence of a directory using IF
but you can test for a device (such as the NUL device) that would exist in a directory.
The IF command is used there to mask the "Directory already exists" output...
Otherwise you can just use: MD C:\%DateVar%
But if that's where you're having problems in the script,
and you want a directory named with the Date and Time
separated by the back slash character,
change those two lines to:
SET DateVar=%DateVar1%\%TimeVa
IF NOT EXIST C:\%DateVar%\NUL MD C:\%DateVar%
Post back if that's not what you're getting
or if either of the other two variables aren't being set...
ASKER
Thanks all for answering - and the very good comments...
By the way, how can I change TimeVar1 to be of the format: ttmmssam or pm, i.e., 0954am
By the way, how can I change TimeVar1 to be of the format: ttmmssam or pm, i.e., 0954am
ASKER
I haven't heard back from anybody... I would really appreciate any help on this question.
I have some comments that may help,
but can you provide an example of
the output of the 'date/t' command
as it's formatted on your system?
Also the exact format of the TimeVar1 variable
as it's value is currently being set
and specifically how you want it formatted.
-i.e., You specified "ttmmssam" but is that "hhmmssam"?
Is the 'tt' indicating the am or pm and is it returned as just an a or p?
In that previous post i meant to ask you
for the specific format of the 'time /t' command
as it's generated on your system,
not the 'date /t' command...
ASKER
Sure...
The script is producing something like:
d20020221/t901a
I would like it to pad the time values so the length of the output is always 6
d20020221/t0901a
and if I add on seconds, 8
d20020221/t090159a
The script is producing something like:
d20020221/t901a
I would like it to pad the time values so the length of the output is always 6
d20020221/t0901a
and if I add on seconds, 8
d20020221/t090159a
ASKER
Sorry, I didn't see a previous message... Having it read am would be better than just a; same goes for pm... Of course, this would also change the length I just specified in the last message.
There were several problems in your original code, including a trailing space at the end of the DATE /T line. This fixes you up with a leading zero for times that are <10:00am or 10:00pm.
The trick is to note that the TIME /T output starts with a space for those times. This code uses the IF command to detect that space and if found, it inserts a leading 0 (zero).
REM -- Creates a directory named with the current date
REM -- and a subdirectory named with the current time
SET RootDirVar=c:\temp
FOR /F "TOKENS=2-4 DELIMS=/ " %%a IN ('date /t') DO SET DateVar=d%%c%%a%%b
for /f "DELIMS=x" %%a in ('TIME /T') DO SET TempVar=%%am
for /f "TOKENS=1-3 DELIMS=: " %%a IN ("%TempVar%") DO SET TimeVar=%%a%%b%%c%m
if /i "%TempVar%" LSS "1" FOR /F "TOKENS=1-3 DELIMS=: " %%a IN ("%TempVar%") DO SET TimeVar=0%%a%%b%%c
SET DirNameVar=%RootDirVar%\%D ateVar%\%T imeVar%
IF NOT EXIST %DirNameVar% MD %DirNameVar%
REM -- end of batch file code
-- Dan
The trick is to note that the TIME /T output starts with a space for those times. This code uses the IF command to detect that space and if found, it inserts a leading 0 (zero).
REM -- Creates a directory named with the current date
REM -- and a subdirectory named with the current time
SET RootDirVar=c:\temp
FOR /F "TOKENS=2-4 DELIMS=/ " %%a IN ('date /t') DO SET DateVar=d%%c%%a%%b
for /f "DELIMS=x" %%a in ('TIME /T') DO SET TempVar=%%am
for /f "TOKENS=1-3 DELIMS=: " %%a IN ("%TempVar%") DO SET TimeVar=%%a%%b%%c%m
if /i "%TempVar%" LSS "1" FOR /F "TOKENS=1-3 DELIMS=: " %%a IN ("%TempVar%") DO SET TimeVar=0%%a%%b%%c
SET DirNameVar=%RootDirVar%\%D
IF NOT EXIST %DirNameVar% MD %DirNameVar%
REM -- end of batch file code
-- Dan
oops, minor error. Remove the 'm' at the end of line 6. Make it read:
for /f "TOKENS=1-3 DELIMS=: " %%a IN ("%TempVar%") DO SET TimeVar=%%a%%b%%c%m
Sorry.
-- Dan
for /f "TOKENS=1-3 DELIMS=: " %%a IN ("%TempVar%") DO SET TimeVar=%%a%%b%%c%m
Sorry.
-- Dan
oops, minor typo in my typo fix. Make it read:
for /f "TOKENS=1-3 DELIMS=: " %%a IN ("%TempVar%") DO SET TimeVar=%%a%%b%%c%
-- Dan
for /f "TOKENS=1-3 DELIMS=: " %%a IN ("%TempVar%") DO SET TimeVar=%%a%%b%%c%
-- Dan
ASKER
Thanks for noticing the oversight... Just wondered about the last issue about how to pad with zeros the directory name to make sure it would always be 9 characters, i.e., t092959am
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry... I read it too fast.. Nice job.