Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.txt0123456
newfile.txt
X0123456

:: 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.

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

>>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.

@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

![]() |
![]() |
![]() |

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