简体   繁体   中英

Building string in batch script on Windows

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM