Hi guys i just learnt data parsing using for loop..im wondering is there a way to remove the whitespaces and number for the text file below? Thanks... Example
START_TYPE : 4 DISABLED
DISPLAY_NAME : Alerter
START_TYPE : 2 AUTO_START
DISPLAY_NAME : AutomaticDesired
START_TYPE:DISABLED
DISPLAY_NAME:Automatic
This might help you get started: @echo off setlocal EnableDelayedExpansion for /f "delims=" %%a in (file) do ( set line=%%a set line=!line: =! for /l %%i in (0 ,1, 9) do set line=!line:%%i=! echo !line! )
that was helpful! well..i tried to find guides for your code but unfortunately its hard to find.. could you explain what
set line=!line: =!
set line=!line:%%i=!does? thanks!
Yeah, generally, if you're changing variables inside a FOR loop,
you need 'setlocal EnableDelayedExpansion' somewhere
near the top of your script and you need to use
'!' instead of '%' to get the right value. As forset line=!line: =! set line=!line:%%i=!
the first one is replacing spaces for nothing, and the second -
which is part of a FOR loop - is replacing digits from 0..9 for
nothing.
understood..thanksss
@OP, the batch might fail if you consider whitespaces includes tabs, instead of just only spaces. You might want to modify the batch to include tabs. Alternatively, you can use file processing tools like sed to do the job..Here's a one liner for you using sed for windows C:\test>more file START_TYPE : 400000001 DISABLED DISPLAY_NAME : Alerter START_TYPE : 2 AUTO_START DISPLAY_NAME : Automatic C:\test>sed "s/[ \t]*//g;s/[0-9]//g" file START_TYPE:DISABLED DISPLAY_NAME:Alerter START_TYPE:AUTO_START DISPLAY_NAME:Automatic
why below for loop is not working? set MM=08
for /f "tokens=%MM%" %%i in ("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec") do (set MMM=%%i)
echo %MMM%Above batch doesn't run properly for 08 and 09 and gives error 8" was unexpected at this time., it does work if value is 07 or 11 or any other.
Any ideas guys?
@skywalker, what is it that you are trying to do?
Hi GhostDog, I am converting month part of date to monthname
eg- if MM-01 then MMM-Jan
if MM-02 then MMM-FebI think when the value of MM is 08, it is somehow not able to understand the value...is it considering it as octal? guess
I can do above needed thing using IF condition, but wanted to avoid that.
Thanks
This should do the trick and works fine...but although not completely happy with extra two lines :) set %MM%=08
if %MM%==08 set MM=8
if %MM%==09 set MM=9
for /f "tokens=%MM%" %%i in ("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec") do (set MMM=%%i)
echo %MMM%Thanks
Finally, here comes the generic code, should be helpful in future!! set %MM%=08
REM Prefix 1000 and then do modulo with 1000, will return remainder without starting 0 :)
set /a MM=1000%MM% %% 1000
for /f "tokens=%MM%" %%i in ("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec") do (set MMM=%%i)
echo %MMM%Thanks
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |