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.
Related
I'm currently in the process of running a script that exports a registry key for Outlook profiles before deleting them.
My script currently achieves this - however I am trying to create a folder named Outlook_Backup under the user profile which is dated and has a timestamp.
Is there any way of being able to Create the Outlook_Backup directory and then cd to the Outlook_Backup-dd-mm-yy folder in this location despite having a different folder name each time the script is run?
Usually you could cd to Outlook_Backup but each folder will change such as Outlook_Backup-29-01-2019-26-08-91 and Outlook_Backup-29-01-2019-26-15-65
cd %userprofile% & mkdir Outlook_Backup & cd Outlook_Backup & mkdir Outlook_Backup-%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"-"%time:~3,2%"-"%time:~6,2%"-"%time:~9,2%
The code provided works, however is there a way to append something such as cd latest.Outlook_Backup or the latest created in the Outlook_Backup directory?
I totally get where you are comming from with this.
There are times you want to just paste one or several lines into a CMD Prompt CLIinstance and be done ( and might want to just, as I put it about my own such use of CMD CLI, "Be Lazy", not create permanent variables, or keep itall in one lineto easily give yo oyhers to paste on in to cmd or for whatever reason)
So you have a few options:
A: separate the cmd into separate lines, save the date in a variable and use throughout.
B: wrap everything in a loop ( which also means it can still be written to span multiple lines without them running imdividually as they are entered )
C: Use a loop at the end to find the name of the folder written each time.
All are completely valid methods.
I usually prefered option B when trying to keep things single lined as the loop allows me to stipl paste itnmulti-linee but get a more? when testing in case I missed something in my copy paste, and its easy to expand/collapse (IMO)
I'm on mobile, writing this and my kids just started waking up. Gotta get rolling.
I will paste option B in real quick if I can but not going to have time to write all 3 just this moment.
Oh, another thing, avoid CDing into locations if possible. Ymmv but often writing a cmd that generates the full path is more efficient and less likely to cause issues.
In any case:
For %%A IN ("%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"-"%time:~3,2%"-"%time:~6,2%"-"%time:~9,2%") DO ( cd %userprofile% & mkdir Outlook_Backup & cd Outlook_Backup & mkdir Outlook_Backup-%%~A & CD Outlook_Backup-%%~A
)
The above can be run across multiple lines as so:
For %%A IN (
"%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"-"%time:~3,2%"-"%time:~6,2%"-"%time:~9,2%"
) DO (
cd %userprofile%
mkdir Outlook_Backup
cd Outlook_Backup
mkdir Outlook_Backup-%%~A
CD Outlook_Backup-%%~A)
You don't neee those ChDir (CD) statements (as I mention above) and MkDir (MD) can create multiple levels of sub directories, so it would be pragmatic to write it as this:
For %%A IN (
"%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"-"%time:~3,2%"-"%time:~6,2%"-"%time:~9,2%"
) DO (
MD "%userprofile%\Outlook_Backup\Outlook_Backup-%%~A\"
CD /D "%userprofile%\Outlook_Backup\Outlook_Backup-%%~A\" )
Or as a single line:'
For %%A IN ( "%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"-"%time:~3,2%"-"%time:~6,2%"-"%time:~9,2%") DO ( MD "%userprofile%\Outlook_Backup\Outlook_Backup-%%~A\" & CD /D "%userprofile%\Outlook_Backup\Outlook_Backup-%%~A\" )
What is the syntax that would allow me to delete folder and the create it, regardless of it prior (in)existence?
if exist folder rmdir folder && mkdir folder
Will NOT work. Neither replacing && with &, ||, |...
Which is proper syntax then?
OS: Windows 2012
Shell: cmd.exe*
PS I could accept PS based answers as long as two conditions are met. 1) It have to start from cmd.exe 2) It have to give meaningufull errors to cmd standard output if there are any encountered (like lack of permissions).
Since you are using rmdir without the /S switch I assume the folder of interest is empty, so why removing and recreating it? You could simply create it and suppress the error message in case it already exists, like:
mkdir "folder" 2> nul
This variant maintains all folder attributes and the owner, of course.
If you insist on removing and recreating the folder, use this:
rmdir "folder" 2> nul & mkdir "folder"
This variant destroys all folder attributes and the owner.
In case the folder is not guaranteed to be empty, you need the /S switch for rmdir; also the /Q is useful to avoid being prompted to really remove the folder, like:
rmdir /S /Q "folder" & mkdir "folder"
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
I have a Directory named A on Desktop.
It has the following hierarchy:
A -> B -> C -> D
A -> Q -> W -> X -> D
A -> S -> D -> F
All the above are Directory names. I want to know if a directory named D is there inside directory A. After ensuring the existence of D, I want to know the full path(s) from directory A to D.
Someone please help me in this regard.
Using find inside A:
Unix
find . -name "D" -type d
Windows
dir A /AD /S
I don't know anything about git-bash.
Crazy as it seems, native batch does not have a direct way to list all locations of a specific folder name. Unless the DIR command includes a wild card, then DIR will look for files or folders within the specified folder, rather than listing the folder name itself.
The solution is to pipe the result to FINDSTR
dir /s /b /ad A | findstr /irc:"[\\]D$"
Performance might be optimized a wee bit if the target folder name is added to the DIR command, with a wildcard appended:
dir /s /b /ad A\D? | findstr /irc:"[\\]D$"
Not sure if this is what you mean, but ...
.... Setup ....
user#machine ~:$ mkdir tmp
user#machine ~:$ cd tmp
user#machine ~/tmp:$ mkdir -p A/B/C/D
user#machine ~/tmp:$ mkdir -p A/Q/W/X/D
user#machine ~/tmp:$ mkdir -p A/S/D/F
... find command ....
user#machine ~/tmp:$ find ./ -type d -name 'D'
./A/B/C/D
./A/S/D
./A/Q/W/X/D
user#machine ~/tmp:$
Can anybody tell me how to do the following in in a Windows batch script? (*.bat):
Create a folder only if it doesn't already exist
In more detail, I want to create a folder named VTS on the C:\ drive, but only if that folder doesn't already exist. I don't want to overwrite the contents of the folder if it already exists and the batch is executed.
You just use this: if not exist "C:\VTS\" mkdir C:\VTS it wll create a directory only if the folder does not exist.
Note that this existence test will return true only if VTS exists and is a directory. If it is not there, or is there as a file, the mkdir command will run, and should cause an error. You might want to check for whether VTS exists as a file as well.
if exist C:\VTS\NUL echo "Folder already exists"
if not exist C:\VTS\NUL echo "Folder does not exist"
See also https://support.microsoft.com/en-us/kb/65994
(Update March 7, 2018; Microsoft article is down, archive on https://web.archive.org/web/20150609092521/https://support.microsoft.com/en-us/kb/65994 )
Just call mkdir C:\VTS no matter what. It will simply report that the subdirectory already exists.
Edit: As others have noted, this does set the %ERRORLEVEL% if the folder already exists. If your batch (or any processes calling it) doesn't care about the error level, this method works nicely. Since the question made no mention of avoiding the error level, this answer is perfectly valid. It fulfills the needs of creating the folder if it doesn't exist, and it doesn't overwrite the contents of an existing folder. Otherwise follow Martin Schapendonk's answer.
mkdir C:\VTS 2> NUL
create a folder called VTS and output A subdirectory or file TEST already exists to NUL.
or
(C:&(mkdir "C:\VTS" 2> NUL))&
change the drive letter to C:, mkdir, output error to NUL and run the next command.
set myDIR=LOG
IF not exist %myDIR% (mkdir %myDIR%)
I use this way, you should put a backslash at the end of the directory name to avoid that place exists in a file without extension with the same name as the directory you specified, never use "C:\VTS" because it can a file exists with the name "VTS" saved in "C:" partition, the correct way is to use "C:\VTS\", check out the backslash after the VTS, so is the right way.
#echo off
#break off
#title Create folder with batch but only if it doesn't already exist - D3F4ULT
#color 0a
#cls
setlocal EnableDelayedExpansion
if not exist "C:\VTS\" (
mkdir "C:\VTS\"
if "!errorlevel!" EQU "0" (
echo Folder created successfully
) else (
echo Error while creating folder
)
) else (
echo Folder already exists
)
pause
exit
You can use:
if not exist "C:\VTS\" mkdir "C:\VTS"
You can also expand the code to replace any missing expected files.
if not exist "C:\VTS\important.file" echo. > "C:\VTS\important.file"
This should work for you:
IF NOT EXIST "\path\to\your\folder" md \path\to\your\folder
However, there is another method, but it may not be 100% useful:
md \path\to\your\folder >NUL 2>NUL
This one creates the folder, but does not show the error output if folder exists. I highly recommend that you use the first one. The second one is if you have problems with the other.
You need to create a folder if it doesn't exist eh? Well, here is an example of how to do it.
First, I check to see if the folder doesn't already exist by entering this code:
if not exist "FOLDERPATH" (
mkdir "FOLDERPATH"
)
So if I run the code. And if the folder already exists, It will do nothing. This is what we do if the folder already exists:
if exist "FOLDERPATH" (
rmdir /s /q "FOLDERPATH"
mkdir "FOLDERPATH"
)
Now if I run the code, It will re-create the folder if it already exists. This is the example code:
#echo off
cls
if not exist "C:\ExamplePath\" (
echo Creating Folder...
mkdir "C:\ExamplePath\"
pause
)
if exist "C:\ExamplePath\" (
echo Re-Creating Folder...
rmdir /s /q "C:\ExamplePath"
pause
)
Now the if exist part is Optional. If the folder already exists, you can jump to an label instead like this:
if exist "FOLDERPATH" (
goto :ExampleLabel
:ExampleLabel
echo Hi.
pause
)
Hopefully, this could help with your problem.
Personally, I would do this:
if not exist "C:\YourFolderPathHere\" (
mkdir C:\YourFolderPathHere\
) else (
echo Folder already exists!
)
Let's break that down:
if not exist "C:\YourFolderPathHere\": this checks for the folder. The Backslash (\) after the path is very important, else it will look for a file rather than a folder.
mkdir C:\YourFolderPathHere\: Creates the directory if the above statement is true.
echo Folder already exists!: prints to console that it already exists.
Here's the same code, but with comments:
::Does the folder exist?
if not exist "C:\YourFolderPathHere\" (
::No, make it.
mkdir C:\YourFolderPathHere\
) else (
::Yes, don't make it, and print text.
echo Folder already exists!
)
One-line version:
if not exist "C:\YourFolderPathHere\" mkdir C:\YourFolderPathHere\
Haven't tested it though, so don't quote me on it.
i created this for my script I use in my work for eyebeam.
:CREATES A CHECK VARIABLE
set lookup=0
:CHECKS IF THE FOLDER ALREADY EXIST"
IF EXIST "%UserProfile%\AppData\Local\CounterPath\RegNow Enhanced\default_user\" (set lookup=1)
:IF CHECK is still 0 which means does not exist. It creates the folder
IF %lookup%==0 START "" mkdir "%UserProfile%\AppData\Local\CounterPath\RegNow Enhanced\default_user\"