Batch File Colon Problem

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ZayaMia
Posts: 14
Joined: 25 Apr 2017 02:54

Batch File Colon Problem

#1 Post by ZayaMia » 26 Apr 2017 23:17

Hey all again :oops: ,


I'm having a problem with my batch script. I am trying to extract the time & date but due to the colon it gives me an incomplete date & time.

Here's the script:

Code: Select all

(for /F "tokens=1,2 delims=:<>" %%a, in ('findstr "Name=\"PHIL\" currentState=\"Executive13\"" *.xml') do (
 set "list=%%b"
 for /F "delims=" %%c in (^"!list: ^=^
% Do NOT remove this line %
!^") do set %%c
 if "!Name!+!currentState!" equ ""PHIL"+"Executive13"" (
 echo File: "%%a", date=%%~ta", testDuration=!testDuration!, holidayCount=!holidayCount!, lockTime=!lockTime!"
 )
)) > Outputfile.txt
start notepad outputfile.txt



The time is dated in XML files as:(extracted from"lockTime"):

<testInfo testDuration="57" holidayCount="0" completedtask="12" currentState="Executive13" Name="PHIL" testVersion="13" lockTime="2017-04-11T11:20:05"
<result testStepName="locating" sequenceNrResult="1" testStepResult="OK">
etc.
</testInfo>
</testresult>


How do I fix this problem with the colon with batch?

Output file always looks like this:

lockTime="2017-04-11T11" ( normally it should display :"2017-04-11T11:20:05" )

If I remove : from first for /f statement's delims= clause. it will fix the "lock time". BUT it won't show me the modified Date then anymore (date=%%~ta).

Thank you for reading and help in advance !

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Batch File Colon Problem

#2 Post by elzooilogico » 27 Apr 2017 04:55

suposing it works for you (you have not posted the full script, so I'm only guessing)...

removing the colon from the first for makes %%a to be filename.xml: Note the final colon, so the filename is not what you expect, and %%~ta won't work. The workaround is to add an additional for loop that removes it from filename.

so, remove the colon in the first for loop

Code: Select all

(for /F "tokens=1,2 delims=<>" %%a, in ('findstr "Name=\"PHIL\" currentState=\"Executive13\"" *.xml') do (

and change

Code: Select all

echo File: "%%a", date=%%~ta", testDuration=!testDuration!, holidayCount=!holidayCount!, lockTime=!lockTime!"
with

Code: Select all

   for /F "tokens=1* delims=:" %%f in ("%%a") do (
    echo File: "%%f", date=%%~tf, testDuration=!testDuration!, holidayCount=!holidayCount!, lockTime=!lockTime!
   )


final code may be

Code: Select all

@echo off & setlocal enabledelayedexpansion & rem only guessing 
(for /F "tokens=1,2 delims=<>" %%a, in ('findstr "Name=\"PHIL\" currentState=\"Executive13\"" *.xml') do (
 set "list=%%b"
 for /F "delims=" %%c in (^"!list: ^=^
% Do NOT remove this line %
!^") do set %%c
 if "!Name!+!currentState!" equ ""PHIL"+"Executive13"" (
   for /F "tokens=1* delims=:" %%f in ("%%a") do (
    echo File: "%%f", date=%%~tf, testDuration=!testDuration!, holidayCount=!holidayCount!, lockTime=!lockTime!
   )
 )
)) > Outputfile.txt
start notepad outputfile.txt

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Batch File Colon Problem

#3 Post by Compo » 27 Apr 2017 06:57

Here's a modification, please note that this is untested!

Code: Select all

@Echo Off
(For /F "Tokens=1* Delims=:" %%A In ('FindStr/I "Name=\"PHIL\" currentState=\"Executive13\"" *.xml') Do (Set "list=%%B"
   SetLocal EnableDelayedExpansion
   Set list=!list: =^
% Do NOT remove this line %
!
   For /F "UseBackQ Tokens=1-2 Delims==>" %%C In ('!list!') Do If Not "%%D"=="" Set "_%%C=%%~D"
   If "!_Name!+!_currentState!"=="PHIL+Executive13" (
      Echo File: "%%A", date="%%~tA", testDuration="!_testDuration!", holidayCount="!_holidayCount!", lockTime="!_lockTime!")
   EndLocal))>OutputFile.txt
Start notepad OutputFile.txt

ZayaMia
Posts: 14
Joined: 25 Apr 2017 02:54

Re: Batch File Colon Problem

#4 Post by ZayaMia » 27 Apr 2017 20:59

This worked perfectly ! Thank you so much ! :!:

Code: Select all

@echo off & setlocal enabledelayedexpansion & rem only guessing 
(for /F "tokens=1,2 delims=<>" %%a, in ('findstr "Name=\"PHIL\" currentState=\"Executive13\"" *.xml') do (
 set "list=%%b"
 for /F "delims=" %%c in (^"!list: ^=^
% Do NOT remove this line %
!^") do set %%c
 if "!Name!+!currentState!" equ ""PHIL"+"Executive13"" (
   for /F "tokens=1* delims=:" %%f in ("%%a") do (
    echo File: "%%f", date=%%~tf, testDuration=!testDuration!, holidayCount=!holidayCount!, lockTime=!lockTime!
   )
 )
)) > Outputfile.txt
start notepad outputfile.txt

Post Reply