Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mkdir in windows multiple path in single command

@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?

like image 643
m.k.frenky Avatar asked Jan 20 '14 01:01

m.k.frenky


4 Answers

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.
like image 142
Michael Burr Avatar answered Sep 22 '22 23:09

Michael Burr


@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.

like image 44
m.k.frenky Avatar answered Sep 25 '22 23:09

m.k.frenky


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.

like image 29
John Deters Avatar answered Sep 24 '22 23:09

John Deters


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?

like image 27
Magoo Avatar answered Sep 25 '22 23:09

Magoo