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:
for /f "delims=" %%# in ('dir document.doc /s /b') do (
set "new_dir=%%~dp#"
)
cd /d "%new_dir%"
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 after set /A
is interpreted by Windows command line interpreter as arithmetic expression (formula).
set /a variable= dir document.doc /s /p
This command line outputs on execution the error message missing operator
because dir
is interpreted as a variable name and also document.doc
both most likely not existing and therefore replaced by 0
on evaluating the expression. But cmd.exe
expects an operator between those two environment variable names and as there is none like /
before variable s
, 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:
@echo off
for /R %%I in ("document*.doc") do cd /D "%%~dpI" & goto FoundFile
echo Could not find any document*.doc in %CD% or its subdirectories.
pause
goto :EOF
:FoundFile
echo Found a file document*.doc in directory %CD%.
pause
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
:
@echo off
for /F "delims=" %%I in ('dir "document*.doc" /A-D /B /S 2^>nul') do cd /D "%%~dpI" & goto FoundFile
echo Could not find file document.doc in %CD% or its subdirectories.
pause
goto :EOF
:FoundFile
echo Found file document.doc in directory %CD%.
pause
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 with cmd.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 about eol
(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 with set "FilePath=%%~dpI"
. And the environment variable FilePath
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
respectively document.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 example document_1.doc
instead of document.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 embedded dir
command line in a separate command process started in background.