无法在文件名,Windows批处理文件和image magick中带有空格的文件上循环

时间:2018-09-02 10:51:22

标签: windows batch-file windows-7 imagemagick

我正在尝试遍历一堆文件夹,创建子文件夹,然后在文件上循环,使用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 ..
)

3 个答案:

答案 0 :(得分:2)

要处理带空格的文件名,请用引号引起来。例如您的命令

<head>
  <script>
    window.LMS_REST_API_URL = "http://192.168.0.111:3000/";
  </script>
...

应更改为

private lms_cli_URL = window["LMS_REST_API_URL"];

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"

在没有 空格的情况下,引号不会造成任何损害,因此,最佳做法是习惯于总是qoute路径名或文件名。

答案 1 :(得分:1)

感谢@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 ..
)

答案 2 :(得分:0)

使用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