问题
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