变量为什么会消失?

时间:2013-11-30 10:34:31

标签: windows batch-file user-accounts

我创建了一个names.bat文件 这将创建一个名为profiles的文件夹,然后在profiles文件夹中添加一个批处理以查看配置文件,我在创建程序veiwer.bat时遇到问题 names.bat包含以下代码:

@echo off
TITLE Profiles
SET /a YEAR=%DATE:~6,4%
if not exist Profiles mkdir Profiles
CD Profiles
ECHO @echo off>"Profile Viewer.bat"
ECHO :profile>>"Profile Viewer.bat"
ECHO title Profile Viewer>>"Profile Viewer.bat"
ECHO SET /P NAME=Search names:>>"Profile Viewer.bat"
ECHO call %NAME%.bat>>"Profile Viewer.bat"
ECHO goto profile>>"Profile Viewer.bat"
GOTO RES
:RES
SET /P NAME=State your name:
ECHO @echo off>%NAME%.txt
ECHO title %NAME%>>%NAME%.txt
ECHO echo Name:%NAME%>>%NAME%.txt
SET /P AGE=State your Age:
ECHO echo Age:%AGE%>>%NAME%.txt
SET /a YOB=%YEAR%-%AGE%
SET /P A=Where you born in %YOB%(Y/N)
IF %A%==Y goto YES
IF %A%==y goto YES
IF %A%==N goto NO
IF %A%==n goto NO

:NO
SET /P YOB=State your Year of birth:
ECHO echo Birth Date:%YOB%>>%NAME%.txt
goto CONTINUE

:YES
ECHO echo Birth Date:%YOB%>>%NAME%.txt
goto CONTINUE

:CONTINUE
ECHO pause>>%NAME%.txt
rename *.txt *.bat
start %NAME%.bat
%SystemRoot%\explorer.exe "C:\Users\%username%\Desktop\Profiles"
echo Here is your profile
pause

所以我编辑“Profile Viewer.bat”以查看它是否有效

@echo off
:profile
title Profile Viewer
SET /P NAME=Search names:
call .bat
goto profile

%NAME%变量在哪里? 我希望它在profile.bat中 我知道它是一个空变量,我希望它在代码中字面上说出来;

@echo off
:profile
title Profile Viewer
SET /P NAME=Search names:
call %NAME%.bat
goto profile

我知道我可以自己做,但我想让name.bat为我做这件事

1 个答案:

答案 0 :(得分:1)

ECHO call %NAME%.bat>>"Profile Viewer.bat"

执行此行时,会扩展%NAME%变量。由于它尚未定义,它扩展为零。因此,我们在Profile Viewer.bat中找到了错误的行。

为防止%NAME%被扩展,我们需要告诉命令解释器我们想要一个实际的%而不是启动一个变量。因此,我们需要escape %,如此:

ECHO call %%NAME%%.bat>>"Profile Viewer.bat"