find multiple files paths with single string

2019-05-22 21:02发布

问题:

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:

:start
    cls
    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
  )
)
:notfound
    cls
    echo Enter file name with extension:
    echo %filename%
    echo File Not Found!
    ping localhost -n 4 >nul
    goto start

:break
    if "%datapath:~-1%"=="\" set datapath=%datapath:~,-1%
    cls
    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.

input:  
readme.txt

output:  
3 files found  
C:\folder\folder\readme.txt  
C:\folder\folder\folder\readme.txt  
D:\folder\readme.txt

回答1:

@echo off
set filename=readme.txt
for %%a in (C D E F G H U W) do (
  for /f "tokens=*" %%b in ('dir /s /b "%%a:\%filename%"') do (
    echo you can do something here with  %%~nxb in %%~dpb
    echo    full name: %%b
  )
)

I see no need to set the filenames to variables, as you can process them inside your loop. But if you really need them (for some reason) in variables:

@echo off
setlocal enabledelayedexpansion
set filename=readme.txt
set count=0
for %%a in (C D E F G H U W) do (
  for /f "tokens=*" %%b in ('dir /s /b "%%a:\%filename%" 2^>nul') do (
    set /a count+=1
    set _file[!count!]=%%b
  )
)
set _file


回答2:

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(
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"
   )
)

:Display
for /L %%i in (1,1,%Count%) do (
    cls
    Title Please wait a while ... Searching for "%FileName%" on "!Drive[%%i]!\"
    echo(
    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
::***************************************************************************************
:AskQuestion
cls & Mode con cols=100 lines=5
echo(
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" (
        Exit
)
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"
    )
)
::***************************************************************************************
:loop
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
::***************************************************************************************