Outcome is to change to directory after searching for the file via DIR through cmd
Location of file is C:\Folder
is the following code possible?
set /a variable= dir document.doc /s /p
cd %%variable%%
Change directory to C:\Folder
Outcome is to change to directory after searching for the file via DIR through cmd
Location of file is C:\Folder
is the following code possible?
set /a variable= dir document.doc /s /p
cd %%variable%%
Change directory to C:\Folder
try this:
Run in a command prompt window
set /?
and read the output help carefully from first to last page.set /A
is for evaluating an arithmetic expression. So the string afterset /A
is interpreted by Windows command line interpreter as arithmetic expression (formula).This command line outputs on execution the error message
missing operator
becausedir
is interpreted as a variable name and alsodocument.doc
both most likely not existing and therefore replaced by0
on evaluating the expression. Butcmd.exe
expects an operator between those two environment variable names and as there is none like/
before variables
, the error message is output.It is not possible to assign possible multi-line output of a command line like
dir document.doc /s /p
to an environment variable with command SET.In a batch file you can use this code:
Command FOR searches for any non hidden file matching the pattern
document*.doc
in current directory and all non hidden subdirectories. A wildcard character like*
or?
must be specified to run a search for a file. If a file is found, the command CD is executed to change to directory of the file and the loop is exited with a jump to a label.Another solution to really search only for file
document.doc
:This example shows how to run a command line like
dir "document*.doc" /A-D /B /S 2>nul
in a separate command process started by FOR withcmd.exe /C
with capturing all output lines written to handle STDOUT while in this case redirecting an error message written to handle STDERR to device NUL to suppress it.The captured output is next processed by FOR line by line with skipping with default options all empty lines and lines starting with a semicolon and splitting up each line into substrings (tokens) using space and tab as delimiters with assigning only first substring to specified loop variable
I
. This line splitting behavior is disabled by using"delims="
which defines an empty list of delimiters and so no line splitting is possible anymore. In this case it is impossible that a line output by DIR starts with;
and so we don't need to care abouteol
(end of line) option.%%~dpI
expanding to just drive and path of current file name with path can be also assigned to an environment variable for example withset "FilePath=%%~dpI"
. And the environment variableFilePath
can be referenced in rest of the batch file either with immediate expansion using%FilePath%
or with delayed expansion using!FilePath!
enclosed the entire argument string containing this variable reference in double quotes for working also for file paths containing a space or one of these characters:&()[]{}^=;!'+,`~
Both batch codes always changes to first found
document*.doc
respectivelydocument.doc
file and ignoring all other files matching the pattern respectively with same name perhaps also found by FOR or DIR in other directories in searched directory tree. The first solution is faster on a large directory tree needs to be searched for the file. But first solution ignores hidden subdirectories and can change to a directory containing for exampledocument_1.doc
instead ofdocument.doc
.For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cd /?
dir /?
echo /?
for /?
goto /?
pause /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of
2>nul
. The redirection operator>
must be escaped with caret character^
on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embeddeddir
command line in a separate command process started in background.