2016-10-14 4 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

'에 대한 %% A의 (CDEFGHUW) 할 DIR/s의/ㄴ % % 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

이해가 안돼; 모든 발견은 고유합니다 (drive + path + filename + extension). 파일 이름 만 사용하려는 경우 도움이되는 [여기] (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

좋은 결과를 얻었습니다. 감사합니다! 나는 코드의 일부를 이해하지 못한다. 어쨌든 루프를 사용하여 여러 경로를 구하는 방법을 찾았고 그 중 하나를 찾아 다음 변수를 찾은 후 변경되는 변수 하나에 넣었습니다. 매우 효율적이지는 않지만 매우 복잡하지는 않습니다. –

관련 문제