Assign directory of a searched file to a CMD variable

0

In batch I can search a file with the following command:

C:\>DIR /S /B PROGRAMA.EXE

And if the file is found it will return like this:

  

C: \ Users \ Dev \ Desktop \ Program.exe

I would like to be able to get in the result only the directory without the file name, or just C:\Users\Dev\Desktop\ to be able to only assign the path to a variable.

How can I do this in CMD?

    
asked by anonymous 27.01.2017 / 20:22

2 answers

0

I was able to solve the problem with this code:

Option to assign all directories to different variables:

@Echo Off
C:
cd /
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

Option to assign the last directory to a variable:

@echo off
set "Working_Folder=C:"
For /F "Delims=" %%F In ('Dir /B /S /A-D "%Working_Folder%\PROGRAM.exe" 2^>Nul') Do (
    Set "MyFolder=%%~dpF"
)
Echo "%MyFolder%" & pause>nul
    
30.03.2017 / 22:03
1

I suggest creating a batch mydir.bat :

@echo off
    set MASK=*.java
    for /F %%i in ('dir %MASK% /S /B' ) do (
rem nome de arquivo e extensão (arquivo.txt)
       set FON=%%~nxi
rem extensão (.txt)
       set EXT=%%~xi
rem nome do arquivo (arquivo)
       set PAT=%%~ni
rem unidade e diretório (c:\tmp\)
       set PAT=%%~dpi
rem Imprime localização do arquivo     
       echo %%~dpi
    )
    
13.02.2017 / 16:42