How to go to a subdirectory in CMD? - windows

How can I go to a subdirectory without specifying its whole path?
So when I am in
C:/users/USERNAME/desktop/project/build/
How can I then navigate to
C:/users/USERNAME/desktop/project/build/directory 1/
I tried
cd /directory 1
But this results in
The system cannot find the path specified.
I want to use something as simple as the .. command, which goes one directory back without specifying its whole path. Is there something similar to change to a subdirectory?

On Windows the directory separator is \ ... the backslash character. The slash character / is used on Windows for parameters/options of a command/executable/script.
For compatibility reasons like #include instructions in C/C++/C# with a relative path using / as directory separator Windows accepts also paths with / and automatically convert them to \ when a string is interpreted as file/folder name with a path. That is done by the file IO functions used by Windows executables on accessing the file system of a storage device.
A path starting with a backslash is a path relative to root of current drive.
For that reasons cd /Directory 1 is executed as cd /D irectory 1 which means with interpreting /D at beginning of the directory argument string as optional option /D by command CD to change also the drive and the rest of the directory argument string not enclosed in double quotes as name of the subdirectory to change current directory to. The subdirectory irectory 1 does not exist on your machine.
If the directory name starts with any other character than letter D and using / instead of \ at beginning of the directory argument string like on using cd /Folder 1, there would be executed cd "C:\Folder 1" with drive C: being the current drive.
There are three types of relative paths:
Path is relative to current directory if path starts with .\ or with directory name.
Path is relative to parent directory of current directory if path starts with ..\.
Path is relative to root of current drive if path starts with \.
.\ and ..\ can be also used anywhere inside a path after a directory separator multiple times and Windows automatically resolves the path to an absolute path.
So correct would be using cd "Directory 1", or not recommended cd Directory 1.
The help of command CD output on running cd /? explains that the command CD does not require enclosing a path with one or more spaces in double quotes as usually required because a space is usually interpreted as separator between arguments to pass to a command, executable, or script.
It is nevertheless best practice to always enclose the path in double quotes because of some other characters require path being enclosed in double quotes to interpret (nearly) all characters in path as literal characters.
For example a directory has the name Test & Doc. With cd Test & Doc the Windows command processor executes cd Test and next tries to find an executable or script with Doc and Doc.* having a file extension listed in environment variable PATHEXT in current directory or any directory listed in environment variable PATH because of the ampersand is interpreted as unconditional AND operator for execution of multiple commands on a single command line. But on using cd "Test & Doc" the entire double quoted string is interpreted as name of a directory to change to by the Windows command processor.
BTW: All internal (included in cmd.exe ... the Windows command processor) and external commands (executables installed by default in %SystemRoot%\System32) can be started with the parameter /? in a command prompt window to get displayed the help for this command.
cd /?
cmd /?
pushd /?
popd /?
More details on Windows commands can be found at
Microsoft's command-line reference
SS64.com - A-Z index of the Windows CMD command line
Single line with multiple commands using Windows batch file
The Microsoft documentation about Naming Files, Paths, and Namespaces offers even more details about file names and how paths are interpreted by Windows kernel. Everybody writing program or script code for applications or scripts executed on Windows should have read this documentation page at least once from top to bottom.

Related

STAR error - INPUT file error: could not open read file: Read1 [duplicate]

When creating filepaths and URLs, I noticed that many times the path starts with ./ or ~/.
What is the difference between filepaths that start with ./ and ~/?
What do each of them mean?
For the sake of completeness ...
Just path is a file or directory named path in the current directory.
./path is a file or directory named path in the current directory, with the directory spelled out. The dot directory . represents the current directory, and path is the name of the file or directory within this directory.
~/path is a shorthand for $HOME/path where $HOME is a variable which refers to your home directory. Typically your home directory will be somewhere like /home/you or /Users/you where you is your account name. (The command echo "$HOME" will display your home directory.) The expanded value is an absolute path (unless you have messed up the value of $HOME thoroughly), as indicated by the initial slash.
/path is an absolute path which refers to a file or directory named path which is in the root directory /. Every file on Unix is ultimately somewhere in the directory tree which starts with the root directory.
A file name which begins with $ includes the value of a shell variable in its name (like for example $HOME above); you have to know the value of that variable to determine whether it ends up containing a relative or an absolute path. Similarly, ~ at the beginning of a file name gets replaced ("expanded") by the shell to a different string, as outlined above.
(Technically, it's possible for a file name to begin with a literal dollar sign or tilde, too; you would then have to quote or backslash-escape that character to avoid having the shell expand it to something else. This is rather inconvenient, so these file names tend to be rare in practice.)
In the following exposition, we refer to the result of any such replacements, and ignore the complication of possible quoting.
Every file name which begins with / is an absolute path (aka full path) which explains how to reach a particular node starting from the root directory. For example, /var/tmp/you/reminder.txt refers to a file or directory reminder.txt (probably a file, judging from the name; but Unix doesn't care what you call your files or directories) which is in the directory you which is in the directory tmp which is in the directory var which is in the root directory.
Every file name which doesn't begin with / is a relative path which indicates how to reach a particular file or directory starting from the current directory. The special directory .. is the parent directory (that is, the directory which contains this directory) and the special directory . is the current directory. So path/there refers to the file or directory there inside the directory path in the current directory; and (hover the mouse over the gray area to display the spoiler)
there/.././and/back/.. is a (wicked complicated) way to refer to the directory and in the current directory, where we traverse the there directory and then move back to the current directory; then stay in the current directory; then refer to the directory back inside the directory and, but then move back to the parent directory of that, ending up with ./and.
In addition to ~/ for the current user's home directory, some shells and applications allow the notation ~them/ to refer to the home directory of the user account them. Also, some web server configurations allow each user to have a public web site in their directory ~/public_html and the URL notation http://server/~them/ would serve up the site of the user account them for outside visitors.
The current directory is a convenience which the shell provides so you don't have to type long paths all the time. You can, if you want to.
/bin/ls /home/you/Documents/unix-101/directories.txt
is a longwinded but perfectly valid way to say (assuming you are in your home directory),
ls Documents/unix-101/directories.txt
You could also say
cd Documents/unix-101
ls directories.txt
and until you cd again, all your commands will run in this directory.
See What exactly is current working directory? for a longer exposition of this related concept.
A "directory" is sometimes called a "folder" by people who are not yet old enough to prefer the former.
Tangentially, don't confuse the directory name . with the Bourne shell command which comprises a single dot (also known by its Bash alias source). The command
. ./scriptname
runs the commands from the file ./scriptname in the context of the current shell instance, as opposed to in a separate subshell (which is what just ./scriptname does). In other words, this command line invokes the dot command on a file scriptname in the dot directory.
The Bourne shell (and derivatives like Bash, Zsh, etc) use single quotes to prevent variable expansion and wildcard expansion, and double quotes to permit variable expansion, but inhibit wildcard expansion in a string. The quoting rules on Windows are different, and generally use double quotes to keep whitespace-separated values as a single string (and % instead of $ for variable substitutions).
./ means "starting from the current directory". . refers to the current working directory, so something like ./foo.bar would be looking for a file called foo.bar in the current directory. (As a side note, .. means refers to the parent directory of the current directory. So ../foo.bar would be looking for that file one directory above.)
~/ means "starting from the home directory". This could have different meanings in different scenarios. For example, in a Unix environment ~/foo.bar would be looking for a file called foo.bar in your home directory, something like /home/totzam/foo.bar. In many web applications, ~/foo.bar would be looking for a file called foo.bar in the web application root, something like /var/http/mywebapp/foo.bar.
./ is the current directory
~/ is the home directory of the current user
./ means that path is relative to your current position.
~/ means that path is relative to your home directory.
I will explain a simple example of it. As developers mentioned:
./ is current directory.
~/ is the home directory of the current user.
How both of the file path expressions can help us? Suppose you want to execute a script (.sh) and you're in the same directory where file exists then you can simply do it ./filename.sh
I mostly use ~/ to access my home directory files like .bashrc when I want to add any config in it. It's easier since the file path expression (for home directory) feels much easier and makes accessibility to the file from anywhere, without worrying about the path or changing the path.
. represents current directory
.. represents the parent directory
~ represents the home directory for the current user. Home directory is also represented by HOME env variable. you can do echo $HOME on the shell to see it.
These are generally used to specify relative paths. The / in the end of each notation is a separator that you can use when using these notations together.
Ex:
$ cd ../.. # Go 2 directories backwards
$ cd ~ # Takes you to $HOME directory
$ cd . # Does nothing :) As it literally means go to the directory that you are already present in.
$ cd ~/dir1 $ go to `$HOME/dir1`
On Unix, in any directory if you do ls -a you would see that . and .. will be mentioned (even for empty directory). Like mentioned, these have special meaning and are generated by default in Unix systems and are generally helpful to specify relative paths (i.e, path to a different directory relative to your current directory)
cd command is harmless. So, just play around by combining notations with cd command. You will eventually get a grip of them.

Get relative path of a parent's subfolder

I had a massive batch file which I split into several smaller ones, with one master file calling each of the smaller files individually. For neatness I put the individual scripts in a subfolder from the project folder (the master script is in the project folder).
This has however has caused an issue - I can't work out how to change some paths in the new individual scripts. Here is approximately what layout is like:
Project
|---MasterScript.bat
|
|---Scripts
| |---scriptA.bat
|
|---Exes
| |---program.exe
| |---config.xml
So the master script calls each of the batch scripts - A and script A calls program.exe with argument /config config.xml.
The issue is how to address program.exe and config.xml.
I unfortunately have just hacked these scripts together without really knowing how batch file paths are resolved, and so have literally no idea how to write relative paths using the parent folder etc. relationships.
So essentially I am asking in general how to write relative batch paths and specifically how to write these paths.
%~dp0 expands to drive and path of argument 0 which is the batch file itself. This file path reference expands always to a path with a backslash. Try it out with a batch file containing only
#echo off
echo Batch file path is: "%~dp0"
pause
How to reference arguments (parameters, options) of a batch file without or with a modifier is described in help output on running call /? in a command prompt window on several display pages.
Short relative path tutorial:
Paths starting with name of a directory or a file name are relative to current directory.
Paths starting with .\ are also relative to current directory.
Paths starting with ..\ reference the parent directory of current directory.
Paths starting with just \ are relative to root directory of current DRIVE.
For example \Windows references the directory Windows in root of current drive independent on which directory is the current directory.
.\ and ..\ can be also used one or more times nearly anywhere within a path. .\ and ..\ can't be used left to drive letter and colon as this would mean a directory in current or parent directory is specified which contains a colon in name which is not possible.
The usage of ..\..\ or even more ..\ in sequence helps to reference a path 2 or more directories upwards in directory structure relative to current directory.
On just running applications it is absolutely valid to use ~dp0..\Exes\program.exe.
It is also no problem to reference other directories or files with such a mixture of absolute and relative path components in arguments passed to an application or script.
But it is often helpful if the real absolute full path is determined from a pure relative path or a mixture of absolute and relative path before passing a directory or file name with path to an application or script for getting better readable warning and error messages containing the passed directory/file name.
The command FOR can be used to get full path of a directory or file from a relative path or a mixture of absolute and relative path.
Example:
#echo off
for /F %%I in ("%~dp0..\Exes") do set "FullExesPath=%%~fI"
echo Path to project executables: "%FullExesPath%"
pause
Note: There is no backslash at end of this resolved path. On using %FullExesPath% in a file reference the backslash must be typed between this directory reference and the file name which makes the string easier to read.
For details on command FOR run in a command prompt window for /? and read carefully the output help pages.

Windows CMD Shorten file path

I got Gnu Utilities that adds the command sed to windows, but to use it I have to type:
C:\ProgramFiles\GnuWin32\bin\sed.exe <args>
How do I shorten this to just sed <args>?
To run an executable without a full path, it needs to be either in the current directory, or in the PATH environment variable. In the CMD prompt, there are several ways to do this.
The first way is to put C:\ProgramFiles\GnuWin32\bin in your PATH variable, which makes every program in that directory available without a full path.
set "PATH=%path%;C:\ProgramFiles\GnuWin32\bin"
This updates PATH in the current command prompt. If you need to set it for other CMD windows, see How to persistently set a variable in Windows 7 from a batch file?
The second method is to have sed.exe in the current directory. The most obvious way to do that is to change directories.
cd C:\ProgramFiles\GnuWin32\bin
sed
Or you can copy it to your current directory.
copy C:\ProgramFiles\GnuWin32\bin\sed.exe .\
sed
(This works with sed.exe because it's a self-contained utility. Don't try this with a Windows application like excel.exe)
Finally, you can create a "redirect" somewhere in the current directory or the path.
>.\sed.bat echo C:\ProgramFiles\GnuWin32\bin\sed.exe %*
This creates a batch file in the current directory called sed.bat that calls the full sed.exe. You can drop this file into any directory in your PATH.
mklink .\sed.exe C:\ProgramFiles\GnuWin32\bin\sed.exe
This creates a symlink to the sed.exe in the current directory, much like a symlink in Unix or a shortcut in Windows.

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

.cmd file get relative path when running file on a server

I have a CMD file that contains following command:
START H:\Applications\MyStoreApp\Application.exe
The file is location in the H drive (this is a mapped shared drive) in the folder MyStoreApp.
I want to make my cmd to resolve it's location relatively like this:
START .\Application.exe
I'm getting an error now since it cannot resolve this on a remote host.
Is there anyway to solve this without having to enter the specific location of my file including the mapped network drive?
%~dp0 is the path to the actual script directory (%0 is the script itself).
Considering H:\Applications\MyStore\App\launcher.cmd
#echo "%~dp0Application.exe"
"H:\Applications\MyStoreApp\Application.exe"
Or, for heavy use :
#SET $root=%~dp0
#REM Remove the last backslash
#SET $root=%$root:~0,-1%
#START "App" "%$root%\App.exe" /config "%$root%\App.ini" /log "%$root%\App.log"
Quotes are required in case of spaces in folder name.
For more information about parameter extensions, see SS64.com.

Resources