Command Variable Outputs

风格不统一 提交于 2019-12-13 17:59:14

问题


Basically, I was working on some code, dealing with creating a text file, then later reading the text file line by line, and turning each line into a variable, which is then echoed within other text to make the variable fit into html code, which will later be used in a website. Here is my code:

    @echo off
    set file=file
    cd %cd%
    for /d %%b in (*) do dir /ad /on /s /b "%%b" >> get_dirs.txt
    for /F "tokens=*" %%A in (get_dirs.txt) do ( 
    echo %%A > tmpfile.txt
    set /p t= < tmpfile.txt
    pause
    echo ^<a href="%file%:///%t%"^>%t%^</a^>
    )
    pause

Ignore the pauses they were being used for previous debugging. My main problem is that my command prompt is giving me an output of <a href="file:///"></a> which is want I want accept my variable %t% is not being echoed with the rest of html code. I can't seem to figure out what is wrong with it. Thank you.


回答1:


Standard delayed expansion problem. Search SO for hundreds of articles about setlocal delayedexpansion

Your code is written better

for /F "tokens=*" %%A in (get_dirs.txt) do ( 
 echo ^<a href="%file%:///%%A"^>%%A^</a^>
)

If, for reasons you don't reveal, you actually want the variable in t then

for /F "tokens=*" %%A in (get_dirs.txt) do ( 
 set "t=%%A"
 echo ^<a href="%file%:///%%A"^>%%A^</a^>
)

will do that, but you'd syill need to either use delayedexpansion or a subroutine to use the dynamic value assigned to t.



来源:https://stackoverflow.com/questions/40923431/command-variable-outputs

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