使用单个字符串查找多个文件路径

时间:2016-10-14 16:59:21

标签: shell batch-file

我尝试编写一个批处理脚本,找到与输入字符串同名的文件的所有路径。现在它只能找到找到的第一个文件,我想不出一种让它列出多个文件位置的方法。我不是很有经验,我需要一些帮助。

这是脚本代码的一部分:

: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%
    --- || ---

我希望脚本能够搜索计算机并列出每个遇到的具有相同名称的文件,并且我希望能够将这些文件放入'进入不同变量的路径 例如,如果readme.txt是输入,那么我想要具有该特定名称(readme.txt)的所有文件的所有路径的列表,并且我想为每个路径设置变量,因此我可以在之后使用它。

input:  
readme.txt

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

2 个答案:

答案 0 :(得分:0)

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

我认为没有必要将文件名设置为变量,因为您可以在循环中处理它们。但如果你真的需要它们(出于某种原因)变量:

@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

答案 1 :(得分:0)

您可以尝试使用以下代码:

@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
::***************************************************************************************