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.
Related
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.
I've been stuck on this for awhile and could use some help.
I'm trying to copy a large folder from a mapped network drive (A:) onto my local PC. I also need to exclude a subdirectory on that drive path called "Images". My current code (backup.bat) is below:
cd %HOMEPATH%\Desktop\%mydate%
xcopy "A:\PROGRA~2\QuadTech" 121\ /e /EXCLUDE:"A:\PROGRA~2\QuadTech\INSPEC~1\Images\"
Error I keep getting:
I've tried shortening the path with "dir /x" and I am sure the path name is correct. Also note that I need quotations as there are spaces in the PATH name.
Why am I getting errors when trying to Exclude this directory??
ANSWERED
I now have my Exclude statement point to my desktop where it reads a list of strings in a txt file.
xcopy "A:\PROGRA~2\QuadTech" 121\ /e /EXCLUDE:C:\Users\QuadTech\Desktop\excldelist.txt
Txt file contents:
\Images\
This is happening because the /EXCLUDE option does not specify files to exclude.
It specifies files containing lists of files to exclude.
More info by typing xcopy /?, though I am sure you know that.
(I know, I missed it too in the beginning; sometimes it is just a matter of having a second pair of eyes.)
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.
I would like to copy *.html files from a directory path "C:\abc\xyz\" on my system. I have created a shared folder on my system and I am having the domain admin rights into my user account. I have created a .bat file and commands inside the .bat file is as follows:
pushd \\Target-Hostname\c$\abc\xyz\
xcopy *.html \\Shared-Folder-Path\ /s/e/h/q
popd
However, I get an error "Invalid drive specification". May I know why this error arises? How can alter the command in a .bat file? There are around 100 systems from which I need to copy *.html files (Note: file path on remote systems will remain the same).
Can I copy *.html files by using a VBScript that will execute on a network having domain setup?
UNC paths consist of at least the names of a host and a share on that host, optionally followed by a path below that share:
\\server\share[\sub\folder]
An UNC path \\share\ is invalid, which is what xcopy is telling you.
Also, if you're copying from remote systems to a local folder you don't need an UNC path for the destination in the first place. Simply use the local path:
xcopy \\Target-Hostname\c$\abc\xyz\*.html C:\local\folder /s/e/h/q
Of course you can do the same in VBScript:
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile "\\Target-Hostname\c$\abc\xyz\*.html" "C:\local\folder\"
but just changing the language wouldn't have addressed the misconception in your approach.
I'm not familiar with this programming language,in PHP it's getcwd().
Not entirely sure I understand your question but:
The current directory is held in the pseudo-variable %CD%
Getting the directory that the Batch file is located in can be done as follows:
%~dp0
%0 is the name of the batch file. ~dp gives you the drive and path of the specified argument.