从另一个批处理文件更新批处理文件

时间:2012-12-04 15:28:35

标签: batch-file batch-processing

亲爱的StackOverFlow会员,

请帮我处理这个批处理文件。我想使用“SET / P INPUT =%=%”给出的答案,让它永久更新另一个批处理文件。

这是第一个从用户那里获得答案的批处理文件

@echo off
cls
echo.
echo .................................................................
echo ..... Specify what the name of the Store is, this will send .....
echo ............... alerts to abd@abc.co.za ..............
echo ................................................................. 
echo.
pause
:option
cls
color 5E
echo. 
echo "............   Press 1 to specify what the store name is......"
echo "............   Press 2 to exit the program   ................."
echo. 
SET /P M=Type from the menu above 1 or 2 then press ENTER:
IF %M%==1 GOTO SEND                                           
IF %M%==2 GOTO EOF  

:SEND
cls
color 0A
set INPUT=
set /P INPUT=Enter Store Name: %=%                            
if "%INPUT%"=="" goto input
echo "You said that the store name is: %INPUT%"

:: Have the user confirm his/her choice
SET /P ANSWER=Is the name correct (Y/N)?     
echo You chose: %ANSWER%                                      
if /i {%ANSWER%}=={y} (goto :yes)                             
if /i {%ANSWER%}=={yes} (goto :yes)                           
goto :no
:yes
echo You pressed YES!... The name is updating     
goto name
:no
echo You pressed NO!... The program will exit
pause
cls
goto eof
:name
::set /A store=%INPUT%
echo %INPUT% >> notify_support.bat
::Terminate the program
:EOF

正如您所看到的,我正在努力指定我应该“回显%INPUT%>> notify_support.bat”。这是从第二个批处理文件中获取的代码

@echo off
call senditquiet -s smtp.gmail.com -port 587 -u rsupp0rt@gmail.com -protocol ssl -p access -f rsupp0rt@gmail.com -t 888@gmail.com -subject "Store ABC" -body "Hello there, There is an issue logged at the store.<br>Best regards." 

当第一个批处理文件运行时,它会更新第二个批处理文件,但只是将其转储到文件的末尾。

我需要INPUT ECHOed来替换第二批文件中的“Store ABC”。

请协助,我对批处理文件感到厌烦。

1 个答案:

答案 0 :(得分:3)

echo %INPUT% >> notify_support.bat

该行包含>>,表示“在文件末尾转储”。您可以使用单个>覆盖现有文件内容。这样,您可以重新生成整个文件(无论如何只有2行)。

另一种解决方案是实际解析现有文件并替换该文本。您可以使用for /F ...来执行此操作,它允许您遍历文件的行。然后,您可以根据现有文件的(更改的)内容生成新文件。缺点是这种文件解析方法特别适用于每行与字段和分隔符(如CSV文件)具有相同格式的数据文件。它不太适合解析批处理文件或程序源文件等“复杂”文件。

相关问题