Home > database >  Move file to folder
Move file to folder

Time:01-20

I need some help with moving a file.

I'm trying to create a batch file that will resize images and overwrite the file smaller sized file.
The resize part can't have source and destination as the same filename, so I thought I could set a temp folder and move it back.

@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

The above resizes the file and places it in res folder, then the move is done, but I don't know where the file ends up, it's not where it should at least.

This is the output of the cmd window and it seems the move/echo is correct(?).

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.

What am I doing wrong here?

CodePudding user response:

this option will place the files back into the original directory with an appended _scaled tag in the name, then delete the original file once it exists. Using findstr we will only focus on the items without the _scaled tag.

@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

This is untested, so just update me on the result, obviously do some QA first by creating dummy directory with some files and change source to it.

EDIT, as per your last comment, if this is a once of run and you want to retain the original name, then you could simply move them to a folder, do the conversion and let them land back in the source.

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

move /Y "%source%\*.jpg" "           
  •  Tags:  
  • Related