Reputation: 217
I have written a batch script which creates a folder. The path is given as a input parameter and the folder name is today' s date.
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set year=%%c
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set month=%%a
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set day=%%b
set TODAY=%day%_%month%_%year%
mkdir %1% %TODAY%
and the output I get is as follows:
arvind_test.bat C:\Users\M1015807\Desktop\logs\ C:\Users\M1015807\Desktop\logs\
C:\Users\M1015807\Desktop\zip\test>for /F "tokens=2-4 delims=/ " %a in ('date /T') do set year=%c
C:\Users\M1015807\Desktop\zip\test>set year=2013
C:\Users\M1015807\Desktop\zip\test>for /F "tokens=2-4 delims=/ " %a in ('date /T') do set month=%a
C:\Users\M1015807\Desktop\zip\test>set month=04
C:\Users\M1015807\Desktop\zip\test>for /F "tokens=2-4 delims=/ " %a in ('date /T') do set day=%b
C:\Users\M1015807\Desktop\zip\test>set day=16
C:\Users\M1015807\Desktop\zip\test>set TODAY=16_04_2013
C:\Users\M1015807\Desktop\zip\test>mkdir C:\Users\M1015807\Desktop\logs\TODAY
A subdirectory or file C:\Users\M1015807\Desktop\logs\TODAY already exists.
>> was unexpected at this time.
Can any one help please..
Upvotes: 1
Views: 621
Reputation: 67256
A couple observations about your code, and an important point at end.
%date%
variable show the same information of date /t
command, but the former is more efficient. The execution of a command in FOR requires a copy of cmd.exe and the creation of a temporary file. The replacement of a variable value is immediate:
for /f "tokens=2-4 delims=/ " %%a in ("%date%") do ...
tokens=2-4
provide access to three pieces of information in the same FOR command, so it is not necessary to execute it three times:
for /f "tokens=2-4 delims=/ " %%a in ("%date%") do (
set year=%%c
set month=%%a
set day=%%b
)
If you are using three previous variables with the sole purpose to assemble TODAY variable, then the variables are not necessary:
for /f "tokens=2-4 delims=/ " %%a in ("%date%") do set TODAY=%%b_%%a_%%c
If you are using TODAY variable with the sole purpose to create the folder, then it is not necessary either:
for /f "tokens=2-4 delims=/ " %%a in ("%date%") do mkdir %1 %%b_%%a_%%c
The important point now:
mkdir
command creates separated folders if they are separated by space. This command creates two folders: mkdir one two
. You should eliminate the space between the path and the folder name: mkdir %1%%b_%%a_%%c
. However, if the path given in first parameter may contain spaces, it must be enclosed in quotes: arvind_test.bat "C:\Users\Joe Doe\M1015807\Desktop\logs"
, but the quotes must be removed from the parameter this way: %~1
AND the complete folder name must be enclosed in quotes:
for /f "tokens=2-4 delims=/ " %%a in ("%date%") do mkdir "%~1%%b_%%a_%%c"
Upvotes: 0
Reputation: 882596
The numeric %
variables aren't supposed to be surrounded by %
, they just have a leading %
. The expression:
mkdir %1% %TODAY%
has the following elements:
mkdir
;%1
(note, not using the following %
symbol);% %
(an empty string most likely);TODAY
); and%
at the end (which, from memory, also gives you an empty string).You can see this in the following script:
@echo off
set TODAY=blah
echo mkdir %1% %TODAY%
which, if you call it as myscript.cmd hello
, gives you:
mkdir helloTODAY
Changing the script to:
@echo off
set TODAY=blah
echo mkdir %1 %TODAY%
(removing the %
immediately after the 1
) gives you more like what you want (although your actual code probably has a \
separating the path and directory, rather than a space as per your example):
mkdir hello blah
Upvotes: 3