I'm trying to build a string in my batch script but I couldn't get it to working.
Here's what I'm trying:
@echo off
set str="wt -p "
for /r "C:\Documents\Files\" %%f in (*) do (
set str=%str% "Command Prompt" cmd /k "echo %%f";new-tab -p
)
echo %str%
pause
wt -p
command is supposed to open it in the new Windows Terminal instead of regular Command Prompt.
I want to specify that %%f
which is the full path of the file does include blank spaces inside it. So the command should be accepting it.
I've always been struggling with creating batch scripts, it is overwhelming after some point too. What am I doing wrong here?
CodePudding user response:
When you want to use a variable that you defined/changed within a code block, you need delayed expansion.
@echo off
setlocal enabledelayedexpansion
set "str=wt -p "
or /r "C:\Documents\Files\" %%f in (*) do
set "str=!str!"Command Prompt" cmd /k "echo %%f";new-tab -p "
)
echo %str%
goto :eof