问题
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?
error message :
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
I want it to end up like this
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
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 ..
)
回答1:
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
should change to
convert "%%a" -sampling-factor 4:2:0 -strip -quality 85 -interlace JPEG -colorspace RGB -resize "%%f" "%%f\%%a"
same with the mogrify
command:
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.
回答2:
Thanks to @Stephan and his answer's I've got the spaces and the rename working. Heres the result.
@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:
Convert images to webp format using ffmpeg.exe. The ffmpeg.exe file must be placed in the folder of this script.
@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
来源:https://stackoverflow.com/questions/52136267/cant-loop-over-files-with-spaces-in-the-filename-windows-batch-file-and-image-m