无法在文件名,Windows批处理文件和image magick中带有空格的文件上循环 [英] Cant loop over files with spaces in the filename, windows batch file and image magick
问题描述
我正在尝试遍历一堆文件夹,创建子文件夹,然后在文件上循环,使用imagemagick对其进行转换,然后将其放入新的子文件夹中并重命名它们.某些文件的名称中包含空格,并导致错误.如何解决此问题?
I'm trying to loop over a bunch of folders, create subfolders then loop on the files, convert them with imagemagick and put them into the new subfolders and rename them. Some files have spaces in their name and cause the error... How can I fix this?
错误消息:
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.**
文件夹结构如下所示...文件夹结构如下所示.
The Folder Structure looks like this... The Folder Structure looks like this...
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等...
If possible I'd like to rename the files to 1.jpg, 1.webp, 2.jpg, 2.webp etc...
批处理文件如下所示...
The batch File looks like this...
@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 ..
)
推荐答案
要处理带空格的文件名,请用引号将它们引起来.例如您的命令
To handle filenames with spaces, quote them. For example your command
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"
在没有 空格的情况下,引号不会造成任何危害,因此最佳实践习惯于始终对路径名或文件名进行查询.
The quotes doesn't do any harm, when there is no space, so as best practice get used to always qoute path- or file names.
这篇关于无法在文件名,Windows批处理文件和image magick中带有空格的文件上循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!