Batch file to insert string

June 12, 2009 at 04:38:43
Specs: Windows XP
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


See More: Batch file to insert string

Report •


#1
June 12, 2009 at 05:38:03
:: 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.

Report •

#2
June 12, 2009 at 06:05:25
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


Report •

#3
June 12, 2009 at 08:38:57
>>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.


Report •

Related Solutions

#4
June 12, 2009 at 09:06:52
@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


Report •


Ask Question