How to concatenate variable with string or variable in batch file

后端 未结 1 847
眼角桃花
眼角桃花 2021-01-04 19:35

I would like to concatenate a variable with a string.

In line 7 to line 11 I try to concat !somevariable! with a string or with %%P variable.
But this does not

1条回答
  •  有刺的猬
    2021-01-04 20:09

    The concatenation works! But your echo fails.

    As you are in a command block (parenthesis) all percent variables are expanded before the block will be executed, so the output of echo "%myvar%" is the content of myvar before entering the FOR-Loop.

    But you know the correct way already, using the delayed expansion (with !)

    So your code should look like

    SETLOCAL EnableDelayedExpansion
    for /f "delims=" %%P in ('dir /b *.pdf') do (
      SET "sPDFName=%%~nxP"
      echo "!sPDFName:~0,1!"
      IF "!sPDFName:~0,1!"=="1" (SET "sPDFName=!sPDFName:~0,1!")
      IF "!sPDFName:~0,1!"=="0" (SET "sPDFName=!sPDFName:~0,1!")
      SET tempStr=GEN !sPDFName! 
      SET myvar=!myvar! %%P
    
      echo "!myvar!"
      echo "!tempStr!"
      ::echo "!sPDFName!"
      pause
      for /f "delims=" %%H in ('dir /b *.html') do (
        IF "!sPDFName:~-0!"=="!%%H:~0,1!" echo %%H
      )
    )
    

    0 讨论(0)
提交回复
热议问题