Batch file 如何将多管道命令的输出分配给windows CMD批处理中的变量?
我需要遍历目录中的一组文件,匹配一个通配符模式,并在两个其他模式之间提取文件名的一部分。Unix(即bash)等效命令如下所示:Batch file 如何将多管道命令的输出分配给windows CMD批处理中的变量?,batch-file,variables,Batch File,Variables,我需要遍历目录中的一组文件,匹配一个通配符模式,并在两个其他模式之间提取文件名的一部分。Unix(即bash)等效命令如下所示: ls -1 "$wildcard1"*"$wildcard2" | while read filename; do string2find=`echo $filename | cut -d $delim1 -f2 | cut -d $delim2 -f1` echo $string2find #do something with string2find variabl
ls -1 "$wildcard1"*"$wildcard2" | while read filename; do
string2find=`echo $filename | cut -d $delim1 -f2 | cut -d $delim2 -f1`
echo $string2find
#do something with string2find variable here
done
在批处理文件中,我尝试了这个
for %%f in (WILD1*WILD2) do (
echo %%f | cut -d_ -f2 | cut -d"." -f1> temp.out <--here, need something between an "_" and a "."
type temp.out <-- see the right string in the file
set var= <clear the variable
set /p var=<temp.out
echo %var% <-- ECHO is off message I am getting here
del temp.out
REM need to do something with var here
)
用于%%f in(WILD1*WILD2)do(
echo%%f | cut-d u-f2 | cut-d”。“-f1>temp.out第一步是延迟变量扩展。第二步查看以/?
将命令的输出获取到变量中。第三步——某些字符需要用^
转义才能在其他命令中使用。例如:for%%I in(*u*.txt)do for/f“eol=|代币=1*delims=u”%%J英寸(“%%~nI”)do echo%%K
搜索至少包含一个下划线的非隐藏*.txt
文件,并输出第一次出现一个或多个下划线与文件扩展名之间的字符串。对于名为的文件,Test1\u file 1.txt
为输出文件1
,对于名为第二个\uuuuuuuu file 2\u示例的文件,txt
为输出utput文件2\u示例
。如果这是您需要的命令行,并且您想知道该命令行的详细工作原理,请告诉我,我将写一个答案并给出完整的解释。打开a,运行for/?
并阅读整个输出帮助。对于批处理文件的初学者来说,for/F
的用法不容易理解因此,我已经写了很多答案,并非常详细地解释了批处理文件如何使用for/F
循环,以及如何根据其他使用的选项(如eol=
,tokens=
和delims=
)逐步处理for with option/F
解析的字符串。