6

At work every morning I have to create multiple file folders (using a YYYYMMDD date format as the file folder name) in different directories across our network for various departments. This is a real pain and time waster, and I would like to automate the process. So my question is:

Does anyone know how I can write a script that uses the current system date in YYYYMMDD format, and creates multiple folders in different network directories with each folder named as the date in YYYYMMDD format?

Thanks in advance for your answers.

3
  • 1
    pet peeve: the Windows command prompt is not DOS. DOS is a 16-bit operating system that is mostly dead. I re-tagged the question to reflect that.
    – RBerteig
    Sep 11, 2009 at 8:17
  • 1
    A pilot is flying a plane and is lost in fog. He is low on fuel and his passengers are nervous. At last he sees a tall building with one guy working on the 5th floor. The pilot banks and shouts through his open window: "Hey, where am I?". The office worker replies: "You're in an airplane.". The answer the guy in the building gave the pilot was 100% correct but absolutely useless. While you are absolutely right that command prompt is not DOS, it is what most people understand. My 2 cents: we should focus on making things accessible/searcheable - even if they are not 100% correctly labeled.
    – Vinayak
    Sep 11, 2009 at 13:26
  • 1
    @Vinayak If you actually had a question about DOS, would you enjoy sifting through topics that were 95% about Windows?
    – coneslayer
    Jul 23, 2010 at 19:38

3 Answers 3

9

Create a batch file that looks like this:

@echo off
for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set yyyymmdd=%%k%%j%%i
echo Date: %yyyymmdd%

mkdir \\server1\share1\subdir1\%yyyymmdd%
mkdir \\server1\share2\subdir2\%yyyymmdd%
mkdir \\server2\share3\subdir3\%yyyymmdd%
...

Warning: the format of the date (yyyymmdd=%%k%%j%%i) depends on your regional settings. Because I use the French date format (dd/mm/yyyy), I have to use "%%k%%j%%i" as the format (%%i = day, %%j = month, %%j = year).

If your regional settings are set to US style (mm/dd/yyyy), you should use "%%k%%i%%j" (%%i = month, %%j = day, %%j = year).


If you want to include the time as well, use this:

@echo off
for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set yyyymmdd=%%k%%j%%i
echo Date: %yyyymmdd%
for /F "tokens=1-3 delims=: " %%i in ('echo %time%') do set hhmmss=%%i%%j%%k
echo Time: %hhmmss%

mkdir \\server1\share1\subdir1\%yyyymmdd%%hhmmss%

The date is stored in the variable %yyyymmdd%, the time in %hhmmss% . Same remark as above for the date, not applicable for the time.

You could use a separator between the date and time: %yyyymmdd%_%hhmmss% for instance.

2
  • could you show an example including time as well? Oct 13, 2010 at 15:06
  • 1
    @marked: updated my answer with an example including time
    – Snark
    Oct 14, 2010 at 9:08
0

Another, uglier but much more flexible way, is to generate a separate batch file for every directory that needs to be created, that (a) creates the directory and (b) renames the next batch file that needs to be executed to a previously selected common name. You just run a batch file with that common name every day

0

Try these options:

set name=%date%
set name2=%name:~6,4%-%name:~3,2%-%name:~0,2%
set tm=%time%
set name3=%tm:~0,2%-%tm:~3,2%
set finname=%name2%_%name3%
mkdir \\Server\Share1\Subfolder1\%finname%
1
  • In case I want to do the same for all working days in the month (e.g. say for month of May 2018, i want to create all the folders for 01st May, then 02nd May and so on and so forth - 20180501, 20180502, 20180503....). then how do i iterate it ?, any suggestions are welcome.
    – tush1r
    May 31, 2018 at 12:03

You must log in to answer this question.