Batch for loop

Acer Aspire 5920g
July 17, 2010 at 06:26:00
Specs: Microsoft Windows 7 Professional, 2.201 GHz / 2046 MB
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 : Automatic

Desired

START_TYPE:DISABLED
DISPLAY_NAME:Automatic


See More: Batch for loop

Report •


#1
July 17, 2010 at 07:17:36
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!
)



Report •

#2
July 17, 2010 at 07:52:53
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!


Report •

#3
July 17, 2010 at 08:26:55
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 for

set 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.



Report •

Related Solutions

#4
July 17, 2010 at 09:38:10
understood..thanksss

Report •

#5
July 17, 2010 at 21:42:09
@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

GNU win32 packages | Gawk


Report •

#6
July 19, 2010 at 00:17:45
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?


Report •

#7
July 19, 2010 at 00:50:02
@skywalker, what is it that you are trying to do?

GNU win32 packages | Gawk


Report •

#8
July 19, 2010 at 00:54:46
Hi GhostDog,

I am converting month part of date to monthname
eg- if MM-01 then MMM-Jan
if MM-02 then MMM-Feb

I 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


Report •

#9
July 19, 2010 at 01:16:46
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


Report •

#10
July 19, 2010 at 02:08:46
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


Report •


Ask Question