2016-10-14 62 views
2

我試圖編寫一個批處理腳本來查找與輸入字符串具有相同名稱的文件的所有路徑。現在它只能找到找到的第一個文件,而我不能想辦法讓它列出多個文件位置。我不是很有經驗,我需要一些幫助。使用單個字符串查找多個文件路徑

這是腳本代碼的一部分:

: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 
+0

'爲%%一個在(CDEFGHUW)做DIR/S/B% %a:\%filename%2> nul' - 夠短? – Stephan

+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 
+0

謝謝!但只解決了長度和效率問題...我仍然需要幫助與同名的多個文件... –

+0

不明白;每個發現都是獨一無二的(驅動器+路徑+文件名+擴展名)。如果你打算只使用文件名,你應該能夠找到有用的[這裏](http://stackoverflow.com/search?q=%5Bbatch-file%5D+copy+without+overwrite)。 – Stephan

+0

我希望腳本搜索計算機並列出每個遇到的文件具有相同的名稱,我希望能夠將這些文件的路徑放入不同的變量。例如,如果readme.txt是輸入,那麼我需要具有該特定名稱(readme.txt)的所有文件的所有路徑的列表,並且我想爲每個路徑設置變量,以便在此之後使用它。 –

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 
::*************************************************************************************** 
+1

它運作良好,謝謝!我只是不明白代碼中的一些東西。無論如何,我找到了一種通過使用循環並找到一個循環來獲得多個路徑的方法,並將其放入一個在找到下一個變量後發生變化的變量。效率不高,但也不是很複雜。 –

相關問題