CMD文件 - 延迟的变量扩充 [英] CMD file - delayed variable expansion
问题描述
我写一个CMD /批处理文件(下运行的Win-7的cmd.exe),我似乎越来越挂了延迟的变量扩充。
我使用的文本文件输入即形式:
9 .CN = ISRJX.OU = Linc.OU = thisco.O = UCOM.T = UCOM。 13年8月20日
10 .CN = FXXLISHER.OU = Linc.OU = thisco.O = UCOM.T = UCOM。 13年10月13日
11 .CN = QXX004F.OU = Linc.OU = thisco.O = UCOM.T = UCOM。 13年10月14日
12 * .CN = QXX1001OB.OU = Linc.OU = thisco.O = UCOM.T = UCOM。 13年10月15日
块引用>如inputfile.txt内容。目的是经过提取的第一个字.CN =,在过程中的这一点。
请注意,我不能地带根据之前的CN =,因为字符的数量将有所不同。
字符数我的脚本是:
SETLOCAL ENABLEEXTENSIONS enableDelayedExpansion
REM关闭@echo
FOR / F令牌= 3 delims ==。 %%一个在(inputfile.txt)做(
设置ACCT = %%一个
回声。 %%一个,ACCT%%
)
ENDLOCAL我试过每一套报价相结合的,%等,并同时启用&放!;禁用推迟扩张,仍不能得到这个工作。在大多数情况下,当我使用!我结束了呼应的实际!即
回声!ACCT!
将呼应的实际文本!ACCT!我看过很多例子和答案,这里和其他地方,关于延迟的变量扩充。我只是无法弄清楚如何解决它在这个例子中,在这里我想ACCT在循环中拓展。
怀疑这是一个简单的标点符号的问题,但我的想法。当我运行它,我看到一套ACCT为%% a的值,但是当它相呼应,显然它不会扩展到新的价值 - 而不是它会响应任何它被设置为previously,或空白
我也试过
disabledelayedexpansion
,这使得我的结果没有差异(包括当我使用!ACCT!
而不是%ACCT%
)解决方案的ACCT变量名称后删除空间,并使用!标记。
关闭@echo
SETLOCAL ENABLEEXTENSIONS EnableDelayedExpansion
FOR / F令牌= 3 delims ==。 %%的(inputfile.txt)在做(
设置ACCT = %% A
回声。 %% A,!ACCT!
)
ENDLOCALI am writing a CMD/batch file (running under Win-7 cmd.exe) and I seem to be getting hung up on delayed variable expansion.
I am using text file input that is in the form:
9 .CN=ISRJX.OU=Linc.OU=thisco.O=UCOM.T=UCOM. 8-20-13
10 .CN=FXXLISHER.OU=Linc.OU=thisco.O=UCOM.T=UCOM. 10-13-13
11 .CN=QXX004F.OU=Linc.OU=thisco.O=UCOM.T=UCOM. 10-14-13
12 *.CN=QXX1001OB.OU=Linc.OU=thisco.O=UCOM.T=UCOM. 10-15-13
as contents in "inputfile.txt". Purpose is to extract the first word after ".CN=", at this point in the process.
Note that I can't strip based on number of chars before "CN=" because the number of chars will vary.
My script is:
setlocal enableextensions enableDelayedExpansion REM @echo off for /f "tokens=3 delims==." %%a in (inputfile.txt) do ( set "acct =%%a" echo. %%a,%acct% ) endlocal
I've tried every set of combination of quote, !, % etc. and both enabled & disabled delayed expansion, and still can't get this to work. For the most part, when I use ! I end up echoing the actual !. i.e.
"echo !acct!"
will echo the actual text "!acct!".I have read many examples and answers, here and elsewhere, about delayed variable expansion. I just can't figure out how to work around it in this example, where I want "acct" to expand within the loop.
Suspect this is a simple punctuation problem, but I'm out of ideas. When I run this, I see acct set to the value for %%a, but when it echoes, clearly it doesn't expand to the new value -- instead it will echo whatever it was set to previously, or blank.
I have also tried
disabledelayedexpansion
, which makes no difference in my results (including when I use!acct!
instead of%acct%
.)解决方案Remove the space after the acct variable name and use the ! marks.
@echo off setlocal EnableExtensions EnableDelayedExpansion for /f "tokens=3 delims==." %%A in (inputfile.txt) do ( set "acct=%%A" echo. %%A,!acct! ) endlocal
这篇关于CMD文件 - 延迟的变量扩充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!