Bactch to delete 5 0's from a file

February 14, 2012 at 08:05:08
Specs: Windows 7
Hello,

can someone help me to make a batch to remove 5 zero's from a txt file lfrom the collum 18 of every line ?

PT DFFT EEW323 000003434434 4kdkfd

thank you


See More: Bactch to delete 5 0s from a file

Report •


#1
February 14, 2012 at 09:11:26
@echo off & setlocal EnableDelayedExpansion
for /F "delims=" %%j in ('type "File.txt"') do (
  set line=%%j
  set line=!line:~0,17!!line:~22!
  echo.!line!>> "File.new"
)


Report •

#2
February 14, 2012 at 10:45:30
Ivo,

I get this error

C:\Temp>cscript script.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

C:\Temp\script.vbs(1, 1) Microsoft VBScript compilation error: Invalid character

you know why?

thank you


Report •

#3
February 14, 2012 at 12:48:13
The script I posted is a batch script and must be suffixed .bat (e.g. mybat.bat), then to execute just type its name mybat.

That has nothing to do with VBScript (suffixed .vbs).


Report •

Related Solutions

#4
February 14, 2012 at 12:49:19
IVO gave you a batch, like you requested; not a VBScript, which is what you're treating it as.

EDIT: Beaten.

How To Ask Questions The Smart Way


Report •

#5
February 15, 2012 at 02:18:57
Ivo,

Thank you its working

do you think we can complicate it a bit? is it possible to

1. delete all 0 after the line 18
2. until it finds a number that its not a 0
3. add the number of 0's deleted as space at the end of that number

example

00000000ST34567335 text text text
and do this
ST34567335 tex text text

so it satys all align, because if I remove the 0 and dont add the blank lines it will not be align

thank you



Report •

#6
February 15, 2012 at 10:16:04
Do you want to mean... line actually is column and using _ for space to be readable

00000000ST34567335 text text text

becomes

ST34567335________ text text text


Report •

#7
February 15, 2012 at 10:32:12
yes, exactly, I need to remove on column 18 all the 0's until it finds a number and replace all the 0's it have removed by an empty space example:

Original File

B170600852000003000000000ST17357186TEST TEST TEST  
B170600852000000100000000ST17356576TEST TEST TEST 1 test
B17060085200000250000000ES100011429TEST TES TEST TEST

Changed File

B1706008520000030       ST17357186TEST TEST TEST  
B1706008520000001       ST17356576TEST TEST TEST 1 test
B1706008520000025       ES100011429TEST TES TEST TEST

if we just remove the 0's it will be unformated because some lines have 7 0's to be removed others have 8 0's to be removed so the only way to align the text should be to add spaces in the same number of the 0's removed

thank you


edited by moderator: Added pre tags -Razor2.3


Report •

#8
February 16, 2012 at 13:37:00
@echo off & setlocal EnableDelayedExpansion > "File.new"
for /F "delims=" %%j in ('type "File.txt"') do call :CLEAN %%j
exit

:CLEAN
set line=%*
set cnt=0
for /L %%k in (17 1 27) do if "!line:~%%k,1!"=="0" (set /A cnt+=1) else (goto :DONE)
:DONE
set /A end= %cnt% + 17
if %cnt% gtr 0 (
  set filler=
  for /L %%k in (1 1 %cnt%) do (set filler=!filler! )
  set line=%line:~0,17%!filler!!line:~%end%!
)
echo.%line%>> "File.new"
exit /B


Report •

#9
February 17, 2012 at 04:05:59
Ivo,

it's amazing what can you do,

when I try to run it I saw an issue, do you think you still can help?

the part that we need to remove the 0's is always composed by 18 characters like:

00000000ST17356576

the only change that I need is the number of zeros removed from this part they need to be added at the end of that part as spaces like: ST17356576________ where _ is a space

my initial request didint work because the 0's need to be added at the end

B1706008520000030ST17357186________TEST TEST TEST

thank you so much


Report •

#10
February 17, 2012 at 04:07:27
sorry,

at the end of the part of 18 characters after that it will start test again


Report •

#11
February 19, 2012 at 14:47:34
Be patient, I shall give you the needed code but now I have to perform an urgent repair on one of my servers.

Report •

#12
February 20, 2012 at 06:53:57
@echo off & setlocal EnableDelayedExpansion > "File.new"
for /F "delims=" %%j in ('type "File.txt"') do call :CLEAN %%j
exit

:CLEAN
set line=%*
set cnt=0
for /L %%k in (17 1 34) do if "!line:~%%k,1!"=="0" (set /A cnt+=1) else (goto :DONE)
:DONE
set /A pos= 17 + %cnt%
set /A lgt= 18 - %cnt%

if %cnt% gtr 0 (
  set filler=
  for /L %%k in (1 1 %cnt%) do (set filler=!filler! )
  set line=%line:~0,17%!line:~%pos%,%lgt%!!filler!%line:~35%
)
echo.%line%>> "File.new"
exit /B


Report •

#13
February 29, 2012 at 08:20:50
thank you very much Ivo

Report •

Ask Question