0

The program exits without warning as soon as it hits the second FOR loop FOR /F %inputDir% %%F in (*.jpg) do

I am running on a windows 10 machine, moving and renaming pictures

I have already tried putting in break points and flags, but to no avail. I even tried just having a dummy load (just an echo and then pause) inside the FOR loop (FL) since I learned from somewhere that batch executes entire FLs at once. I have NOT tried using delayed expansion, but willing to learn how.

@echo off
echo LOADING CONFIG
cd "Batch stuff"
FOR /F "tokens=1,2 delims=&" %%A in (config.txt) do (
REM echo %%A
REM echo %%B
SET inputDir=%%A
SET outputDir=%%B
)
echo inputDirectory: %inputDir%
echo outputDirectory: %outputDir%

echo CONFIG LOADED
echo Continue?
pause





FOR /F %inputDir% %%F in (*.jpg) do (
echo here
pause
cls
echo openning: %%F
echo newName: 
SET /P newName=Enter a new name for this image: 
cls 
echo openning: %%F
echo newName: %newName%

echo Hit space to continue or exit the program (manually)
pause
REM CALL nameAndMove %%F %newName%


)

Expected results: Second FL runs opens the picture Prompts for newName clears Screen re-displays information (filepath and newName) and asks for confirmation calls to a WIP subroutine (FL didn't work even when I wasn't calling to the sub)

I am not able to see any error messages since the program exits before I get a chance to see any. Even with the pause function after the second FOR loop, I am still unable to get the program to freeze. I assume this has something to do with incorrect syntax, since the FL failed with a dummy load.

Filepath:

Filepath

1 Answer 1

1

for /f doesn't let you specify the directory path as you are attempting to do. If you're wanting to loop through a directory (including subfolders) you'll need for /r like this:

for /r "%inputDir%" %%A in (*.jpg) do (
    echo %%~nxA
)

You will need to use delayed expansion if you are going to be using your parameter every time your loop iterates (i.e. if you want to change the name of each .jpg). My preferred method of doing that is to assign the parameter as a variable, and then you'll just use exclamation points when you use that variable during delayed expansion:

setlocal enabledelayedexpansion
for /r "%inputDir%" %%A in (*.jpg) do (
    set "jpg=%%~nxA"
    echo I need to change the name of !jpg!
)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! I forgot that /F doesn't like directories, and as for the renaming section. I'll be passing the path and new name off to a subRoutine (which will also move that image)
Now that I think about it, renaming the images inside the second FL and then moving them all at once sounds better
It's no problem - and yeah, getting stuff done in the loop is usually easier to write and cleaner than calling a subroutine with your parameter. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.