0

I have a set of variables that represent directory names: folder_1, folder_2,... folder_n. In my code I am trying to set the variable FOLDER to the correct directory name based on the user input selection after validating the value with a conditional if statement. However, as my code is, the FOLDER variable is assigned with double quote which causes the DESTINATION variable to end up looking something like this: C:\User\stackoverflow\"chosen folder"

When attempting to use this variable as the destination for a move command it fails.

@echo off
setlocal EnableDelayedExpansion

REM totally nothing import happening up here

set /p selection=""

if %selection% GEQ 0 if %selection% LEQ %a% (
    echo you selected folder !folder_%selection%!
    set FOLDER="!folder_%selection%!"
    set DESTINATION=!PATH!\Bermuda Triangle\!FOLDER!
    set /a pass=1
)

REM nothing here either 

move /-y C:\wherever\this\file\is DESTINATION
pause

Okay, that's fine, I'll just lose the quotes in line 10.

    set FOLDER=!folder_%selection%!

Running my script with these changes "apparently" fixes the syntax issue for the move command, because it gets moved to where it is supposed to; however, the pause command at the end of my script no longer works and the window closes. So while it works I can't check my echo feedback calls to make sure everything is functioning as intended.

Can anyone help me understand what is going on here?

HotSteel
  • 11
  • 6
  • 2
    `!PATH!` is already a system variable; please don't change it. – SomethingDark Aug 22 '22 at 18:50
  • 2
    Do not be afraid of using doublequotes, especially in the correct places. ```set /p "selection="```, ```"%selection%"```, ```"%a%"```, ```set "FOLDER=!folder_%selection%!"```, ```set "DESTINATION=!PATH!\Bermuda Triangle\!FOLDER!"```, ```"%DESTINATION%"``` – Compo Aug 22 '22 at 18:58
  • @Compo thanks for the tip. I just started using batch scripting yesterday, so I'm not familiar with the syntactical basics, including the proper use of quotes. – HotSteel Aug 22 '22 at 19:18
  • @Compo reworking some of the quotes in my code fixed the issue, specifically your suggestion for line 10. I still don't understand why my cmd window was getting closed, but the original problem I was trying to figure out is solved. Also, thanks for the post edits. :) – HotSteel Aug 22 '22 at 19:26
  • 1
    `%destination%` contains a space. Without quotes, that makes to arguments, leaving `move` to deal with three parameters - which it can't and so generates a script-breaking syntax error. For troubleshooting don't run your script with a double-click, but open a command prompt window and run it from there, so the window will stay, enabling you to read any error messages. – Stephan Aug 22 '22 at 19:40

0 Answers0