1.

Solve : Check condition & convert no.?

Answer»

Dear Experts,
How to do this, If 4th token in abc.txt is PM, then convert the 2nd no. in 24hr format
ex. for 01 - 13, for 11 - 23

abc.txt is,
11/26/2009 11 20 PM 12 abc.bk
11/26/2009 10 13 AM 9 abc.bk
11/26/2009 01 46 PM 11 abc.bk
11/26/2009 10 14 AM 12 abc.bk
11/26/2009 01 20 PM 10 abc.bk

output required is,
11/26/2009 23 20 PM 12 abc.bk
11/26/2009 10 13 AM 9 abc.bk
11/26/2009 13 46 PM 11 abc.bk
11/26/2009 10 14 AM 12 abc.bk
11/26/2009 13 20 PM 10 abc.bk

my code is- But not working as required,
please advise,

cd\
c:
setlocal enabledelayedexpansion
for /f "tokens=1-7" %%i in (abc.txt) do (
set LD=%%l
set rhh=%%j
if !LD!==PM (
if %rhh%==01 set rhh=13
if %rhh%==02 set rhh=14
if %rhh%==03 set rhh=15
if %rhh%==04 set rhh=16
if %rhh%==05 set rhh=17
if %rhh%==06 set rhh=18
if %rhh%==07 set rhh=19
if %rhh%==08 set rhh=20
if %rhh%==09 set rhh=21
if %rhh%==10 set rhh=22
if %rhh%==11 set rhh=23
ECHO %%i %%j %%k %%l %%m %%n %%o>> output.txt
) else (
echo No RESULT found
)
)
pauseBecause you enabled delayed expansion, you must replace the % signs in a variable (%rrh% for example) with ! . Don't replace the % in the %%a, as it will mess up then.

Also, your last bit, with the ELSE won't work no matter what. So, to fix it, add
& set changed=yes
to every if command like this:
if %rhh%==01 set rhh=13
so it would look like:
if %rhh%==01 set rhh=13 & set changed=yes
Then remove the else command, there were no opening brackets from any if commands, so it would end the for loop and display a command error.

Instead of these lines:
) else (
echo No result found
)

Replace them with:
If not %changed%==yes echo YOUR MESSAGE HERE


Dear Helpmeh,
I have done the advised changes, as follows,
but still i am not getting the results as desired,

cd\
c:
setlocal enabledelayedexpansion
set changed=yes
for /f "tokens=1-7" %%i in (abc.txt) do (
set LD=%%l
set rhh=%%j
if !LD!==PM (
if !rhh!==01 set rhh=13 & set changed=yes
if !rhh!==02 set rhh=14 & set changed=yes
if !rhh!==03 set rhh=15 & set changed=yes
if !rhh!==04 set rhh=16 & set changed=yes
if !rhh!==05 set rhh=17 & set changed=yes
if !rhh!==06 set rhh=18 & set changed=yes
if !rhh!==07 set rhh=19 & set changed=yes
if !rhh!==08 set rhh=20 & set changed=yes
if !rhh!==08 set rhh=21 & set changed=yes
if !rhh!==10 set rhh=22 & set changed=yes
if !rhh!==11 set rhh=23 & set changed=yes
echo %%i %%j %%k %%l %%m %%n %%o>> output.txt

) else (
If not %changed%==yes echo YOUR MESSAGE HERE
)
)
pause

result i GOT is,
11/26/2009 11 20 PM 12 abc.bk
11/26/2009 01 46 PM 11 abc.bk
11/26/2009 01 20 PM 10 abc.bk

there should be 13 at the place of 01 & 23 at the place of 11Now I see what the issue is...

Take this:
echo %%i %%j %%k %%l %%m %%n %%o>> output.txt

Replace it with this:
echo %%i !rrh! %%k %%l %%m %%n %%o>> output.txt

Great Helpmeh,
Thanks a lot.

It's working great!!



Discussion

No Comment Found