无法遍历文件名中带有空格的文件、Windows 批处理文件和图像魔法
Posted
技术标签:
【中文标题】无法遍历文件名中带有空格的文件、Windows 批处理文件和图像魔法【英文标题】:Cant loop over files with spaces in the filename, windows batch file and image magick 【发布时间】:2019-02-07 17:09:30 【问题描述】:我正在尝试遍历一堆文件夹,创建子文件夹然后循环文件,使用 imagemagick 转换它们并将它们放入新的子文件夹并重命名它们。有些文件的名称中有空格并导致错误...我该如何解决这个问题?
错误信息:
convert: unable to open image 'photo': No such name or directory @error/blob.c/OpenBlob/3489. convert: no decode delegate for this image format '' @ error/constitute.c/ReadImage/554.**
文件夹结构如下所示... 文件夹结构如下所示...
batch_file.bat
folder_a
...photo 1.jpg
...photo1.jpg
folder_b
...photo 1.jpg
...photo2.png
我希望它以这样的方式结束
batch_file.bat
folder_a
...300
......1.webp
......1.jpg
......2.webp
......2.jpg
...600
......1.webp
......1.jpg
......2.webp
......2.jpg
...photo 1.jpg
...photoC.jpg
folder_b
...300
......1.webp
......1.jpg
......2.webp
......2.jpg
...600
......1.webp
......1.png
......2.webp
......2.png
...photo 1.jpg
...photoA.png
如果可能,我想将文件重命名为 1.jpg、1.webp、2.jpg、2.webp 等...
批处理文件如下所示...
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
%~d1
CD "%~p1"
SET FOLDERS=300 600
FOR /D %%r IN (*) DO (
CD %%r
ECHO In Folder: %%r
FOR %%f IN (%FOLDERS%) DO (
MD %%f
ECHO In Folder: %%f
PAUSE
FOR %%a IN (*.jpg, *.png) DO (
convert %%a -sampling-factor 4:2:0 -strip -quality 85 -interlace JPEG -colorspace RGB -resize %%f %%f\%%a
ECHO Converting File: %%a
mogrify -format webp %%f\%%a
PAUSE
)
)
CD ..
)
【问题讨论】:
【参考方案1】:要处理带空格的文件名,请引用它们。例如你的命令
convert %%a -sampling-factor 4:2:0 -strip -quality 85 -interlace JPEG -colorspace RGB -resize %%f %%f\%%a
应该改为
convert "%%a" -sampling-factor 4:2:0 -strip -quality 85 -interlace JPEG -colorspace RGB -resize "%%f" "%%f\%%a"
与mogrify
命令相同:
mogrify -format webp "%%f\%%a"
当没有 空格时,引号不会造成任何伤害,因此最佳做法是习惯于始终引用路径名或文件名。
【讨论】:
如果可能的话,我想将文件重命名为 1.jpg、1.webp、2.jpg、2.webp 等我将如何去做? @斯蒂芬 简单:实现一个计数器set counter=0
。然后在for
循环内使用set /a counter+=1
递增它并使用!counter!
构建新文件名(使用%%~xa
获取原始文件的扩展名)。如果您有困难,请提出一个新问题; chamaeleon 问题在这里不太受欢迎。
这也很有效,谢谢,好的。感谢您的提醒:)。【参考方案2】:
感谢@Stephan 和他的回答,我的空格和重命名工作正常。结果如下。
@echo off
%~d1
CD "%~p1"
SETLOCAL ENABLEDELAYEDEXPANSION
SET FOLDERS=300 600
FOR /D %%r IN (*) DO (
CD %%r
ECHO In Folder: %%r
ECHO Checking for pngs
FOR %%a IN (*.png) DO (
ECHO Converting %%a to .jpg
mogrify -format jpg "%%a"
)
FOR %%f IN (%FOLDERS%) DO (
MD %%f
ECHO In Folder: %%r\%%f
SET counter=0
FOR %%a IN (*.jpg) DO (
SET /a counter+=1
ECHO Optimizing File: %%a : Into !counter!%%~xa
convert "%%a" -sampling-factor 4:2:0 -strip -quality 85 -interlace JPEG -colorspace RGB -resize "%%f" "%%f\!counter!%%~xa"
ECHO Converting Optimized File: !counter!%%~xa into .webp
mogrify -format webp "%%f\!counter!%%~xa"
)
)
CD ..
)
【讨论】:
【参考方案3】:使用 ffmpeg.exe 将图像转换为 webp 格式。 ffmpeg.exe文件必须放在这个脚本的文件夹中。
@echo off
echo Attention!
echo When entering paths, a backslash '\' at the end of the path is not allowed!
echo For example: C:\Pictures is correct, but C:\Pictures\ is not.
echo Make sure there are no duplicate file names in folders.
echo For example, two files: Photo1.jpg and Photo1.png located in the same folder will be perceived as identical files
echo and will be prompted to overwrite the existing file. As a result, one of them will not be recorded.
echo If these files are in different folders, then this is not a problem.
set target=""
set /p target="Enter the path to the source folder: "
set outpath=""
set /p outpath="Enter the path to the folder with the new .webp files: "
if "%target%"=="""" (
echo The path to the folder with source files has not been entered. Execution will be aborted.
) else if "%outpath%"=="""" (
echo The path to the folder with the new .webp files has not been entered. Execution will be aborted.
) else (
echo %target%
for /r "%target%" %%i in (*.jpg *.jpeg *.gif *.png *.bmp) do (
mkdir "%outpath%%%~pi"
ffmpeg -i "%%i" -vcodec libwebp "%outpath%%%~pi\%%~ni.webp"
)
)
pause
【讨论】:
以上是关于无法遍历文件名中带有空格的文件、Windows 批处理文件和图像魔法的主要内容,如果未能解决你的问题,请参考以下文章