How to find a file without specifying folder/drive in bat or moon?

Asked

Viewed 1,161 times

0

How to find a file and save it in a variable?

I will use it for reading (this part is already solved), however, I want to locate without specifying folder/drive, for example .\Arquivo.txt.

I’ve tried that, only it doesn’t work when you’re reading...

I need this, since, if I would type the complete directory, it would work on my computer, however, in another would not be successful, since the folder/path/directories name, would not be the same.

How to do this in bat or moon?

1 answer

2

  • Update v2 Passing by parameter (lua.log) and insertion/input (lua.txt)...


inserir a descrição da imagem aqui


rem :: Abaixo descritivo das variáveis relativas aos arquivos:
rem :: ----------------------------------------------------------------------
rem :: Arquivo passado no argumento/parâmetro arquivo parâmetro
rem :: variável nome do arquivo via arquivo.bat lua.txt (%~1) = !_arg!
rem :: variável caminho drive/pasta e sem o nome do arquivo   = !_path_arg!
rem :: variável caminho drive/pasta/arquivo com nome completo = !_full_arg!
rem :: ----------------------------------------------------------------------
rem :: variáveis do arquivo passado no input via comando set /p:
rem :: variável nome do arquivo via arquivo.bat lua.log (%~1) = !_input!
rem :: variavel caminho drive/pasta e sem o nome do arquivo   = !_path_input!
rem :: variavel caminho drive/pasta/arquivo com nome completo = !_full_input!
rem :: ----------------------------------------------------------------------

Q395923.cmd


@echo off & setlocal enabledelayedexpansion 

2>nul >nul chcp 850 & title Q395923 & cls & echo/ 
for /f "tokens=*delims= " %%f in ('mountvol^|find /i ":\"')do set _drv=!_drv!, %%f
set "_drv=!_drv: =!" & echo/ & set /p "_arq= Favor informar o nome do arquivo: " & set _lf=^

<nul 2>nul & set "_msg=Definindo as variavei relativas ao arquivo" & cls & set "_input=%~1"
cls & set "_drv=!_drv:~1!" & set "_sys=%windir%\system32" & call :~[ && goto :?)

:^[
echo/!_lf!Procurando pelo arquivo %~1 nos drivers: !_drv!^... !_lf!
for /f "tokens=*delims= " %%f in ('!_sys!\mountvol^|find /i ":\"')do (
     for /f "tokens=*delims= " %%F in ('cd /d %%f ^& cd\ ^& !_sys!\where /r .\ "!_%%/!" 2^>nul') do (
         set "_%2=%%~nxF" && set "_path_%2=%%~dpF" && set "_full_%2=%%~fF" && echo/ !_msg! !_%%/!^...
         echo/!_lf! Arquivo: !_%%~/! !_lf! Caminho: !_path_%%/! &echo/ Completo: !_full_%%/! & exit /b))

:~[
(for %%/ in (arq,input)do call :^[ "!_%%/!" %%/) && exit /b 

:?)
echo/!_lf! As suas próximas açoes/comandos seguiram abaixo desta linha!!!.. 

  • Update based on the comments...
    Start of Update!

Since I understand your comments, then you will call bat passing these arguments:

[Arquivo.bat] [lua compilador(a variável do arquivo lua q também vou ter q achar)] [arq(a variável do arquivo txt)]

  • Therefore to the bat passing by arguments without passing the MD5:

 %0 = [Arquivo.bat]          
 %1 = [lua compilador(a variável do arquivo lua q também vou ter q achar)]
 %2 = [arq(a variável do arquivo txt)]
---------------------------------------------------------------------------
    %0          %1           %2   
Q395923.cmd "buscar_arq" "algum_txt"

  • The code in bat without hash/MD5, would be:

inserir a descrição da imagem aqui

@echo off && setlocal enabledelayedexpansion & title < nul && title Q395923 & echo/ 

set "_file=%~1" && set "_txt=%~2" & echo/ Procurando pelo arquivo: !_file!... & echo/

for /f %%f in ('%windir%\system32\mountvol^|find /i ":\"')do for /f %%F in ('cd /d "%%f" ^& cd\ ^& %windir%\system32\where /r .\ "!_file!" 2^>nul')do (
   set "_lua=%%F" && set "_pasta=%%~dpF" && goto :^)
          )

:^)
if "!_lua!/." == "/." (echo/ Arquivo "%~1" n localizado^!! & goto :eof) else (echo/  Arquivo: !_file! &echo/    Pasta: !_pasta!) 

rem:: variável !_lua! vai armazenar o caminho completo buscado no bat = %1 no bat
rem:: variável !_txt! vai armazenar o arquivo.txt, passado no comando = %2 no bat

  • Therefore to the bat passing by arguments with the MD5:

 %0 = [Arquivo.bat]          
 %1 = [lua compilador(a variável do arquivo lua q também vou ter q achar)]
 %2 = [arq(a variável do arquivo txt)]
 %3 = [Hash(MD5 do arquivo que busca encontrar)]
---------------------------------------------------------------------------
    %0          %1          %2                     %3
Q395923.cmd "buscar_arq" "algum_txt" "d3369d9f356dc5795a49ae2ecca5787a"


  • The code in bat with hash/MD5, would be:

inserir a descrição da imagem aqui

@echo off && setlocal enabledelayedexpansion & title < nul && title Q395923 & echo/ 

set "_file=%~1" && set "_txt=%~2" && set "_md5=%~3" & echo/ Procurando pelo arquivo: !_file!... & echo/

for /f %%f in ('%windir%\system32\mountvol^|find /i ":\"')do for /f %%F in ('cd /d "%%f" ^& cd\ ^& %windir%\system32\where /r .\ "!_file!" 2^>nul')do (
   set "_lua=%%F" && set "_pasta=%%~dpF" && for /f delims^=^ ^tokens^=* %%i in ('%windir%\system32\certutil -hashfile "!_file!" md5^|find /v ":"')do if "%%~i" == "!_md5!" goto :^)
          )

:^)
if "!_lua!/." == "/." (echo/ Arquivo "%~1" n localizado^!! & goto :eof) else (echo/  Arquivo: !_file! &echo/    Pasta: !_pasta! &echo/ Hash MD5: !_md5!) 

rem:: variável !_lua! vai armazenar o caminho completo buscado no bat = %1 no bat
rem:: variável !_txt! vai armazenar o arquivo.txt, passado no comando = %2 no bat
rem:: variável !_md5! vai armazenar o hash/MD5 do arquivo que desejas = %3 no bat

  • Update based on the comments...
    End of Update!

inserir a descrição da imagem aqui


Obs.: A computer search (all driver/folders/subfolders) will take...


To search for a file (archiv_target.ext), I suggest first getting the MD5 of the target file, to check whether the MD5 of the file found by bat be the even that searches, avoiding finding any file with the same name but its content differs from the desired file.


To obtain the MD5 with the certutil.exe use the code below, which will display on the screen the string of MD5 and will also move a copy to your Crtl+C, making it easy to paste into your file bat:

@for /f delims^=^ ^tokens^=* %%i in ('%windir%\system32\certutil -hashfile "c:\pasta\subpast\arquivo_alvo.ext" md5^|find /v ":"')do (
    @echo/%%i & echo/%%i|clip
    )

Obs.: Certiutil.exe already comes with Windows XP, 7, 8, 8.1, 10 and server versions.

To locate the file and not knowing what drive or folder this file might be in....

1) Use of command mountvol to list all drivers available in the system

2) Passing the exit from mountvol within a looping for, we can get the desired file with the command **where* in each drive and its sub-folders

3) Being found by the command where, we will pass the exit of this command to the with certutil and thus get the MD5 from the archive

4) Using a if, we can check if the found file has the sum MD5 same as the file buscado/desejado.

@echo off && setlocal enabledelayedexpansion & title < nul && title Q395923
set "_file=arquivo_alvo.ext" && set "_hash=d3369d9f356dc5795a49ae2ecca5787a"

for /f %%f in ('mountvol^|find /i ":\"')do for /f %%F in ('cd /d %%f ^& cd\ ^& where /r .\ "!_file!"')do >nul set "_lua=%%F" && (
     for /f delims^=^ ^tokens^=* %%i in ('%windir%\system32\certutil -hashfile "%%~F" md5^|find /v ":"')do if "%%~i" == "!_hash!" (
         set "_path_file=%%~dpF" & goto :^))
         )
echo/ Arquivo "!_file!" n localizado^!! & goto :eof 

:^)
echo/ Arquivo: "!_file!" &echo/   Pasta: "!_path_file!" & echo/    Hash: "!_hash!"


  • Without checking if the MD5 matches the filing cabinet you seek:

To locate a file without knowing the folder and being on the same drive:


cd\ & where /r "." "arquivo.ext"

Inside a bat using for:


@echo off & cd\ 

for /f %%f in (`where /r "." "arquivo.ext"`)do set "_path_file=%%~dpf"
echo/%_path_file%
  • and not being on the same drive

  • @Elonneto 1st code does this, considers q n knows which drive/folder, searches all drivers/folders.

  • ok vlw very obg msm

  • how do I move to a variable? and use for example like this: lua compilador(a variavel do arquivo lua q tambem vou ter q achar) arq(a variavel do arquivo txt)

  • I have tended n... this in bat or moon?

  • bat, eh q o usuario ai pass a file, ai i have q find the moon file and that file q it will pass

  • yes, but I already know the name of the program on the moon, I want to pass its path to a variable and then use to open it

  • ta blz, give me your Discord? that so agent speaks for there I have to leave Daq 1 hr

  • ue Discord, sabe oq eh nn?

Show 4 more comments

Browser other questions tagged

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