目前使用批处理命令,是否可以指向值表?

时间:2017-09-21 07:21:01

标签: batch-file

我正在使用批处理命令在mylist.txt文件中生成文本列表。例如

\\abc\asd
\\def\as
\\ghi\as

我目前有一个数据列表(大约2500行),每行包含几个值/列,包含静态信息(类似于数据库)。该列表目前采用csv格式。

例如:

\\abc\asd   123   home
\\def\as   456   office
\\ghi\as   789   elsewhere

我需要匹配mylist.txt中的值并使用它来执行批处理命令。我可以知道怎样才能做到这一点吗?

我正在考虑使用函数,这意味着每个函数都将被命名为变量值之一。

例如。其中一个函数将被称为" \ abc \ asd"。当mylist.text的值为" \ abc \ asd"时,将调用该函数。

它是否可行,因为它可能最终会有2500个功能?我想知道这是否是最佳方式..请指教,谢谢!

其他信息(10月12日编辑)

嗨,我正在尝试比较文本文件中的行,如果该行与我的预定义文本匹配,我想执行batch / cscript命令。这有可能吗?

我目前的情况:

for /f "delims=" %%a in (input.txt) do (
set /a c+=1
set myVariable=%%a

REM if this line contains my predefined string A, execute a cscript command A
if not x%myVariable:prefinedtext1%==x%myVariable% echo theCommandIWantToExecute


REM if this line contains my predefined string B, execute a cscript command B
if not x%myVariable:prefinedtext2%==x%myVariable% echo theSecondCommandIWantToExecute

1 个答案:

答案 0 :(得分:1)

是的,这可以做到,并且非常容易实现一些功能。但是维护会很痛苦,性能可能会受到~2500个功能的影响。

以下是您的样本数据的简单演示。

@echo off

for /f "delims=" %%C in (mylist.txt) do call :%%C
echo Done
exit /b

:\\abc\asd   123   home
echo Function 1 (%0)  arg1=%1 arg2=%2
exit /b

:\\def\as   456   office
echo Function 2 (%0)  arg1=%1 arg2=%2
exit /b

:\\ghi\as   789   elsewhere
echo Function 3 (%0)  arg1=%1 arg2=%2
exit /b

- OUTPUT ---

Function 1 (:\\abc\asd)  arg1=123 arg2=home
Function 2 (:\\def\as)  arg1=456 arg2=office
Function 3 (:\\ghi\as)  arg1=789 arg2=elsewhere
Done

如果每行的第一个值表示批处理脚本的路径,则可以从CALL中删除:,然后将调用每个批处理脚本。你的主要剧本很小,而且表现会更好。但是这需要很多批处理脚本来跟踪。

@echo off

for /f "delims=" %%C in (mylist.txt) do call %%C
echo Done
exit /b