0

Started programming a little while ago, and wanted to do a simple program that can sort through images via user input. I wanted to do this via cmd/powershell because I assume it'd be the faster way to do it and it's easier than, say, C or C++ (Please note I'm not the best at this)

I currently have the following:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
cd "C:\img\Unsorted"

:SORT
FOR /R %%f IN (*.jpg *.png *.gif *.jpeg) DO (
    echo Current file is: %%f
    start "" "D:\Downloads\ImageGlass_Kobe_8.6.7.13_x64\ImageGlass.exe" %%f
    CHOICE /N /C 123 /M "PICK A NUMBER (1 (Folder A), 2 (Folder B), or 3(Delete))"%1
    IF ERRORLEVEL ==3 GOTO THREE
    IF ERRORLEVEL ==2 GOTO TWO
    IF ERRORLEVEL ==1 GOTO ONE
    GOTO END

    :THREE
    echo Current file is: %%f ::This is where the output is: 'Current file is: %f' which clearly indicates it forgot the file
    move %%f "C:\img\Delete"
    echo To the trash it goes!
    taskkill /IM ImageGlass.exe
    GOTO END

    :TWO
    echo Folder B Selected
    taskkill /IM ImageGlass.exe
    move %%f "C:\img\FolderB"
    GOTO END

    :ONE
    echo Folder A
    taskkill /IM ImageGlass.exe
    move %%f "C:\img\FolderA"
    GOTO END
)

:END
goto SORT

The problem i'm running is that it returns "The system cannot find the file specified." Whenever the move command is sent (because it somehow looses the file info??)

Compo
  • 36,585
  • 5
  • 27
  • 39
Remi
  • 33
  • 5
  • [**Never** use `:label` nor `:: label-like comment` inside a command block enclosed in `()` parentheses](https://stackoverflow.com/a/32147995/3439404) – JosefZ Oct 06 '22 at 10:46
  • Thanks! Unfortunately the comment was added for the post specifically and is not part of the program! So, it still doesn't run well – Remi Oct 06 '22 at 10:50
  • `%%f` is not defined outside the `for` loop. – Stephan Oct 06 '22 at 11:01
  • the correct syntax for `if` is `if errorlevel 3 ...`. Your wrong syntax just happens to work because `==` is interpreted like a whitespace during parsing. Don't rely on this "autocorrection" – Stephan Oct 06 '22 at 11:03
  • Huh, so how could I define it? – Remi Oct 06 '22 at 11:04
  • Thanks Stephan! I'll try to do that and see if adjusting the syntax works – Remi Oct 06 '22 at 11:11
  • Forget about `goto` and labels in loops (or other code blocks) - `goto`'s break your loop and labels have undefined behaviour, probably also breaking your loop. You have to change your logic to not use labels and `goto`s within the `for` loop. – Stephan Oct 06 '22 at 11:37

1 Answers1

0
CHOICE /N /C 123Q /M "PICK A NUMBER (1 (Folder A), 2 (Folder B), or 3(Delete))"%1
set "choicemade=option!errorlevel!"
taskkill /IM ImageGlass.exe
if /i "!choicemade!"=="option4" goto :eof

if /i "!choicemade!"=="option3" (
 echo Current file is: %%f
 move "%%f" "C:\img\Delete"
 echo To the trash it goes!
)

(repeat for options 2 & 1)

Notes: Because delayedexpansion has been invoked, the "variable" !errorlevel! is the value of errorlevel as it has changed within the loop, and similarly for choicemade.

Since you want to taskkill regardless of the option taken, include it here - After setting choicemade

I've added option Q for Quit. Since it's the 4th character in the /c string, pressing q will set errorlevel to 4

":eof" (with the colon) is defined by cmd as physical end-of-file

if /i makes the match case-insensitive.

move "%%f" .... because %%f may contain spaces.

The goto end statements can be omitted so that the for loop merely proceeds to the next value.

IMHO, it's not good practice to use cmd's keywords (like sort) as labels or variable-names.

"loose" means "not tight" or "unleash". "lose" means "not win" or "to be deprived of, or cease to have or retain (something)". You need to lose the extra o,

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thanks for everything! I turn around the code by just making a big line everytime like this: IF ERRORLEVEL 3 echo echo "To the trash it goes!" && move %%f "C:\img\Delete" && taskkill /IM ImageGlass.exe And it works but I will look at your option to see how it works. Also, thanks for the Taskkill optimization, haven't thought about it! And thanks for the loose vs lose clarification, not a native english speaker so it always helps! – Remi Oct 06 '22 at 21:59