-3

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.

spaniard89
  • 307
  • 1
  • 6
  • 18

3 Answers3

2
@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 :
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • Your solution doesn't contain START "" and I want a solution with START, cause it seems to hang if START "" is not used. Thanks for the reply though. – spaniard89 Nov 29 '13 at 14:55
  • Yeah I understand that, but there seems to be some problem with the WMIC on the system I am working currently on. Even the older code of mine without the start and changed in the code seems to work fine and it been working from ages, it's just the system I am currently that doesn't work. – spaniard89 Nov 29 '13 at 15:01
  • @spaniard89 I and the other guys here can't repair your computer. Please come back if it's working properly. – Endoro Nov 29 '13 at 15:05
  • I do understand your frustration. I am much more frustrated, http://ss64.com/nt/wmic.html in the notes section, you will find why it hangs while WMIC. – spaniard89 Nov 29 '13 at 15:09
  • @spaniard89 I know this issue for a long time. I remove the additional `cr` commands with the second `for` loop. – Endoro Nov 29 '13 at 15:21
  • Thank you for the help mate, but It doesn't work. It hangs exactly at the same point. Is there a way or a solution with start being involved? cause its the only way it doesn't hang. – spaniard89 Nov 29 '13 at 15:36
1

Adding the /b to the start command, will run it in the same window like the parent.
So you can capture the output.

FOR /F "skip=1 tokens=1-6" %%A IN ('START "" /B /wait WMIC ...

But it should also run without the START, your system seems to be broken.

jeb
  • 78,592
  • 17
  • 171
  • 225
  • Thanks a lot for the solution mate. But if I run the WMIC command on the same window it seems to hang. Is there any other way without using /B? Like somehow passing the value from one window to another. – spaniard89 Nov 29 '13 at 15:18
  • I would recommend to repair your system first. Else I can only see a solution to redirect the output to a file, as the two windows are more or less independent – jeb Nov 29 '13 at 16:50
0

Change the FOR line to this:

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

/Format:table `) DO (

Note that I added usebackq and then changes 2 single quotes to back quotes... best to copy paste :) See FOR /?

RGuggisberg
  • 4,630
  • 2
  • 18
  • 27