How to locate a file without specifying folder / drive in bat or lua?

How to locate 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, as for example .\Arquivo.txt.

Already tried this, only it does not work when it will do the reading...

I need this, since if I were to enter the full directory, it would work on my computer, but on another one it would not succeed, since the folder name/path / directories, they wouldn't be the same.

How to do this in bat or moon ?

Author: It Wasn't Me, 2019-07-08

1 answers

  • update v2 passing parameter (Moon.log) and insert / input (lua.txt) ...


insert the description of the image here


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 the Update!

Whereas I understood yours comments, then you will call the 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)]

  • carrying to the bat passing 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"

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

insert the description of the image here

@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

  • carrying to the bat passing 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"


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

insert the description of the image here

@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!

insert the description of the image here


Obs.: a search on the computer (all drivers/folders/subfolders) will take...


To search for a file (_alvo file.ext), I suggest first getting the MD5 from the Target file, to check if the MD5 of the file found by the bat is the same that you are looking for , avoiding finding some file with the same name but its content differs from the desired file.


{[47]) To obtain MD5 with certutil.exe please use the code below is what will be displayed on the screen string the MD5 it 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 locates the file and not knowing on which drive or folder this file may be....

1) use the command mountvol to list all the drivers available on the system

2) passing the output of mountvol inside a looping for, we can search or desired file with command * * where * on each drive and its subfolders

3) being found by the command where, let's pass the output of this command to the com certutil and so we get the MD5 of file

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 hits the file you are looking for:

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%
 2
Author: It Wasn't Me, 2019-07-18 20:35:53