将文件移动到文件夹

Move file to folder

我需要一些关于移动文件的帮助。

我正在尝试创建一个批处理文件,它将调整图像大小并覆盖较小尺寸的文件。
调整大小部分不能将源和目标作为相同的文件名,所以我想我可以设置一个临时文件夹并将其移回。

@echo off
set "source_folder=c:\src"
set "result_folder_1=c:\res1"

SET COPYCMD=/Y
:start
if exist %source_folder%\*.jpg (
    TIMEOUT /T 2 >nul
    for %%a in ("%source_folder%\*jpg") do (
    call scale.bat -source "%%~fa" -target "%result_folder_1%\%%~nxa" -max-height 1000 -max-width 1000 -keep-ratio yes -force yes
        TIMEOUT /T 5
    echo /Y "%result_folder_1%\%%~nxa" "%%~a"
        move /Y "%result_folder_1%\%%~nxa" "%%~a"
        del "%%~fa"
    )
)
goto start

上面调整了文件大小并将其放在res文件夹中,然后移动完成,但我不知道文件最终在哪里,至少它不应该在那里。

这是 cmd window 的输出,看来 move/echo 是正确的(?)。

Waiting for 0 seconds, press a key to continue ...
/Y "c:\res1\details5.jpg" "c:\src\details5.jpg"
        1 file(s) moved.

Waiting for 0 seconds, press a key to continue ...
/Y "c:\res1\details6.jpg" "c:\src\details6.jpg"
        1 file(s) moved.

Waiting for 0 seconds, press a key to continue ...
/Y "c:\res1\details7.jpg" "c:\src\details7.jpg"
        1 file(s) moved.

Waiting for 0 seconds, press a key to continue ...
/Y "c:\res1\details8.jpg" "c:\src\details8.jpg"
        1 file(s) moved.

Waiting for 0 seconds, press a key to continue ...
/Y "c:\res1\details9.jpg" "c:\src\details9.jpg"
        1 file(s) moved.

我做错了什么?

此行表示文件可能已移动:

Waiting for 0 seconds, press a key to continue ...
/Y "c:\res1\details5.jpg" "c:\src\details5.jpg"

看起来位于“c:\res1\details5.jpg”的文件已移至“c:\src”,如果已经有一个文件“details5.jpg” ,则该文件已被替换。

我的印象是您的批处理文件正在做正确的事情(您的批处理文件正在移动文件,如果已经存在,则替换文件),而您似乎期待新文件到达。

此选项会将文件放回原始目录,并在名称中附加 _scaled 标记,然后在原始文件存在后将其删除。使用 findstr 我们将只关注没有 _scaled 标签的项目。

@echo off
set "source=C:\src"

:start
for /f "delims=" %%a in ('dir /b "%source%\*.jpg" ^| findstr /V /R "_scaled"') do (
    call scale.bat -source "%source%\%%~nxa" -target "%source%\%%~na_scaled%%~xa" -max-height 1000 -max-width 1000 -keep-ratio yes -force yes
    if exist "%source%\%%~na_scaled%%~xa" del /Q "%source%\%%~nxa"
    )
(timeout /t 5)>nul && goto :start

这是未经测试的,所以只需向我更新结果,显然首先通过创建包含一些文件的虚拟目录并更改源代码来进行一些质量检查。

编辑,根据你最后的评论,如果这是 运行 的一次并且你想保留原来的名字,那么你可以简单地将它们移动到一个文件夹,进行转换并让它们登陆回到源头。

@echo off
set "source=C:\src"
set "destination=C:\res1"

move /Y "%source%\*.jpg" "%destination%"
for %%a in ("%destination%\*.jpg") do (
    call scale.bat -source "%destination%\%%~nxa" -target "%source%\%%~nxa" -max-height 1000 -max-width 1000 -keep-ratio yes -force yes
    if exist "%source%\%%~nxa" del /Q "%destination%\%%~nxa"
    )