How to concatenate variable with string or variable in batch file

最后都变了- 提交于 2019-11-30 19:34:48

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
  )
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!