| 1. |
Solve : batch check codition & echo? |
|
Answer» Dear Experts, 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? 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 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> |
|