Andreas
Andreas

Reputation: 23958

Move file to folder

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?

Upvotes: 0

Views: 279

Answers (2)

user7818749
user7818749

Reputation:

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" "%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"
    )

Upvotes: 1

Dominique
Dominique

Reputation: 17493

This line indicates that the file might have been moved:

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

It looks like the file, located at "c:\res1\details5.jpg" has been moved to "c:\src" and, in case there already was a file "details5.jpg", then that file has been replaced.

I have the impression that your batchfile is doing the right thing (your batchfile is moving files and in case of already existing, replacing file), while you seem to be expecting new files to arrive.

Upvotes: 0

Related Questions