Search code examples
batch-filefor-loopcmdwindow

Using variable in Windows FOR Loop


What I'm trying to achieve is to take a user input which would be a term with numbers and letters and then use that to match directories in a folder and delete ones matching, here's what i have till now, but its not working,

@echo off
set /p:ID=Enter Search Term:
for /d %%G in ("*(%ID%)") do rd /s /q "%%~G"
pause

The output that i get if i turn on echo shows, that %ID% is not getting replaced with variable value which was the prompted input. Following is the output

for / %G in ("*()") do rd /s /q "%~G"

Can someone point out the error in this? thanks a lot


Solution

  • set /p:ID=Enter Search Term:
          ^ This colon is included in the name of the variable
    

    So, you end with a variable named :ID. You can execute set : to see the variable defined.

    To solve the problem just remove the colon.

    set /p ID=Enter Search Term: