1.

Solve : batch check codition & echo?

Answer»

Dear Experts,

How to do this,

if token no 6 in abc.txt is LEQ current time, do echo %%a %%b %%c %%d %%e %%f %%g >> output.txt

abc.txt is =
abc.tre 0 W131738 Nov 16 12:00:57 2009
sfsdf.sdfsdf 0 W131455 Nov 16 10:02:11 2009
fsdf.rty 0 W7F0BCU182 Nov 16 11:02:12 2009
qw.qwe 2 w4059 Nov 16 09:05:38 2009
wsx.rfv 2 W0132017 Nov 16 05:06:13 2009
xdr.cft 0 W0132017 Nov 16 07:06:33 2009
tgb.ikm 2 W0131738 Nov 16 12:15:01 2009


if current time is 11:00:00
Output.txt should have,
sfsdf.sdfsdf 0 W131455 Nov 16 10:02:11 2009
qw.qwe 2 w4059 Nov 16 09:05:38 2009
wsx.rfv 2 W0132017 Nov 16 05:06:13 2009
xdr.cft 0 W0132017 Nov 16 07:06:33 2009


This is what i have tried,
but the output.txt is just showing %i %j %k %l %m %n %o
cd\
set Ct=%time%
set Ctt=%Ct:~0,2%
for /f "tokens=6" %%i in (abc.txt) do set LD=%%i
set LDD=%LD:~0,2%
if %LDD% LEQ %Ctt% (echo %%i %%j %%k %%l %%m %%n %%o>> output.txt
) else (
echo No result
)
pause

please advise,i often see your posts. you should show what you have tried by now.Dear gh0std0g74,
as advised,
I have updated my post use a better language to do date comparison. eg in vbscript
Code: [Select]Set objFS=CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strFile = objArgs(0)
Set objFile = objFS.OpenTextFile(strFile)
CurrentDateTime =Now
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
s = Split(strLine," ")
t=""
For i = 3 To UBound(s)
t = t &" "&s(i)
Next
t=Trim(t)
datetime = Split(t," ")
mth= datetime(0)
dy = datetime(1)
thetime = Split(datetime(2),":")
hr = thetime(0)
min = thetime(1)
sec = thetime(2)
theyr = datetime(3)
strDateTimeInFile = CDate( dy & " " & mth & " " & theyr) &" "&CDate( hr & ":" & min & ":" & sec )
If DateDiff( "d", CurrentDatetime ,strDateTimeInFile ) > -1 Then
WScript.Echo "CurrentTime is earlier than strDateTimeInFile"
WScript.Echo strLine
End If
Loop
on command line
Code: [Select]c:\test> cscript /nologo test.vbs file
Can it be done using batch dos commands?
beacause i am not more concern about date comparison, comparison parameter could be no's also.nice

just one thing


Quote

strDateTimeInFile = CDate( dy & " " & mth & " " & theyr) &" "&CDate( hr & ":" & min & ":" & sec )

Why use CDate and String concat? You could use DateSerial and TimeSerial; also, this will allow for the return type to be a Date, rather then a string. (although I imagine type coercion is making everything work as it is; you can never really be sure in different locales)

Code: [Select]
DateTimeInFile= DateSerial(theyr,mth,dy)+TimeSerial(hr,min,sec)

OLE automation Dates are stored as "double" types; "1" is one day after the origin (I can't recall, I think it was in the 1700's or something); times are represented as fractional portions. Therefore, we can add a TimeSerial to a DateSerial to create an entire TimeStamp.

Quote from: Yogesh123 on November 17, 2009, 02:06:24 AM
Can it be done using batch dos commands?
beacause i am not more concern about date comparison, comparison parameter could be no's also.

You'll probably have to hope Salmon Trout or one of the other Batch Experts decides to tackle this one if you want a batch solution. Quote from: BC_Programmer on November 17, 2009, 02:09:50 AM
nice

just one thing


Why use CDate and String concat? You could use DateSerial and TimeSerial; also, this will allow for the return type to be a Date, rather then a string. (although I imagine type coercion is making everything work as it is; you can never really be sure in different locales)

Code: [Select]
DateTimeInFile= DateSerial(theyr,mth,dy)+TimeSerial(hr,min,sec)

heh, i had always wanted to try using these 2 functions, but never really got to it. CDate is what i am familiar with so i just wrote the script without MUCH thinking. But good call though. Quote from: Yogesh123 on November 17, 2009, 02:06:24 AM
beacause i am not more concern about date comparison, comparison parameter could be no's also.
why do you say its not about date comparison? didn't you say you need the 6th token to be LEQ current time or something.?? If it is, then IT IS date comparison. Unless your date format in the file is formatted to become like this
YYYYMMDDHHMMSS, then it may be possible for comparison. (but still you need to format the current time and date also ), which is too much of a hassle to do in batch. Its time to move on.Quote from: gh0std0g74 on November 17, 2009, 02:22:57 AM
heh, i had always wanted to try using these 2 functions, but never really got to it. CDate is what i am familiar with so i just wrote the script without much thinking. But good call though.

Basically, when dealing with Dates, I've always found it works best to have everything as a date, rather then a string, always best to avoid Locale issues. Although as you said you whipped it up without thinking and it is definitely possible to over-analyze any approach @echo off
setlocal enable delayed expansio
for /f "tokens=1-7" %%a in (file.txt) do (
set tme1=!time::=!
set tme2=%%f
set tme2=!tme2::=!
if !tme2! LEQ !tme1! echo %%a %%b %%c %%d %%e %%f %%g >> output.txt
)
Untested, might not work, but it will at least point you in the right direction. Yogesh - try this. It is dependent on your %time% format being hh:mm:ss.ms, no allowance is made for AM or PM if that is included in the format. If you use the %1 variable for testing purposes it must be entered in the full format as above. The script is untested.

Code: [Select]@echo off>output.txt
setlocal enabledelayedexpansion

:: set time=%1

set time=%time::=%
set time=%time:~0,6%

for /f "delims=*" %%1 in (abc.txt) do (
set line=%%1

for /f "tokens=1-6" %%2 in ("!line!") do (
set filetime=%%7
set filetime=!filetime::=!

if !filetime! leq !time! echo %%1>>output.txt
)
)

type output.txt
No doubt one of the scripting gurus will COME up with something much more efficient.

Good luck.We must use:
setlocal enabledelayedexpansion
Use !LD! not %LD% Salmon Trout provided this INFO a day or so ago

rem I remarked set Ct=%time% out for a better test.
Remove set Ctt=08 and the rem's in the code


C:\batch>cat yo3.bat

Code: [Select]rem set Ct=%time%
rem echo Ct = %Ct%
rem set Ctt=%Ct:~0,2%

set Ctt=08
echo Ctt = %Ctt%

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1-7" %%i in (abc5.txt) do (
set LD=%%n
echo LD=!LD!
set LDD=!LD:~0,2!
echo LDD=!LDD!
echo LDD less or equal Ctt
if !LDD! LEQ !Ctt! (
echo %%i %%j %%k %%l %%m %%n %%o
) else (

echo.
echo No result found

echo.

)
OUTPUT:

C:\batch> yo3.bat
Ctt = 08
LD=12:00:57
LDD=12
LDD less or equal Ctt

No result found

LD=10:02:11
LDD=10
LDD less or equal Ctt

No result found

LD=11:02:12
LDD=11
LDD less or equal Ctt

No result found

LD=09:05:38
LDD=09
LDD less or equal Ctt

No result found

LD=05:06:13
LDD=05
LDD less or equal Ctt
wsx.rfv 2 W0132017 Nov 26 05:06:13 2009
LD=07:06:33
LDD=07
LDD less or equal Ctt
xdr.cft 0 W0132017 Nov 16 07:06:33 2009
LD=12:15:01
LDD=12
LDD less or equal Ctt

No result found

C:\batch>


Discussion

No Comment Found