批处理文件:START"" / W CMD / C似乎没有一个福尔循环内工作 [英] Batch file: START "" /W CMD /C doesn't seem to work inside a foor loop

查看:622
本文介绍了批处理文件:START"" / W CMD / C似乎没有一个福尔循环内工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,它的内容是:

  SETLOCAL enabledelayedexpansion
SET / A FT = 500
FOR / F跳过= 1令牌= 1-6%% A IN(START/等WMIC路径Win32_LocalTime获取日^,^小时,分钟^,^月,二^,年
/格式:表')DO(
 IF NOT!FT!== 500 GOTO进行
 SET FD = %%的F - %%ð - %%一个
 SET FT = %%乙:%% C:%%ê
:继续
 SET / A FX =DS

回声%FD%%FT%:%*>> %LOGFILE%ENDLOCAL

最初的行

  FOR / F跳过= 1令牌= 1-6%% A IN(START/等WMIC路径Win32_LocalTime获取日^,^小时,分钟^,月^,^二,新年
    /格式:表')

  FOR / F跳过= 1令牌= 1-6%% A IN('WMIC路径Win32_LocalTime获取日^,^小时,分钟^,^月,二^年
    /格式:表')

但作为运行批处理内的WMIC命令造成的进程,因此也挂我想在一个新的CMD窗口运行WMIC命令这似乎解决了挂起问题,但现在运行WMIC命令的子窗口的内容不传递到父窗口。 (即)输出到日志文件仅仅是500(FT值),而不是被内部改变在for循环FT的价值,由于某种原因,它只是跳过for循环。

任何意见或建议,为什么出现这种情况?

我真的会AP preciate任何建议。

由于一吨提前。


解决方案

  @ECHO OFF&安培; SETLOCAL
SET / A FT = 500
FOR / Fdelims =%%倍In('WMIC路径Win32_LocalTime获取日^,^小时,分钟^,^月,第二^,年/格式:表^ |寻找20)DO(
    FOR / F令牌= 1-6%%的A(%%〜X)做(
        IF NOT%FT%== 500 GOTO进行
        SETFD = %%的F - %%ð - %% A
        SETFT = %%乙:%% C:%% E
        :继续
        SET / A FX =DS
    )

回声%FD%%FT%:*ENDLOCAL


您也可以使用将它视为

  @ECHO OFF&安培; SETLOCAL
设置将它视为档案=%TEMP%\\%随机%。〜
WMIC路径Win32_LocalTime获取日,小时,分钟,月,秒,年|找到20>中%将它视为%
SET / A FT = 500
FOR / Fusebackqtokens = 1-6%%的(%将它视为%)在做(
    IF NOT%FT%== 500 GOTO进行
    SETFD = %%的F - %%ð - %% A
    SETFT = %%乙:%% C:%% E
    :继续
    SET / A FX =DS

德尔%将它视为%
回声%FD%%FT%:*

将它视为档案的hexdump都


0000000:32 39 20 20 20 31 37 20 20 20 20 30 20 20 20 20 29 17 0
0000010:20 20 20 31 31 20 20 20 20 20 33 39 20 20 20 20 11 39
0000020:20 20 32 30 31 33 20 20 0D 0D 0A 2013 ...


输出:


2013年11月29日15时52分59秒:

I have a batch file and it's content is:

SETLOCAL enabledelayedexpansion
SET /A FT=500
FOR /F "skip=1 tokens=1-6" %%A IN ('START "" /wait WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year
/Format:table ') DO (
 IF NOT !FT!==500 GOTO proceed
 SET  FD=%%F-%%D-%%A
 SET  FT=%%B:%%C:%%E
:proceed
 SET /A FX="DS"
)
echo %FD% %FT% : %* >> "%LOGFILE%"

endlocal

Initially the line

FOR /F "skip=1 tokens=1-6" %%A IN ('START "" /wait WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year
    /Format:table ')

was

FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year
    /Format:table ')

but as running the WMIC command within the batch caused the process to hang therefore I thought of running WMIC command on a new CMD windows and that seems to solve the hang issue, but now the content of the child windows running the WMIC command is not passed to the parent window. (i.e) The output to the log file is just "500" (Value of FT) and not the value of FT that is changed inside in the For loop, due to some reason it just skips the for loop.

Any idea or suggestion why this occurs?

I will really appreciate any suggestion.

Thanks a ton in advance.

解决方案

@ECHO OFF &SETLOCAL
SET /A FT=500
FOR /F "delims=" %%x IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,SEcond^,Year /Format:table ^|find "20"') DO (
    for /f "tokens=1-6" %%A in ("%%~x") do (
        IF NOT %FT%==500 GOTO proceed
        SET "FD=%%F-%%D-%%A"
        SET "FT=%%B:%%C:%%E"
        :proceed
        SET /A FX="DS"
    )
)
echo %FD% %FT% : %* 

endlocal


You can also use a Tempfile:

@ECHO OFF &SETLOCAL
set "Tempfile=%TEMP%\%random%.~"
WMIC Path Win32_LocalTime Get Day,Hour,Minute,Month,SEcond,Year|find "20">"%Tempfile%"
SET /A FT=500
for /f "usebackqtokens=1-6" %%A in ("%Tempfile%") do (
    IF NOT %FT%==500 GOTO proceed
    SET "FD=%%F-%%D-%%A"
    SET "FT=%%B:%%C:%%E"
    :proceed
    SET /A FX="DS"
)
del "%Tempfile%"
echo %FD% %FT% : %* 

Hexdump of the Tempfile:

0000000: 32 39 20 20 20 31 37 20 20 20 20 30 20 20 20 20  29   17    0
0000010: 20 20 20 31 31 20 20 20 20 20 33 39 20 20 20 20     11     39
0000020: 20 20 32 30 31 33 20 20 0d 0d 0a                   2013  ...


Output:

2013-11-29 15:52:59 :

这篇关于批处理文件:START"" / W CMD / C似乎没有一个福尔循环内工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆