标题: [文件操作] [分享]批处理从每个文件夹只提取一个图片到新文件夹 [打印本页]
作者: Batcher 时间: 2022-11-9 16:47 标题: [分享]批处理从每个文件夹只提取一个图片到新文件夹
【问题描述】
请教下,可以帮忙写个代码,让每个文件夹只提取一个图片,复制在新建的文件夹里面吗?
作者: Batcher 时间: 2022-11-9 16:48
【解决方案】
test_1.bat 请参考Q-04把bat文件保存为ANSI编码:
https://mp.weixin.qq.com/s/Koje4FufWxWBs7ioDy_LJA- @echo off
- cd /d "%~dp0"
- set "NewFolder=%~dp0..\新建文件夹"
- if not exist "%NewFolder%\" (
- md "%NewFolder%"
- )
-
- for /f "delims=" %%i in ('dir /b /ad') do (
- echo 正在处理文件夹:%%i
- pushd "%%i"
- call :MoveFile
- popd
- )
- pause
- goto :eof
-
- :MoveFile
- for /f "delims=" %%i in ('dir /b /a-d *.jpg') do (
- echo 复制文件 "%%i" 到文件夹 "%NewFolder%"
- copy "%%i" "%NewFolder%"
- goto :eof
- )
复制代码
作者: Batcher 时间: 2022-11-11 14:38
【问题2】
可以再稍微改2个点吗:①新建的文件夹可以放在文件夹里面,不要放在桌面;②可以把每个文件夹含有【正面】这个字眼的图片单独复制在新建的文件夹里面
【方案2】- @echo off
- cd /d "%~dp0"
- set "NewFolder=BatTest文件夹"
- if not exist "%NewFolder%\" (
- md "%NewFolder%"
- )
-
- for /f "delims=" %%i in ('dir /b /ad ^| findstr /v /x "%NewFolder%"') do (
- echo 正在处理文件夹:%%i
- pushd "%%i"
- call :CopyFile
- copy /y "*正面*.jpg" "..\%NewFolder%"
- popd
- )
- pause
- goto :eof
-
- :CopyFile
- for /f "delims=" %%i in ('dir /b /a-d *.jpg') do (
- echo 复制文件 "%%i" 到文件夹 "%NewFolder%"
- copy /y "%%i" "..\%NewFolder%"
- goto :eof
- )
复制代码
作者: Batcher 时间: 2022-11-11 15:31
【需求3】
可以用了;但是现在把每个文件夹内每一张图片和含有正面的 都提取了,可以再微调吗,就是分开一种是每一张图片,另外一种是 含有正面的
【方案3】- @echo off
- cd /d "%~dp0"
- set "NewFolder1=BatTest一张"
- set "NewFolder2=BatTest正面"
- if not exist "%NewFolder1%\" (
- md "%NewFolder1%"
- )
- if not exist "%NewFolder2%\" (
- md "%NewFolder2%"
- )
-
- for /f "delims=" %%i in ('dir /b /ad ^| findstr /v /x /c:"%NewFolder1%" /c:"%NewFolder2%"') do (
- echo 正在处理文件夹:%%i
- pushd "%%i"
- call :MoveFile
- copy /y "*正面*.jpg" "..\%NewFolder2%"
- popd
- )
- pause
- goto :eof
-
- :MoveFile
- for /f "delims=" %%i in ('dir /b /a-d *.jpg') do (
- echo 复制文件 "%%i" 到文件夹 "%NewFolder1%"
- copy /y "%%i" "..\%NewFolder1%"
- goto :eof
- )
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |