I may be confusing current directory with working directory but regardless I am trying to make a batch file that runs the tree
command from the folder it's currently in.
I have a folder called "Network_Switch_Backup" with a script, some other items and a subfolder called "backups".
This has worked for testing purposes:
tree U:\Desktop\Network_Switch_Backup\backups /f
But as I will be zipping it and sending it to different computers and users clearly this isn't practical since they could put the folder anywhere besides the desktop.
I've looked at other threads and amongst other things I tried, this looked the most promising (but still did not work):
tree %cd%\backups /f
However tree %cd%\downloads /f
works perfectly fine when running from cmd so I'm just a bit confused.
It is advisable in batch files to reference executables to run with full qualified file name which means full path + file name + file extension, especially if the storage location of the executable is well known. That makes the batch file independent on the values of the environment variables
PATHEXT
andPATH
.PATH
is quite too often not correct defined on many computers running Windows.The full qualified name of TREE is
%SystemRoot%\System32\tree.com
.Environment variable
SystemRoot
is not defined as system or user environment variable likePATH
andPATHEXT
, but is nevertheless defined on execution of a batch file. So it is very safe to use this Windows environment variable.What is the reason for '...' is not recognized as an internal or external command, operable program or batch file? explains very detailed how Windows command processor finds executables and scripts not specified with full qualified file name on command prompt or in a batch file.
There are two directories which need to be taken into account on coding an application or script:
For example a batch file is stored in directory
"%UserProfile%\Desktop"
. Windows sets the directory of the batch file as current directory on simply double clicking the batch file on user's desktop. Therefore the script directory is the current directory on execution of the batch file. But if this batch file is executed by right clicking on the batch file and left clicking on context menu option Run as administrator, the batch file stored in"%UserProfile%\Desktop"
is usually executed from directory%SystemRoot%\System32
depending on user account permissions and on user account control setting of current user. The reason for making%SystemRoot%\System32
the current directory before executing the batch file is explained in detail by answer on Why does 'Run as administrator' changes (sometimes) batch file's current directory?The MSDN article Naming Files, Paths, and Namespaces explains in detail how to reference files and folders relative to current directory. The current directory is simply not included in file/folder argument string or alternatively represented by
.\
.In case of drive and path of current directory needs to be known, for example to output it on running a batch file, there is the dynamic environment variable
CD
(short for Current Directory).%CD%
or!CD!
with delayed expansion enabled expands to full path of current directory which does not end with a backslash, except the current directory is the root directory of a drive. The help output on running in a command prompt windowset /?
explains dynamic environment variableCD
briefly on last help page.Batch files need to be designed very often to reference files or folders with a path relative to directory of the batch file. In this case it is not advisable to use the current directory because the current directory can be really any directory.
The help output on running
call /?
in a command prompt window explains how arguments of a batch file can be referenced from within a batch file. Argument 0 is always the batch file itself.%~dp0
references drive and path of the batch file. This file path ends always with a backslash, but of course can contain a space or one of these characters&()[]{}^=;!'+,`~
which require entire file/folder argument string to be enclosed in double quotes. So%~dp0
must be concatenated in a batch file without an additional backslash after that string and the entire argument string must be enclosed in double quotes to work safely.So the command line to use to reference the subdirectory
Backups
in directory of the batch file independent on which directory is the current directory is:A bug of
cmd.exe
explained in detail on What is the reason for batch file path referenced with %~dp0 sometimes changes on changing directory? should be taken into account on using%~dp0
in a batch file.