Web Hosting, Design, & Coding forum


Batch file to truncate lines of text?


I'm a complete noob. but am in desperate need of a batch file which will truncate all lines within a text file: 40 characters from the left.
.. and if possible, to allow me to designate the directory path?

As an example

from this:

Coding & scripting forum is the ideal place for newcomers and experienced developers to share ideas.
Discuss programming logic, debug code, or talk about best practices for keeping scripts.
Any programming language goes, so whether you need help with web applications, services, or software, we're ready to help!

to this:

Coding & scripting forum is the ideal pl
Discuss programming logic, debug co
Any programming language goes, so w

Thanks for any help with this


Discussion is locked
You are posting a reply to: Batch file to truncate lines of text?
The posting of advertisements, profanity, or personal attacks is prohibited. Please refer to our CNET Forums policies for details. All submitted content is subject to our Terms of Use.
Track this discussion and email me when there are updates

If you're asking for technical help, please be sure to include all your system info, including operating system, model number, and any other specifics related to the problem. Also please exercise your best judgment when posting in the forums--revealing personal information such as your e-mail address, telephone number, and address is not recommended.

You are reporting the following post: Batch file to truncate lines of text?
This post has been flagged and will be reviewed by our staff. Thank you for helping us maintain CNET's great community.
Sorry, there was a problem flagging this post. Please try again now or at a later time.
If you believe this post is offensive or violates the CNET Forums' Usage policies, you can report it below (this will not automatically remove the post). Once reported, our moderators will be notified and the post will be reviewed.

All Answers

Collapse -
Re: truncate

In reply to: Batch file to truncate lines of text?

Although such a program is easy to write in any programming language (including a macro in MS Excel), the real command line freak downloads sed for Windows (a Unix uitility originally) from http://gnuwin32.sourceforge.net/packages/sed.htm and uses that.


Collapse -

In reply to: Re: truncate

Thanks for the reply Kees_B

And thanks for the program link, however It would be preferable to get this process as a batch command.

My request could also be done easily within Excel, but I have a series batch commands, in which I would like to keep this final one as part of that process.

Any ideas to turn this into batch process?



Collapse -
Re: batch process

In reply to: tnx

You can run sed in a .bat file.
You can run an Excel with a startup macro in a .bat file.
You can run a vbscript in a .bat file.
In fact, you can run any program in a .bat file. it's up to you to chose your preferred programming language.

Maybe even it can be done in Powershell. I have never used it, so I can't tell.


Collapse -
Run from Bat

In reply to: Re: batch process

Thanks Kees_B

I can understand how to open a program /file with a bat file, but once the program is open how can It run a specific task within that program.
As in the case of using SED with a bat file?



Collapse -
Re: program

In reply to: Run from Bat

The program opens and starts doing what you programmed it to do. If that's a specific task, it's doing that.
Maybe hire a programmer?


Collapse -
Re: truncate

In reply to: Batch file to truncate lines of text?

I realize it's been a long time since this question was posted, but I was searching for the same thing tonight so maybe it will still be useful to someone else.

I needed to truncate all lines of an input text file as jeffsgp described, except truncate to 35 characters.

After installing sed from the link provided by Kees_B (thanks) I created the following BAT/CMD file for Win7:


@echo off
rem Usage: Supply 2 command line arguments, 1=input file, 2=output file
rem example:
rem truncate myinputfile.txt myoutputfile.txt
@echo Input file is %1
@echo Ouput file is %2
@echo Processing starting.
type %1 | "c:\Program Files (x86)\GnuWin32\bin\sed" "s/\(.\{1,35\}\).*/\1/" > %2
@echo Processing complete.

Note: Input and output file must be different names.

Incidentally, it looks like the cut command would have been easier than sed, but by the time I found that out I had already installed sed.

Popular Forums

Computer Newbies 10,686 discussions
Computer Help 54,365 discussions
Laptops 21,181 discussions
Networking & Wireless 16,313 discussions
Phones 17,137 discussions
Security 31,287 discussions
TVs & Home Theaters 22,101 discussions
Windows 7 8,164 discussions
Windows 10 2,657 discussions


Best Black Friday Deals

CNET editors are busy culling the list and highlighting what we think are the best deals out there this holiday season.