1

I am trying to create a batch file: The batch file will locate the path of executable first. Then, the path will be stored in a variable for later use.

This is my code:

@echo off
setlocal
set directoryName=dir/s c:\ABCD.exe
rem run command
cmd /c %directoryName%
pause 
endlocal

The command prompt does return me with the executable's path but the path is not stored in the variable. Why is it so?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 2
    [The Windows command prompt is *NOT* a DOS prompt!](https://scalibq.wordpress.com/2012/05/23/the-windows-command-prompt-is-not-a-dos-prompt/) – aschipfl Mar 28 '18 at 08:23
  • Possible duplicate of [How to set commands output as a variable in a batch file](https://stackoverflow.com/questions/6359820/how-to-set-commands-output-as-a-variable-in-a-batch-file) – aschipfl Mar 28 '18 at 08:27
  • The executable your are searching for by searching for a file on entire drive `C:` is perhaps installed always well registered according to Microsoft's guidelines for [Application Registration](https://msdn.microsoft.com/en-us/library/windows/desktop/ee872121.aspx). In this case it would not be necessary at all to search all drives of a hard disk for the executable. Many applications are correct registered making it possible to use them from command line with command `start` without path, see [Where is “START” searching for executables?](https://stackoverflow.com/a/27386403/3074564) – Mofi Mar 28 '18 at 17:39

4 Answers4

0

Lets walk through your code

set directoryName=dir/s c:\ABCD.exe

This fills the variable directory name with the value dir/s c:\ABCD.exe.

cmd /c %directoryName%

This executes the command in directoryname. There is no line in your code that saves the files location to a variable.


Extracting the path of a file can be done as follows

@echo off
setlocal
set executable=c:\location\ABCD.exe
FOR %%A IN ("%executable%") DO Set executablepath=%%~dpA
echo executablepath: %executablepath%
pause 
endlocal

%executablepath% will contain c:\location\

Kevin Verstraete
  • 1,363
  • 2
  • 13
  • 13
0

The value that you are assigning to directoryname is dir /s c:\abc.exe.

this value is then substituted for %directoryname% in your cmd line, which executes the command dir/s..., showing you the location(s) of abc.exe in the familiar dir format.

If what you want is just the directoryname in directoryname, then you need

for /f "delims=" %%a in ('dir /s /B /a-d "c:\abc.exe"') do set "directoryname=%%~dpa"

which will first execute the dir command, then process each line of output from that command and assign it in its entirety to %%a.

The dir command shown would "display" the matching names found in the nominated directory (c:\) and its subdirectories (/s) in basic form (/b) - that is, names only, no size or date or report-headers or report-footers, and a-d without directorynames (should they match the "mask" abc.exe)

The delims= option to the for /f command instructs that the entire line as output by the command in single-quotes, be assigned to %%a.

When the result is assigned to the variable directoryname, only the Drive and Path parts are selected by using the ~dp prefix the the a.

Note that only the very last name found will be assigned to the variable as any earlier assignment will be overwritten by a succeeding assignment.

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

Reading your question, it appears that you're not really wanting to save the path of the executable file at all, but the file name complete with it's full path:

I prefer the Where command for this type of search, this example searches the drive in which the current directory resides:

@Echo Off
Set "mPth="
For /F "Delims=" %%A In ('Where /R \ "ABCD.exe" /F 2^>Nul'
) Do Set "mPth=%%A"
If Not Defined mPth Exit /B
Rem Rest of code goes here

The variable %mPth% should contain what you need. I have designed it to automatically enclose the variable value in doublequotes, if you wish to not have those, change %%A on line 4 to %%~A. If the file is not found then the script will just Exit, if you wish it to do something else then you can add that functionality on line 5.

Note: the code could find more than one match, if it does it will save the variable value to the last one matched, which may not be the one you intended. A robust solution might want to include for this possibility.

Edit (this sets the variable, %mPth% to the path of the executable file only)

@Echo Off
Set "mPth="
For /F "Delims=" %%A In ('Where /R \ "ABCD.exe" /F 2^>Nul'
) Do Set "mPth=%%~dpA"
If Not Defined mPth Exit /B
Set "mPth=%mPth:~,-1%"
Rem Rest of code goes here
Compo
  • 36,585
  • 5
  • 27
  • 39
  • The code works well! Thanks for your input. By the way, if I was looking for the path without the filename, can I use the same command (the where command)? – YongShun Teo Apr 03 '18 at 02:12
  • @YongShunTeo, I've added an Edit section regarding your latest comment. – Compo Apr 03 '18 at 02:43
0

This may or may not be what you are looking for. This script searches through the PATH variable and looks for files that have and extension in the PATHEXT variable list.

@SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
@SET EXITCODE=1

:: Needs an argument.

@IF "x%1"=="x" (
    @ECHO Usage: %0 ^<progName^>
    GOTO TheEnd
)

@set newline=^


@REM Previous two (2) blank lines are required. Do not change!

@REM Ensure that the current working directory is first
@REM because that is where DOS looks first.
@PATH=.;!PATH!

@FOR /F "tokens=*" %%i in ("%PATH:;=!newline!%") DO @(
    @IF EXIST %%i\%1 (
        @ECHO %%i\%1
        @SET EXITCODE=0
    )

    @FOR /F "tokens=*" %%x in ("%PATHEXT:;=!newline!%") DO @(
        @IF EXIST %%i\%1%%x (
            @ECHO %%i\%1%%x
            @SET EXITCODE=0
        )
    )
)

:TheEnd
@EXIT /B %EXITCODE%

Note that this may find multiple executables. It may also find multiple types of executables. The current directory is also included first since that is what the shell, cmd.exe, does.

M:>whence wc
.\wc.BAT
.\wc.VBS
C:\Users\lit\bin\wc.BAT
C:\Users\lit\bin\wc.VBS
lit
  • 14,456
  • 10
  • 65
  • 119