无法循环文件名中包含空格的文件,windows 批处理文件和图像 magick
Cant loop over files with spaces in the filename, windows batch file and image magick
我试图遍历一堆文件夹,创建子文件夹然后循环文件,用 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 ..
)
要处理带有 space 的文件名,请将它们引用。例如你的命令
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"
当有 no space 时,引号不会造成任何伤害,因此最佳做法是习惯始终引用路径或文件名。
感谢@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 ..
)
使用 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
我试图遍历一堆文件夹,创建子文件夹然后循环文件,用 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 ..
)
要处理带有 space 的文件名,请将它们引用。例如你的命令
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"
当有 no space 时,引号不会造成任何伤害,因此最佳做法是习惯始终引用路径或文件名。
感谢@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 ..
)
使用 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