mkdir in windows multiple path in single command

35,237

Solution 1

Add the line:

setlocal enableextensions

just after the @echo off line of the batch file. That will enable mkdir to create any intermediate directories.

An excerpt of output from help mkdir:

If Command Extensions are enabled MKDIR changes as follows:

MKDIR creates any intermediate directories in the path, if needed.
For example, assume \a does not exist then:

    mkdir \a\b\c\d

is the same as:

    mkdir \a
    chdir \a
    mkdir b
    chdir b
    mkdir c
    chdir c
    mkdir d

which is what you would have to type if extensions were disabled.

Solution 2

Put quote marks around the paths.

mkdir "%foo%\%bar%\%somename%"

The problem is that the names have spaces in them, and mkdir is interpreting them as two arguments instead of a single name. The quote marks will force it to interpret everything as a single path.

Solution 3

John Deters has nailed the problem - but I'd suggest

set "path_sourcepst4=My Documents\Outlook"
mkdir "%path_backup%\%username%\PST-%date:~10,4%-%date:~7,2%-%date:~4,2%\%path_sourcepst4%"

Using the set "var=string" format will ensure that the value set into the variable does not include any stray (and largely invisible) trailing spaces on the line. You only need to be caught out by that one once... It also reduces the number of " being resolved.

And why not set a variable called say yyyymmdd to %date:~10,4%-%date:~7,2%-%date:~4,2% so that that string isn't repeated?

Solution 4

@echo off
set "var=string"
set "today=%date:~10,4%-%date:~7,2%-%date:~4,2%"
set "path_backup=\\SGSINWPDFS01v\SG\OTHERS\IT\OTHERS\WORKSTATIONS\SCHEDULE"
set "path_sourcepst01=AppData\Local\Microsoft\Outlook"
set "path_sourcepst02=Desktop"
set "path_sourcepst03=My Documents\PST"
set "path_sourcepst04=My Documents\Outlook"
set "path_sourcepst05=My Documents\Outlook Files"
subst b: "%path_backup%"
mkdir "%path_backup%\%username%\%today%"
mkdir "%path_backup%\%username%\%today%\PST"
mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst01%"
mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst02%"
mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst03%"
mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst04%"
mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst05%"
subst b: /D

I found the issue, it seem on network drive can't be created multiple level subfolders. So map to local drive solve the issue.

Share:
35,237
m.k.frenky
Author by

m.k.frenky

Updated on January 21, 2020

Comments

  • m.k.frenky
    m.k.frenky over 4 years
    @echo off
    set "var=string"
    set "today=%date:~10,4%-%date:~7,2%-%date:~4,2%"
    set "path_backup=\\SGSINWPDFS01v\SG\OTHERS\IT\OTHERS\WORKSTATIONS\SCHEDULE"
    set "path_sourcepst01=AppData\Local\Microsoft\Outlook"
    set "path_sourcepst02=Desktop"
    set "path_sourcepst03=My Documents\PST"
    set "path_sourcepst04=My Documents\Outlook"
    set "path_sourcepst05=My Documents\Outlook Files"
    mkdir "%path_backup%\%username%\%today%"
    mkdir "%path_backup%\%username%\%today%\PST"
    mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst01%"
    mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst02%"
    mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst03%"
    mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst04%"
    mkdir "%path_backup%\%username%\%today%\PST\%path_sourcepst05%"
    

    I modified the scripts as follow above. I still the system still can't create the folder at sourcepst01, 03, 04 and 05. The sourcepst02 is working fine.

    It seem I can't MKDIR whole path, the system confused and must do one by one. Am I missing something here?

  • m.k.frenky
    m.k.frenky over 10 years
    I added the new variables: @echo off set "var=string" set "today=%date:~10,4%-%date:~7,2%-%date:~4,2%" But still doesn't work on the second path after %today%
  • Magoo
    Magoo over 10 years
    set "var=string" is a general statement-format. set "today=%date:~10,4%-%date:~7,2%-%date:~4,2%" is a specific instance of that format where var is today and string is %date:~10,4%-%date:~7,2%-%date:~4,2% which means "the contents of date, from position 10 for 4 characters"-"the contents of date, from position 7 for 2 characters"-"the contents of date, from position 4 for 2 characters". (the first character is in position 0). This is assigned to the variable today. The enclosing quotes ensure any trailing spaces on the line are not included in the value assigned.
  • Magoo
    Magoo over 10 years
    Michael Burr's response makes a lot of sense. In a tiny minority of cases, someone has deliberately turned off the enhanced faciities incorporated in CMD since 1996. By default, those extensions are enabled and it's normal to make the assumption that extensions ARE enabled.
  • foxidrive
    foxidrive over 10 years
    Yes, it does. Clarify the error message you receive if it doesn't work for you,
  • Michael Burr
    Michael Burr over 10 years
    I wouldn't think that that would matter, but I can never be sure of all the quirks in batch file commands. Also, as Magoo mentions in another comment, command extensions are normally enabled by default. So if setlocal enableextensions ends up helping, you might want to read the output of help cmd to see how it can be disabled and fix that problem.
  • Michael Burr
    Michael Burr over 10 years
    I haven't heard of an 8 level directory limit, but maybe that depends on the network? The pathname looks like it should be well under 260 characters.
  • Michael Burr
    Michael Burr over 10 years
    What kind of server is the network share on (if you know)? I can create multiple directories with a single mkdir on a UNC path here, so I wonder if it's the network server that's causing the glitch?
  • foxidrive
    foxidrive over 10 years
    Maybe it was a FAT32 limit? I just tried it in an NTFS local drive and I created a 9 level deep folder ok.
  • ariestav
    ariestav over 4 years
    I've also encountered this issue where the mkdir command will not create nested directories in a single command on a mapped network drive. I'm not sure why it won't but then again we're talking about Microsoft -- am I right?