如何通过文件本身更改BATCH文件中的变量值? [英] How to change variable value within BATCH file by the file itself?
问题描述
我一直在寻找答案,我从 http://www.dostips.com/DtTutoPersistency.php 和 http://ss64.com/nt/for_cmd .html 网站,但仍然无法解决我在以下方面遇到的问题: 我有一个BATCH文件,可以在其中测试特定文件夹(SendTo文件夹)的存在.如果我无法通过脚本找到它-我希望用户输入该文件夹的路径-并将结果保存在BATCH文件中.
I was looking for a long time now for an answer to this, learned nice tricks from http://www.dostips.com/DtTutoPersistency.php and http://ss64.com/nt/for_cmd.html sites, but still - don't have a solution to the problem I've encountered in: I have a BATCH file where I test the existence of specific folder (SendTo folder). In case I couldn't find it by the script - I want the user to enter the path to that folder - and keep the result in the BATCH file.
我缩小的BATCH文件("Some file.bat")看起来像:
My narrowed BATCH file ("Some file.bat") looks something like:
@echo off
REM SomeNonsense
:: Win7/Vista
IF EXIST %APPDATA%\Microsoft\Windows\SendTo\NUL (
REM Do something
GOTO :EOF
)
:: WinXP
IF EXIST %USERPROFILE%\SendTo\NUL (
REM Do something
GOTO :EOF
)
:: Else
SET SendPath=
SET /P SendP="Please enter the path to the SendTo Folder:> "
IF EXIST %TMP%\SendPath.txt DEL %TMP%\SendPath.txt
FOR /F "usebackq TOKENS=* DELIMS=" %%A in ("%~0") DO (
ECHO %%A>>%TMP%\SendPath.txt
REM Later I want to change the value of SendPath with SendP,
REM And swap the file back to the original name
)
我现在的问题是,当我只想将文本本身复制到一个临时文件(不使用COPY,因为我想一行一行地复制以便更改SendPath值)时,实际上正在解释文件的行).
My problem right now is that the lines of the file actually being interpreted, when I want only to copy the text itself to a temp file (without using COPY, because I want to copy line by line in order to change SendPath value).
另一件事是不会复制空行.
Another thing is that empty lines aren't copied.
有解决方案吗?
推荐答案
这是您想要做的:
@echo off
rem Your previous Win7/Vista, WinXP testings here...
:: Else
call :defineSendPath
if defined SendPath goto continue
SET /P "SendPath=Please enter the path to the SendTo Folder:> "
rem Store the SendPath given into this Batch file:
echo set "SendPath=%SendPath%" >> "%~F0"
:continue
rem Place the rest of the Batch file here...
goto :EOF
rem Be sure that the following line is the last one in this file
:defineSendPath
这篇关于如何通过文件本身更改BATCH文件中的变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!