Assign directory of a searched file to a CMD variable

Asked

Viewed 632 times

1

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 with the directory without the file name, ie just C:\Users\Dev\Desktop\ to assign only the path to a variable.

There’s a way to do that at the CMD?

  • Did the answer help you? Need more details?

  • Helped yes, but does not return the full path when the file is inside more than 2 folders, I will give a vote on your answer for the work you had, but I will publish the answer that solved my problem. Thank you very much.

2 answers

2

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
    )

0


I managed 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.