-1

I want to create a batch file which when clicked will create a folder with the name 12012016.

I tried with the command

mkdir "E:\Meru\Work\Trace Reports\%date:~6,4%%date:~3,2%%date:~0,2%

But its creates with the name 20160112.

Please help

Hackoo
  • 18,337
  • 3
  • 40
  • 70
meru
  • 47
  • 1
  • 5

3 Answers3

0

This question implies that you have not tried to understand what is going on with this command...

Split in part: mkdir will create a directory with the name you have given.

The name you have given is build together using a fixxed string, you decided to use E:\Meru\Work\Trace Reports\ and three substrings from the system variable %date%.

Substring in batch works like this: %variable_name:~last character NOT to use,number of characters you need%. In your case it takes the year first, then the month and at last the day. You would just simply change the parts from %date:~6,4%%date:~3,2%%date:~0,2% to %date:~3,2%%date:~0,2%%date:~6,4%.

Notice!

The variable %date% has a different value based on the system settings for the time format. An alternative is the command wmic os get localdatetime which I covered in another answer here that will always have the same output format no matter what the settings are.

Community
  • 1
  • 1
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
  • can you share me the code to get the name as 20160112 with wmic os get localdatetime ? – meru Dec 01 '16 at 06:49
  • No I will not! This is no free code writing service and it will not harm if you put a bit of brain into that yourself... I gave you a link to another answer where the usage of the above command is explained and I gave an explanation of how to get substrings. If you would put a little bit of effort into this, you can be done in around 15 minutes. As I see you are around with 0 badges so you have no even read the [tour page] to have a look on how this site works! – geisterfurz007 Dec 01 '16 at 06:54
  • I am sorry but i dont see a substring command. Not sure functions of & and % signs in dos. – meru Dec 01 '16 at 06:59
  • "`Substring in batch works like this: %variable_name:~last character NOT to use,number of characters you need%`." It is in my answer... Start of the 4th paragraph. `%` work as a variable sign basically -> `variable` is a string and `%variable%` is a variable. I do not know why you do need `&` now. Another thing: This is NOT DOS! A lot of people seem to be misunderstanding this. DOS means older operating systems like MS-DOS. This is either cmd.exe or batch whatever you want to call it... – geisterfurz007 Dec 01 '16 at 07:04
0

If you have read this ==> Windows batch file redirect output to logfile with date/time

You can be able to do like this one :

@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
echo stamp: "%stamp%"
pause
echo datestamp: "%datestamp%"
pause
echo timestamp: "%timestamp%"
pause
set MyDateVar=%MM%%DD%%YYYY%
echo My desired Variable Date to use is : %MyDateVar%
pause
mkdir "E:\Meru\Work\Trace Reports\%MyDateVar%"
pause
Community
  • 1
  • 1
Hackoo
  • 18,337
  • 3
  • 40
  • 70
0

Because several hours have now elapsed:

mkdir "E:\Meru\Work\Trace Reports\%date:~0,2%%date:~3,2%%date:~6,4%"
Compo
  • 36,585
  • 5
  • 27
  • 39