Computing.Net > Forums > Programming > Batch file to insert string

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file to insert string

Reply to Message Icon

Name: kens321
Date: June 12, 2009 at 04:38:43 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

Hi

I need some help i have a file called myfile.txt and I want to create a batch file to insert a character in the beginning of every line in my file.

ex.
myfile.txt

0123456

newfile.txt

X0123456



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: June 12, 2009 at 05:38:03 Pacific
Reply:

:: INCHR.BAT Usage: inchr Your_Character
@echo off > MyFile.new
for /F "delims=" %%j in (MyFile.txt) do echo.%1%%j>> MyFile.new
:: End_Of_Batch

I'm sure ghostdog will offer a better solution in Python.

0

Response Number 2
Name: Mechanix2Go
Date: June 12, 2009 at 06:05:25 Pacific
Reply:

Hi IVO,

"I'm sure ghostdog will offer a better solution in Python."

Sure, but how many have python installed? One in 30,000?


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 3
Name: ghostdog
Date: June 12, 2009 at 08:38:57 Pacific
Reply:

>>Sure, but how many have python installed? One in 30,000?

that's the reason why there are converters to convert Python ( or Perl) code into exe for distribution. Python (or other prog lang) can be downloaded and installed to 1 machine for development purposes. after testing and acceptance, convert to exe and distribute. This way, there is not much difference doing it in batch and distribute to every PC.


0

Response Number 4
Name: ghostdog
Date: June 12, 2009 at 09:06:52 Pacific
Reply:

@OP, here's an alternative solution in Python

import sys,fileinput
position = int(sys.argv[1]) - 1
toinsert = sys.argv[2]
filename = sys.argv[3]
for line in fileinput.FileInput(filename,inplace=1):
    line=list(line.strip())
    line.insert(position,toinsert)
    print ''.join(line)

usage: c:\test> python <position:integer> <word to insert> <filename>

here's an executableyou can use in your batch

C:\test>more file.txt
123456

C:\test>test.exe 1 "insert_here" file.txt

C:\test>more file.txt
insert_here123456

C:\test>test.exe 5 "insert_here" file.txt #insert  at position 5

C:\test>more file.txt
1234insert_here56


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file to insert string

Batch file to run program www.computing.net/answers/programming/batch-file-to-run-program/15407.html

Batch file to delete profiles www.computing.net/answers/programming/batch-file-to-delete-profiles/16684.html

Batch file for comparing strings www.computing.net/answers/programming/batch-file-for-comparing-strings-/17507.html