What is the equivalent of mkdir -p on Windows 10? - windows

I've been unable to find help files on Windows for things like MD, mkdir or new-item. Doing update-help just throws errors.
How do I do mkdir -p on Windows 10 in Command Prompt?
I want to create a directory even if that directory already exists.

mkdir of Windows 10 does not support -p or /p or an equivalent flag.
If you want realize the -p functionality according to Unix operating systems you can do that in one line as follows:
set MKDIR_DIR=D:\XYZ\123\ABC && if not exist %MKDIR_DIR% mkdir %MKDIR_DIR%
Replace D:\XYZ\123\ABC with the desired directory.

Use mkdir /? to get information on the command. -p is not a flag in Windows, parent/intermediate directories will be created if command extensions are turned on.
See this Microsoft documentation for more information.

Since mkdir in Windows 10 (even with command extensions enabled) does not the job, here is the content of my mkdir.bat that DOES it for a %path% at a %drive% :)
#ECHO OFF
SETLOCAL enableextensions
SETLOCAL enabledelayedexpansion
REM ## Create directory structure levelwise since there is nothing like "mkdir -p"
REM ## substitute "\" with ";"
SET "pathParts=%path:\=;%"
REM ## iterate over a list of the tokens between " ", ",", ";" or "="
SET pathToMake=%drive%
FOR %%d IN (%pathParts::= %) DO (
SET pathToMake=!pathToMake!\%%d
ECHO !pathToMake!
IF NOT EXIST !pathToMake! MKDIR !pathToMake!
)
Just call it like that:
D:\>SET drive=D:
D:\>SET path=XYZ\123\ABC
D:\>mkdir.bat
D:\XYZ
D:\XYZ\123
D:\XYZ\123\ABC
And et voilĂ : D:\XYZ\123\ABC exists :)

There is very easy solution in windows. -p is not required at all.
If you want to create nested folders then write like below:
mkdir \Taxes\Property\Current
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/mkdir

On Windows 11 Pro using bash this command did the job for me:
mkdir.exe -p folder/subFolder
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/mkdir

Related

running exe command from another directory in a for loop

grep.exe has to be in the current directory in order for the below command to work:
FOR /F tokens^=4^ delims^=^" %%A IN ('grep.exe -o -P -m 1 "name=\"securitytoken\" value=\".*?\"" "forum_post_edit.html"') DO SET "securitytoken=%%A"
ECHO %securitytoken%
I tried enclosing the path to grep.exe between double quotes like "bin/grep.exe" but that messed up the syntax of the code.
Now I can use pushd and popd to temporarily switch to another directory where grep.exe is located but that affects forum_post_edit.html
How can I work around this problem?
Building on #Mofi's comment above, you can extract the short name of grep.exe using this command:
FOR /F %%A IN ("grep.exe") DO SET GrepPath=%%~fs$PATH:A
Now you can reference the short path in your command:
FOR /F tokens^=4^ delims^=^" %%A IN ('%GrepPath% -o -P -m 1 "name=\"securitytoken\" value=\".*?\"" "forum_post_edit.html"') DO SET "securitytoken=%%A"
ECHO %securitytoken%
Note: grep.exe would need to be in a folder defined in your system %PATH% directory in order for this to work.

Batch script to execute some commands in each sub-folder

I need to write a script to work in Windows, that when executed will run a command in some of sub-directories, but unfortunately I have never done anything in batch, and I don't know where to start.
With the example structure of folders:
\root
\one
\two
\three
\four
I want the script to enter the specified folders (e.g. only 'one' and 'four') and then run some command inside every child directories of that folders.
If you could provide any help, maybe some basic tutorial or just names of the commands I will need, I would be very grateful.
You can tell the batch to iterate directories:
for /d %i in (C:\temp\*) do ( cd "%i" & *enter your command here* )
Use a percent sign when run directly on the command line, two when run from a batch
In a batch this would look something like this:
#echo off
set back=%cd%
for /d %%i in (C:\temp\*) do (
cd "%%i"
echo current directory:
cd
pause
)
cd %back%
Put the commands you need in the lines between ( and ).
If you replace C:\temp\ with %1 you can tell the batch to take the value of the directory from the first parameter when you call it.
Depending of the amount of directories you then either call the batch for each directory or read them from a list:
for /f %i in (paths.lst) do call yourbatch %i
The paths.lstwill look like this:
C:\
D:\
Y:\
C:\foo
All of this is written from memory, so you might need to add some quotations marks ;-)
Please note that this will only process the first level of directories, that means no child folders of a selected child folder.
You should take a look at this. The command you are looking for is FOR /R. Looks something like this:
FOR /R "C:\SomePath\" %%F IN (.) DO (
some command
)
I like answer of Marged that has been defined as BEST answer (I vote up), but this answer has a big inconvenience.
When DOS command between ( and ) contains some errors, the error message returned by DOS is not very explicit.
For information, this message is
) was unexpected at this time.
To avoid this situation, I propose the following solution :
#echo off
pushd .
for /d %%i in (.\WorkingTime\*.txt) do call :$DoSomething "%%i"
popd
pause
exit /B
::**************************************************
:$DoSomething
::**************************************************
echo current directory: %1
cd %1
echo current directory: %cd%
cd ..
exit /B
The FOR loop call $DoSomething "method" for each directory found passing DIR-NAME has a parameter. Caution: doublequote are passed to %1 parameter in $DoSomething method.
The exit /B command is used to indicate END of method and not END of script.
The result on my PC where I have 2 folders in c:\Temp folder is
D:\#Atos\Prestations>call test.bat
current directory: ".\New folder"
current directory: D:\#Atos\Prestations\New folder
current directory: ".\WorkingTime"
current directory: D:\#Atos\Prestations\WorkingTime
Press any key to continue . . .
Caution: in Margeds answer, usage of cd "%%i" is incorrect when folder is relative (folder with . or ..).
Why, because the script goto first folder and when it is in first folder it request to goto second folder FROM first folder !
On Windows 10 and later, it should be like this:
#echo off
for /D %%G in ("C:\MyFolderToLookIn\*") DO (
echo %%~nxG
)
This will show the name of each folder in "C:\MyFolderToLookIn". Double quotes are required.
If you want to show full path of the folder, change echo %%~nxG with echo %%G

What is the difference between MD and MKDIR batch command?

Both command creates folders. I read that MKDIR can create even subfolders.
Is that only difference?
Why there are two commands doing the same?
Which one should I use?
In addition to #npocmaka's answer, I want to provide a list of all such aliases, just for reference:
cd = chdir
md = mkdir
rd = rmdir
ren = rename
del = erase
Just aliases of the same command.Here are the help messages:
C:\>md /?
Creates a directory.
MKDIR [drive:]path
MD [drive:]path
and
C:\>mkdir /?
Creates a directory.
MKDIR [drive:]path
MD [drive:]path
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.
On Linux/Unix/MacOS, mkdir is very similar, but md means nothing. If you want something cross-platform, you should use mkdir.
For Question 01
Literally, md and mkdir commands are the same in their functionality. Microsoft Learn web page says this.
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/mkdir
Both md and mkdir are able to create subfolders without using any cd command.
For example;
mkdir a\b\c acts the same as md a\b\c if b and c directories don't exist inside a directory.
It will create 'a' directory then go inside it and create 'b' directory then go inside it and create 'c' directory. If all of a,b, and c do exist, will print an error.
For Question 02
Actually, I have no idea dude!
For Question 03
If you are expecting a cross-platform experience, you better use mkdir command.

Command Prompt Script command "cd"

Ok so I was making my own little command prompt for my own personal use and I have been fighting to make it work for the past 2 hours. Here is what I have done:
#echo off
set /p labnum="Enter Lab Numnber:"
set labdir=C:\Users\BLAHBLAHBLAH\Dir\Lab-
set labdir2="%labdir%%labnum%"
cd labdir2
:cmd
set /p cmd=">"
%cmd%
cls
goto cmd
I basically want to be able to change the path before each "session" But every time the cd labdir2 command is executed, my computer whines, "The system cannot find the path specified." And I know FOR SURE that the directory exists! I have pasted the text straight from windows explorer. Any and all help is appreciated.
Thank you!
The error is here:
cd labdir2
that changes to a directory called labdir2, but you want to change to a directory indicated by the contents of the variable:
cd %labdir2%
to make sure you can cope with special characters I'd enclose it with double quotes:
cd "%labdir2%"
You might even want to include the /d swith with the cd command to make sure that you also change the current drive. So the final version should be:
cd /d "%labdir2%"

How to print current directory name via MS-DOS commands

I need to print current directory name after CD command.
Is it possible to do? Thanks!
echo off
echo.
echo This batch program deletes some files
echo.
pause
cd "D:\Folder1\"
pause
Just use cd with no arguments supplied. From cd /?:
Type CD without parameters to display the current drive and directory.

Resources