Assign the directory of a file to a variable - batch-file

In CMD I can search a file with the following command:
DIR /S /B PROGRAM.EXE
If the file is found the result will be:
C:\Users\Dev\Desktop\Program.exe
I would like to get in the output just the directory without the file name C:\Users\Dev\Desktop\ to assign only the path to a variable.
Is there any way to do this at CMD?

Use a For loop like this:
For /F "Delims=" %A In ('Dir/B/S/A-D "Program.exe" 2^>Nul') Do #Echo=%~dpA
Double up the % in a batch file.
In a batch file, to set any matches as a variable use this structure:
#Echo Off
Set "i=0"
For /F "Delims=" %%A In ('Dir/B/S/A-D "Program.exe" 2^>Nul') Do (Set/A "i+=1"
Call Set "OnlyPath[%%i%%]=%%~dpA")
Set OnlyPath[
Timeout -1
Each match will be set as a different variable just to ensure that if more than one match is made you retrieve them all.

With a batch file, you can do someting like that :
#echo off
set "Working_Folder=%userprofile%\Desktop"
For /F "Delims=" %%F In ('Dir /B /S /A-D "%Working_Folder%\PROGRAM.exe" 2^>Nul') Do (
Set "MyFolder=%%~dpF"
)
Echo "%MyFolder%" & pause>nul

Related

file path is not getting assigned to variable in cmd

In cmd I want to assign the file path of a file say 'test.txt' to a variable name filePath, complete file location is not know, it is know only up to certain folder so I am using dir /b /a /s "test.txt". I am using below syntax :
set filePath=C:\Users\kadamr\AppData\Local\Test>dir /b /a /s "test.txt"
When I echo the value of filePath I get the syntax instead of filePath.
i.e : C:\Users\kadamr\AppData\Local\Test>dir /b /a /s "test.txt"
What I am missing ?
If this is always under the users's home directory, use the USERPROFILE variable as the base directory from which to search. Why did you use /a when no attribute is specified? Also, keep in mind that more than one test.txt file may exist somewhere under the base directory.
#ECHO OFF
SETLOCAL
SET "EXITCODE=1"
SET "BASEDIR=%USERPROFILE%\AppData\Local\Test"
FOR /F "delims=" %%A IN ('DIR /S /B "%BASEDIR%\test.txt" 2^>NUL') DO (
ECHO COMMAND: CALL doit.exe "%%~A"
IF ERRORLEVEL 1 (SET "EXITCODE=%ERRORLEVEL%" & GOTO TheEnd)
SET "EXITCODE=0"
)
:TheEnd
EXIT /B %EXITCODE%

Variable not Echoing in For Loop

I am trying to get the file names using a batch file from a folder but it just doesn't work.
I followed guidelines from here but for some reason this isn't returning anything at all when it should!
FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Desktop\UPD\*.txt') DO SET result=%%G
I also tried:
FOR /F "tokens=*" %%G IN (dir /b C:\Users\Desktop\UPD\*.txt') DO SET _result=%%~G
echo %_result% >> %~dp0Outputfile.txt
What I get is:
ECHO is on.
EDIT
Here is what I did so far now:
IF EXIST C:\Users\Nathanael\Desktop\UPD\*.txt (
echo file found >> %~dp0Outputfile.txt
chDIR C:\Users\Nathanael\Desktop\UPD\
dir *.txt /b >> %~dp0Outputfile.txt
FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Nathanael\Desktop\UPD\*.txt') DO SET result=%%G
echo %result% >> %~dp0Outputfile.txt
)
The output is:
file found
NewVHD.txt
random.txt
ECHO is on.
If the for /f returns multiple files the last one will overwrite the previous in the set
The way cmd.exe parses (code blocks) requires delayed expansion when a var is set and used inside the same code block as is the case with your if exist
So either avoid the code block with reversed logic
IF NOT EXIST "C:\Users\Nathanael\Desktop\UPD\*.txt" Goto :Eof or other label
Or (always indent code blocks to better keep track) :
Setlocal EnableDelayedExpansion
IF EXIST "C:\Users\Nathanael\Desktop\UPD\*.txt" (
echo file found >> %~dp0Outputfile.txt
chDIR "C:\Users\Nathanael\Desktop\UPD\"
dir "*.txt" /b >> %~dp0Outputfile.txt
FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Nathanael\Desktop\UPD\*.txt') DO SET result=%%G
echo(!result! >> %~dp0Outputfile.txt
)
It's a good habit to always enclose pathes in double quotes
to avoid echo is off messages use an other command separator than a space if the var is possibly empty (I used a ( here
FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Desktop\UPD*.txt') DO SET result=%%G
Make sure that the path is correct (for example, perhaps it should be c:\Users\YourName\Desktop\UPD*.txt where YourName is the user name)?

Batch - Returning full path from a dir /b

I am traversing folders on a drive, collecting file names with specific extensions, and building a string which is later used in a command line switch. When I find a qualifying file I need to know its full path as this is what is required by the command line. I currently use "%~dp0%%a\%%b" to build the full path, but I can see that may have limitations later on when the batch becomes more complex (e.g. it digs deeper into sub folders). I am hoping there is a way to replace "%~dp0%%a\%%b" with the path to the located file. Thank you:
#ECHO OFF
for /f "usebackq tokens=*" %%a in (`dir /b /a:d`) do (
pushd %%a
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%b in ('dir /b "*.E01" "*.L01" "*.AD1" 2^>nul') do (
SET EVIDENCE_STR=!EVIDENCE_STR! /e:"%~dp0%%a\%%b"
)
IF DEFINED EVIDENCE_STR (
ECHO !EVIDENCE_STR!
) ELSE (
ECHO No evidence files located in: %%a
)
endlocal
SET EVIDENCE_STR=
popd
)
PAUSE
Why do you need to create 2 loops, each running a dir command to find files? Why not just do for /R loop? Here is an example:
#echo off
set "files=*.E01 *.L01 *.AD"
for /R %%a in (%files%) do echo %%a
Simply use "Sub"-Option: /S of the DIR-Command:
Dir /B /S C:\*.jpg
cause it is that equivalent to:
#echo off
set "DirPath=C:\"
set "files=*.jpg"
for /R %DirPath% %%a in (%files%) do echo %%a
and so you should got the same result in each script.
The Problem: If you don't want any deep of SubDirectorys, you've to filter out these and so you may lose time - in both solutions.

return the name of a file using a batch script

I am trying to write a script that will search a given directory, find files ending in a .ready_go extension, rar them (saving the rar as the file name.rar) and then move onto the next file with the same extensions and do the same.
I am not having luck assigning a variable "filename" to the name of the file found in the loop. this variable will be used to name the rar archive
so far this is what i have:
set dSource=C:\users\admin\desktop\one
set dTarget=\\filesrv1\archives
set fType=*.ready_go
for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (
set filename=%%f
echo %filename%
echo %dsource%
pause
)
its echo'ing the dsources variable fine, but will not echo the filename variable.
I also tried just "echo %%f" which also did not work.
some help would be appreciated. thanks.
You need to use delayedexpansion since you are setting the varable inside the loop.
try this:
setlocal enabledelayedexpansion
set dSource=C:\users\admin\desktop\one
set dTarget=\\filesrv1\archives
set fType=*.ready_go
for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (
set filename=%%f
echo !filename!
echo %dsource%
pause
)

Partial path known..need to search for file type inside

There is a particular folder that begins with a name such as SS followed by random characters. The names would be different every time and the only thing we are sure of is the folder begins with SS. How do we look if this folder has a .txt file inside in batch programming.
An idea :
#echo off
for /f "delims=" %%a in ('dir /b/ad ^|find /i "SS"') do set $Dir=%%a
dir /b/a-d *.txt %$dir%>nul
if %errorlevel% equ 0 echo File(s) found in "%$DIR%"
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /ad "%sourcedir%\ss*" 2^>nul'
) DO (
FOR /f "delims=" %%h IN (
'dir /b /a-d "%sourcedir%\%%a\*.txt" 2^>nul'
) DO (
ECHO "%sourcedir%\%%a\%%h"
)
)
GOTO :EOF
should solve your problem - you need to change sourcedir to suit your system, obviously.
The code below check if the folder contain any .txt file:
#echo off
set "filePath="
for /D %%a in (SS*) do if exist "%%a\*.txt" do set "filePath=%%a"
if defined filePath echo File exists in folder %filePath%
If you want to check for a particular .txt file, just change *.txt by the appropriate name.

Resources