简体   繁体   English

如何使用从 txt 文件中读取的变量使用批处理来输出文本块?

[英]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.

相关问题 如何在输出重定向中使用vbscript变量从批处理脚本写入配置文件 - How can I write a config file from batch script using vbscript variables in output redirect 批处理文件-无法将文本从Java打印到.txt。 通过使用批处理编程 - Batch file - Can't print out the text from java to .txt. by using batch programming 批处理文件-阅读txt文件并重新格式化文本 - Batch File - Read txt file and reformat the text 使用批处理文件从txt文件读取特定值 - read specific value from a txt file using batch file 如何从txt文档在Windows批处理文件中设置变量 - How to set variables in a Windows batch file from a txt document 如何使用Windows批处理读取txt文件中的特定字符串? - How to read specific string in txt file by using windows batch? 从批处理文件中的.txt文件读取路径 - read path from a .txt file in a batch file 如何设置要由批处理脚本更改并输出为.txt的文本文件并擦除原始文本文件的内容? - how to set a text file to be altered by a batch script and output to a .txt and erase the contents of the original text file? 如何使用批处理将 txt 文件中的一行替换为新行 - How can I replace a line with a new line in a txt file using batch 当某些字段为空时,Windows批处理文件如何从分隔的文本文件中正确读取数据? - How can a Windows batch file read data correctly from a delimited text file when some fields are null?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM