Daniel F. Daniel F. - 1 year ago 66
Bash Question

find multiple files paths with single string

I tried to write a batch script that find all the paths of files that have the same name as the input string. right now it can find only the first file found, and i cant think of a way to make it list multiple files locations. I am not very experienced and I need some help.

this is part of the script code:

echo Enter file name with extension:
set /p filename=
echo Searching...

for %%a in (C D E F G H U W) do (
for /f "tokens=*" %%b in ('dir /s /b "%%a:\%filename%"') do (
set file=%%~nxb
set datapath=%%~dpb\
::the path of the file without the filename included "C:\folder\folder\"
set fullpath=%%b
::the path of the file with the filename included "C:\folder\folder\file"
goto break
echo Enter file name with extension:
echo %filename%
echo File Not Found!
ping localhost -n 4 >nul
goto start

if "%datapath:~-1%"=="\" set datapath=%datapath:~,-1%
echo 3 %filename% found
echo %fullpath1%
echo %fullpath2%
echo %fullpath3%
--- || ---

I want the script to search the computer and list every encountered files with the same name and I want to be able to put those files' paths into different variables.

For example, if readme.txt is the input, then I want the list of all the paths of all the files with that specific name (readme.txt) and I want to set variable for each path so I can use it after that.


3 files found

Answer Source

You can try with this code :

@echo off
Title Searching for the path with the same file name
Mode con cols=80 lines=3 & Color 9E
SET /a Count=0
set /a cnt=1
set "FileName=Readme.txt"
set "Report=%~dp0Report.txt"
set "Folder2Copy=%~dp0Readme_Folder"
set "Result2Copy=%~dp0Result2Copy.txt
If exist %Folder2Copy% RD /S /Q %Folder2Copy%
If Exist %Report% Del %Report%
If Exist %Result2Copy% Del %Result2Copy%
Echo                Searching for the path with the same file name
Rem Looking for fixed drives and store them into variables
SETLOCAL enabledelayedexpansion
For /f "skip=1" %%a IN ('wmic LOGICALDISK where driveType^=3 get deviceID') DO (
  for /f "delims=" %%b in ("%%a") do (
     SET /a "Count+=1"
     set "Drive[!Count!]=%%b"

for /L %%i in (1,1,%Count%) do (
    Title Please wait a while ... Searching for "%FileName%" on "!Drive[%%i]!\"
    echo          Please wait a while ... Searching for "%FileName%" on "!Drive[%%i]!\" 
    Call :FindPathFile !Drive[%%i]!\ %FileName% >> %Report%
Start "" %Report%
Goto :AskQuestion
:FindPathFile <Location> <FileName>
Where.exe /r %1 %2
Goto :eof
cls & Mode con cols=100 lines=5
echo Did you want to make copy of all files found as name "%FileName%"
echo saved on "%Report%" ? (Y/N) ?
set /p "Input="
If /I "%INPUT%"=="Y" (
    for /f "delims=" %%i in ('Type "%Report%"') do (
        Call :MakeCopy "%%~i" "%Folder2Copy%\"
Call :Explorer "%Folder2Copy%\" & exit
If /I "%INPUT%"=="N" (
Goto :eof
:MakeCopy <Source> <Target>
If Not Exist "%~2\" MD "%~2\" (
    if not exist "%2\%~n1" ( 
    echo copying "%~1" to "%~2"
    copy /N /B "%~1" "%~2" >>%Result2Copy% 2>&1
    ) else ( 
        call :loop "%~1" "%~2"
set "fname=%2\%~n1(%cnt%)%~x1"
if exist "%fname%" set /a cnt+=1 && goto :loop
copy "%~1" "%fname%"
exit /b
:Explorer <file>
explorer.exe /e,/select,"%~1"
Goto :EOF
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download