[英]How can I use batch to output a block of text using variables read in from a txt file?
TIA for any help or advice. TIA 寻求任何帮助或建议。
I have a requirement to import over 10k connection strings into MTPuTTY and the only way to do so is via the import function using an xml.我需要将超过 10k 个连接字符串导入 MTPuTTY,唯一的方法是通过使用 xml 的导入函数。
I have a connectionstrings.txt which has a list of all the strings and I'll be using each value as an entry several times in each block of text.我有一个 connectionstrings.txt,其中包含所有字符串的列表,我将在每个文本块中多次使用每个值作为条目。
So the strings.txt file would look like;所以strings.txt文件看起来像;
username1@servername1 username1@servername2 username2@servername1 username2@servername2 etc用户名1@服务器名1 用户名1@服务器名2 用户名2@服务器名1 用户名2@服务器名2 等
The batch file should take each of these as a variable and enter them into the block before outputting the block to an xml.批处理文件应将这些中的每一个作为变量并将它们输入到块中,然后再将块输出到 xml。
The standard block should look like this;标准块应该是这样的;
<Node Type="1">
<SavedSession>username1@servername1 <SavedSession>
<DisplayName>username1@servername1 </DisplayName>
<ServerName>username1@servername1 </ServerName>
<PuttyConType>0</PuttyConType>
<Port>22</Port>
<UserName></UserName>
<Password></Password>
<PasswordDelay>0</PasswordDelay>
<CLParams>-load username1@servername1 "username1@servername1 " -P 22</CLParams>
<ScriptDelay>0</ScriptDelay> )
So far I've tried to compile an IF statement using %%I as the variable and building the block with that but I get various errors such as Echo not expected.到目前为止,我已经尝试使用 %%I 作为变量编译一个 IF 语句并用它构建块,但是我得到了各种错误,例如 Echo not expected。 I assume I'm being daft and missing basic syntax.
我认为我很愚蠢并且缺少基本语法。
FOR /F %%i in (connection strings.txt) echo (
<Node Type="1">
<SavedSession>%%i</SavedSession>
<DisplayName>%%i</DisplayName>
<ServerName>%%i</ServerName>
<PuttyConType>0</PuttyConType>
<Port>22</Port>
<UserName></UserName>
<Password></Password>
<PasswordDelay>0</PasswordDelay>
<CLParams>-load %%i "%%i" -P 22</CLParams>
<ScriptDelay>0</ScriptDelay> ) >> connection strings.xml
Ideally, it'd be great if the batch could generate separate XML files per user if that's possible?理想情况下,如果可能的话,批处理可以为每个用户生成单独的 XML 文件会很棒吗?
This can be achieved with the correct application of For loop and escaping of redirection characters.这可以通过正确应用 For 循环和转义重定向字符来实现。
@ECHO OFF
Call :find "connection strings.txt"
ECHO completed
pause
exit
:find
For /F "USEBACKQ delims=" %%a in ("%~1") DO (
For %%i in (%%~a) DO (
CALL :create "%%~i"
)
) 2>nul
GOTO :EOF
:create
(
ECHO ^<Node Type="1"^>
ECHO ^<SavedSession^>%~1^</SavedSession^>
ECHO ^<DisplayName^>%~1^</DisplayName^>
ECHO ^<ServerName^>%~1^</ServerName^>
ECHO ^<PuttyConType^>0^</PuttyConType^>
ECHO ^<Port^>22^</Port^>
ECHO ^<UserName^>^</UserName^>
ECHO ^<Password^>^</Password^>
ECHO ^<PasswordDelay^>0^</PasswordDelay^>
ECHO ^<CLParams^>-load %~1 "%~1" -P 22^</CLParams^>
ECHO ^<ScriptDelay^>0^</ScriptDelay^>
)>>%~1.xml
GOTO :EOF
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.