Windows批处理脚本在子文件夹中搜索图像

时间:2018-03-09 00:18:19

标签: batch-file

我需要找到文件夹和所有子文件夹中的.txt文件列表中的所有文件

然后将文件移动到新位置

我已经尝试过/ r命令来搜索子文件夹,但似乎无法正常工作

当所有文件都在主要文件中时似乎工作正常但我无法获取脚本来搜索子文件夹

imageslist.txt文件的布局

image1*.png     1 
image2*.png     4 
Image 6*.png    2
如果我有代码

,请在下面

    @echo off 
    set Source=C:\Users\Desktop\Folder1\IMAGES
    set Target=C:\Users\Desktop\IMAGES MOVED
    set FileList=C:\Users\Desktop\imageslist.txt
    echo.
    if not exist "%Source%" echo Source folder "%Source%" not found & goto Exit 
    if not exist "%FileList%" echo File list "%FileList%" not found & goto Exit
    if not exist "%Target%" md "%Target%" 

    for /F "usebackq tokens=1-2" %%a in ("%FileList%") do call :CopyFile "%%a" %%b
    for /R %%f in ("%FileList%") do call :CopyFile "%%a" %%b


    :Exit
    echo.
    echo press the Space Bar to close this window.
    pause > nul
    exit /b 0
:CopyFile
:: first argument  = filename
:: second argument = number of copies

REM A little trick that will put limit on 0 if second argument is empty or not a number
set secondarg=%~2
set /a limit=secondarg

REM if limit is invalid (not strict positive), exit the function
IF %limit% LEQ 0 (
    echo Invalid number of copies
    exit /b 1
)
IF NOT EXIST "%Target%\%~1" (
    copy "%Source%\%~1" "%Target%"
    IF %limit% LEQ 1 exit /b 0
    set /a limit-=1
)

REM File already exists: search correct index for filename
set index=0
set "targetfile=%target%\%~n1"
set file_ext=%~x1

:following
set /a index+=1
Rem if file with index %index% already exists, go back to get following index
IF exist "%targetfile%(%index%).%file_ext%" goto :following

Rem we have the correct index, now we can copy
set /a limit=index+limit-1
FOR /L %%g IN (%index%,1,%limit%) DO copy "%Source%\%~1" "%targetfile%(%%g).%file_ext%"
exit /b 0

0 个答案:

没有答案