What is equivalent to Linux mkdir -p in Windows? - windows

In Linux, mkdir -p creates a folder tree.
What is the equivalent option in Windows to create a folder tree? Is there any?

In Windows, mkdir creates directory trees by default.
mkdir a\b\c

The Windows mkdir does it automatically if command extensions are enabled. They are on just about every box I've ever used but, if they're not, you can create your own script to do it:
#echo off
setlocal enableextensions
md %1
endlocal
Expanding:
Command extensions are an added feature of cmd.exe which allows you to do so much more (at the cost of a little compatibility with earlier incarnations of the batch language).
Windows XP cmd.exe should have these extensions enabled by default but you can configure your box so that they're disabled by default (using "cmd /e:off" as the default processor). If you do that and want to use the extensions, your cmd files must have a setlocal to turn them back on.
The script above could be called md2.cmd and then you would be guaranteed to be able to create multiple directory levels with "md2 a\b\c" without having to worry whether the extensions were enabled.
Almost every one of the cmd scripts I write begins with:
setlocal enableextensions enabledelayedexpansion
to ensure I get as close as possible to the behavior of my beloved bash :-)

For a strange reason when I attempted to create a directory with the following method;
mkdir src/main/java/main/resources
it didn't work, I had to surround the path in double quotes, as shown below;
mkdir "src/main/java/main/resources"
Additionally, unix allows for this;
mkdir -p src/main/java src/main/resources
where two branches will be created as shown below, the equivalent to that on windows is;
mkdir "src/java/resources" "src/main/resources"
src
-----java
-------resources
-----main
-------resources
I hope this helps!
xox

If you want to use forward slashes, just give the directory structure you want within double quotes.
mkdir "org/frame/bu/fed/config"

mkdir by default makes all intermediate directories. Just ensure that you use '\' as the separator.

I just try to create multiple folders on today and it is working!
mkdir "templates" "static/css" "static/js"

Related

Is it possible to change the directory of a windows console from an app?

My goal is to write an app that lets you quickly assign aliases to long directory paths and change to them. I wrote an app that manages them in a file in the user's appdata directory, but I can't find a way to change the directory of the shell I run the program in from my app. My goal is to have it work from git bash, cmd.exe, and powershell. I want something like this:
cd /c/vsts/some-long-project-name-reports
g -a reports
Now I have an alias 'reports' for that directory. What I want to do get to that directory next time I open a console is:
g reports
I'm using dotnet core, though looking through questions it seems like there isn't a way to do this at all. With Directory.SetCurrentDirectory(path); or Environment.CurrentDirectory = path; it changes the working directory of the g.exe process, but when it exits the shell goes back to it's working directory when I ran the command.
I've come up with a solution for git bash, I changed my g app to output the path instead and have this as go in my path:
OUTPUT="$(g $1)"
cd $OUTPUT
Then I just need to use . or source to run the script in the current shell:
. go reports
And batch file go.bat doesn't need the . or source to work:
for /F "tokens=*" %%i in ('g %1') do set OUTPUT=%%i
cd %OUTPUT%
I guess I'll have to live with typing the extra characters, but is there a similar way to do this with powershell?
Define a wrapper function in PowerShell (assuming that g.exe outputs the target path):
function g { Set-Location (g.exe $args) }
Generally, as eryksun points out in a comment, an executable - which by definition runs in a child process - cannot change its parent process' working directory.
Therefore, the only solution is to output the target directory's path and let the parent process change to it.

Why do you need to change drives in cmd prompts?

In a cmd prompt, if you want to go from
c:\foo\
to
d:\bar\
You must type
d:
to change drive first, before you can
cd d:\bar\
On Linux, you do not have to do this. Why does cmd unintuitively make you have to do this? Historical? Implementation limitation? By design?
I understand that
cd /d
mimics the Linux behavior, however why is Linux's behavior not default?
As far as I can tell when you
cd anotherDrive:\directory\
it just does nothing, so it's not like the "without /D" usage is reserved for some other semantics.
Having one current working directory per drive allows you to do things like
> cd d:\some\long\path\ # Set the working directory of that other drive
> copy myFile.txt d: # Copy the current file to that drive
> cd .. # Go elsewhere
> copy anotherFile.txt d: # Copy another
I bet this was handy back in the days when you worked with 2 floppy drives.
I don't see any technical reasons for this. Presumably just a design choice of how to work with the prompt.
As PA. mentions you can use the /d switch if you don't like this behaviour.
you don't need to
just read HELP CD
and then try
CD /d d:\bar
the CD default behavior is still there for backwards compatibility with old versions of DOS where the existence of dual diskette bays made it useful.
Newer commands, like pushd for example, make the default behavior to honor drive and path.

Copy and paste files from one directory into current working directory

I'm trying to debug why i can't run this command
DOSKEY w-new=copy C:\utils\ticket_templates\wordpress\readme.txt "%cd%/readme.txt" /Y
I've tried both windows cmd as administrator and not
If i simply type C:\utils\ticket_templates\wordpress\readme.txt "%cd%/readme.txt" into the terminal it works but not if i use the w-new alias.
The w-new alias works inconsistently too. For example i can use it in /user/%USERPROFILE%/ dir but not in any of it's sub-directories.
I checked the file permissions of the destination and it's exactly the same as /user/%USEPROFILE%/
Any advice on debugging this is appreciated. Both source and destination are git repositories btw. As is /user/%USERPROFILE%/
Your problem is %cd% is expanded when the w-new alias is defined, so the copy destination becomes constant - the current directory at the time of macro definition. You can see this by typing the following from the command line prompt:
doskey /macros
Also, you should not use / as a folder separator, use \ instead. The / often works, but not in all situations.
You can prevent %cd% from being expanded during definition by inserting a caret within the variable name:
DOSKEY w-new=copy C:\utils\ticket_templates\wordpress\readme.txt "%^cd%\readme.txt" /Y
But why provide any target at all? You are simply copying a file to the current directory, using the original file name - that is the default behavior of COPY when no destination is specified.
So your macro can simply be:
DOSKEY w-new=copy C:\utils\ticket_templates\wordpress\readme.txt

Populating empty folders recursively with Windows

I have a huge series of folders, named 'A' through 'Z' such that each folder has subfolders in the same form.
For example, I could have a directory at this path: .\A\D\E
I want to populate each of these folders with an empty file (so I can commit it to a git repository if anyone's curious).
Windows doesn't have an equivalent of Linux's touch, but I have the GnuWin32 toolset installed so I can, in fact, use touch in my Windows environment.
I've started by creating a batch file with the following:
FOR /D /r do touch empty
But when I run it, the folders aren't populated. I don't get any errors either:
C:\sandbox>FOR /D /r do touch empty
C:\sandbox>
Does anybody see anything glaringly wrong about the line of batch script above? Is there anything else I can try short of using additional non-Windows commands?
You just need to get the syntax of the FOR command right:
for /r %f in (.) do touch %f\empty

.bat file - cd to a directory with a space in its name and also using a variable?

ive written a quick .bat file that reads in the name of a directory typed in by the user, i store that variable in a variable, and then i want to actually cd to that directory.
i've tested it out with simple directories like "C:," for instance, and that works. however, when i'm dealing with the user entering in something like "C:\Documents and Settings\Desktop," i can't do cd %directory%\sampleFolder.
i keep getting an error of "the system cannot find the path specified," even though i'm using the full name. anyone know how to overcome this?
How about:
cd "%directory%\sampleFolder"
set /p DIR="path:"
cd %DIR%
Works just fine.
#ECHO OFF
ECHO Enter Directory
SET/p directory=
CHDIR %directory%
Works for me (Windows 7) but should work for XP/Vista/etc

Resources