Jump to content



Photo

[Batch] Strip rightmost character from each line


  • Please log in to reply
2 replies to this topic

#1 Mr_Miguel

Mr_Miguel

    Neowinian

  • Joined: 16-July 05

Posted 27 February 2010 - 14:52

Greetings.

I would like to know if it's possible, using a batch file, to strip the rightmost character from each line in a file, and if so, how? That is, for example, I would want this file:

foo|bar|xyz|
123|456|789|

To become this:

foo|bar|xyz
123|456|789

You may assume I'm using Windows XP or higher. I would also like to avoid having to write an entire new file to the disk, since the files I'm working with are quite large. Rather, I would like to modify the existing file.

Thank you in advance.


#2 jake1eye

jake1eye

    Neowinian

  • Joined: 02-April 03
  • Location: New Westminster

Posted 27 February 2010 - 17:54

I tried this cmd promt code,
@Echo Off
CLs
Mode 62,7
Color 9f

Set String="123|456|789|"

Echo.
Set S1=%String:~0,12%
Set S2=%String:~1,3%
Echo.
Echo %S1%
Echo %S2% 
pause
which produce

"123|456|789
123

Because you have | these in your string it causes problem
if you turn this Set S2=%String:~1,3% to this Set S2=%String:~1,4% the script just opens and closes fast.

If you where to do this Vbs and all the strings where like below

foo|bar|xyz
123|456|789


Then a simple VBS script would do.
Example Save as StringLeft.vbs

WScript.Echo Left("foo|bar|xyz|",11) & vbCrLf & Left("123|456|789|",11)



#3 BGM

BGM

    Wibble Wobble™

  • Joined: 30-March 03
  • Location: Farnborough, UK

Posted 27 February 2010 - 19:12

^ good, knowledgeable, concise post..

i'll rep that! :D



Click here to login or here to register to remove this ad, it's free!