Computing.Net > Forums > Programming > Editing text file thru bat

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.

Editing text file thru bat

Reply to Message Icon

Name: z4reg
Date: September 18, 2007 at 00:16:07 Pacific
OS: XP/Vista
CPU/Ram: 512mb/2gb
Product: Intel/845/946
Comment:

Hi
i want bat script to edit filename.scn ,which is editable thru text editor.

---------S C N file Sample-----
CUBEASF

SearchPath=SEASONDATA\CIRCUITS\CHINA\SHANGHAI
SearchPath=SEASONDATA\CIRCUITS\CHINA

MASFile=SHANGHAI.MAS
MASFile=CHINA.MAS
MASFile=CHINAMAP.MAS

View=mainview
{
Clear=True
Color=(191, 223, 239)
Size=(1.0, 1.0) Center=(0.5, 0.5)
FOV=(77.75, 62.50)
ClipPlanes=(0.50, 1000.00)
View=rearview
{
Clear=True
Color=(191, 223, 239)
Size=(256.0, 128.0) Center=(128.0, 64.0)
FOV=(62.5, 62.5)
ClipPlanes=(1.00, 150.00)
}
}

GroupMethod=Dynamic
AmbientColor=(128, 128, 128)

FogMode=LINEAR FogIn=(300.00) FogOut=(1500.00) FogDensity=(0.05) FogColor=(255, 238, 211)

Light=FDirect01
{
Type=Directional Dir=(0.010, -0.528, 0.849) Color=(254, 255, 211)
}


Instance=track01
{
MeshFile=track01.mts CollTarget=True HATTarget=True ShadowReceiver=True
Response=VEHICLE,TERRAIN
}
Instance=track02
{
MeshFile=track02.mts CollTarget=True HATTarget=True ShadowReceiver=True
Response=VEHICLE,TERRAIN
}

Group=Group01
{
Instance=track01
Instance=track01
}

-----END of file----------

so i want few(4/5) find - replace actions


Find: MASFile=SHANGHAI.MAS
Replace: MASFile=NEW_SHANGHAI.MAS
MASFile=SHANGHAI.MAS

Find:Instance=track02
{
MeshFile=track02.mts CollTarget=True HATTarget=True ShadowReceiver=True
Response=VEHICLE,TERRAIN
}

Replace:Instance=track02
{
MeshFile=track02.mts CollTarget=True HATTarget=True ShadowReceiver=True
Response=VEHICLE,TERRAIN
}
Instance=track03
{
MeshFile=track03.mts CollTarget=True HATTarget=True ShadowReceiver=True
Response=VEHICLE,TERRAIN
}


Find: Instance=track01
Instance=track02

Replace: Instance=track01
Instance=track02
Instance=track03


Thanks in advance ;)



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: September 18, 2007 at 17:49:50 Pacific
Reply:

It can't be done though a batch file. Not easily, at least. (But if one of you guys want to give it a shot, don't let me stop you.)

PERL might be a better idea, or VBScript if you must choose a script language native to Windows.


0

Response Number 2
Name: ghostdog
Date: September 19, 2007 at 00:29:21 Pacific
Reply:

if you can use Python, here's a solution
[code]
import re
data = open("file").read()
pat1=re.compile("Instance=track(\d+)\n{(.*?)}",re.DOTALL|re.M)
pat2=re.compile("MASFile=SHANGHAI.MAS")
pat3=re.compile("(Group=.*{.*?})",re.DOTALL|re.M)
foundgroup=pat3.findall(data)[0]
for i in pat1.findall(data):
....numvalue = i[0]
....value = i[1]
newvalue = str(int(numvalue) + 1).zfill(2)
newstring = """Instance=track%s
{
%s
}
""" % (newvalue,value)
data=pat2.sub("MASFile=NEW_SHANGHAI.MAS",data)
grp=foundgroup.split()
grp.insert(-1,"Instance=track%s"%newvalue)
print data[0:data.index("Group=")] + newstring + '\n'.join(grp)
[/code]

usage: Python myscript.py
output:
---------S C N file Sample-----
CUBEASF

SearchPath=SEASONDATA\CIRCUITS\CHINA\SHANGHAI
SearchPath=SEASONDATA\CIRCUITS\CHINA

MASFile=NEW_SHANGHAI.MAS
MASFile=CHINA.MAS
MASFile=CHINAMAP.MAS

View=mainview
{
Clear=True
Color=(191, 223, 239)
Size=(1.0, 1.0) Center=(0.5, 0.5)
FOV=(77.75, 62.50)
ClipPlanes=(0.50, 1000.00)
View=rearview
{
Clear=True
Color=(191, 223, 239)
Size=(256.0, 128.0) Center=(128.0, 64.0)
FOV=(62.5, 62.5)
ClipPlanes=(1.00, 150.00)
}
}

GroupMethod=Dynamic
AmbientColor=(128, 128, 128)

FogMode=LINEAR FogIn=(300.00) FogOut=(1500.00) FogDensity=(0.05) FogColor=(255, 238, 211)

Light=FDirect01
{
Type=Directional Dir=(0.010, -0.528, 0.849) Color=(254, 255, 211)
}


Instance=track01
{
MeshFile=track01.mts CollTarget=True HATTarget=True ShadowReceiver=True
Response=VEHICLE,TERRAIN
}
Instance=track02
{
MeshFile=track02.mts CollTarget=True HATTarget=True ShadowReceiver=True
Response=VEHICLE,TERRAIN
}

Instance=track03
{

MeshFile=track02.mts CollTarget=True HATTarget=True ShadowReceiver=True
Response=VEHICLE,TERRAIN

}
Group=Group01
{
Instance=track01
Instance=track01
Instance=track03
}


0

Response Number 3
Name: z4reg
Date: September 21, 2007 at 07:36:26 Pacific
Reply:

problem with Python is its additional requirements!
Vbs too requires wscript ..if im correct?

so bat or lastly vb script will do..

Thanks for response ghostdog and Razor2.3 ;)


0

Response Number 4
Name: Razor2.3
Date: September 21, 2007 at 21:43:31 Pacific
Reply:

VBScript, 33 lines:

Dim sLookFor(2, 2)
sLookFor(0, 0) = "MASFile=SHANGHAI.MAS"
sLookFor(0, 2) = "MASFile=NEW_SHANGHAI.MAS"
sLookFor(1, 0) = "Instance=track02"
sLookFor(1, 1) = "}"
sLookFor(1, 2) = "Instance=track03" & vbNewLine & "{" & vbNewLine & _
"MeshFile=track03.mts CollTarget=True HATTarget=True ShadowReceiver=True" & vbNewLine & _
"Response=VEHICLE,TERRAIN" & vbNewLine & "}"
sLookFor(2, 0) = "Group=Group01"
sLookFor(2, 1) = "Instance=track02"
sLookFor(2, 2) = "Instance=track03"

With CreateObject("Scripting.FileSystemObject")
Set fOut = .CreateTextFile("filename-new.scn", 2, True)
With .OpenTextFile("filename.scn", 1, False)
Do Until .AtEndOfStream
sLine = SReadWrite(.ReadLine)
For i = 0 To UBound(sLookFor)
If sLine = sLookFor(i, 0) Then
Do Until sLookFor(i, 1) = sLine Or sLookFor(i, 1) = ""
sLine = SReadWrite(.ReadLine)
Loop
fOut.WriteLine sLookFor(i, 2)
End If
Next
Loop
End With
End With

Function SReadWrite(sIn)
fOut.WriteLine sIn
SReadWrite = Trim(sIn)
End Function

Explanation of the 2D array at top:
sLookFor(x, 0) = Search for text
sLookFor(x, 1) = Second search for text (will add text after this is found). Optional
sLookFor(x, 2) = Text to add.


0

Response Number 5
Name: Mechanix2Go
Date: September 22, 2007 at 01:42:27 Pacific
Reply:

"Find: Instance=track01
Instance=track02

Replace: Instance=track01
Instance=track02
Instance=track03"

===============================
Not clear. Do you want to replace this:

Instance=track01
Instance=track02

with this?

Instance=track01
Instance=track02
Instance=track03


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

M2



0

Related Posts

See More



Response Number 6
Name: z4reg
Date: September 22, 2007 at 10:40:31 Pacific
Reply:

@Mechanix2Go : yeah i want that to be replace

@Razor2.3 : thanks I tried it but it doesn't work!
Its erases all contains in filename.scn
and now text "ÿþ" can be found in both files filename.scn and newly created filename-new.scn.
I don't want to create new file but edit the original(filename.scn) file.



0

Response Number 7
Name: z4reg
Date: September 27, 2007 at 01:47:37 Pacific
Reply:

hello...?


0

Response Number 8
Name: Razor2.3
Date: September 27, 2007 at 03:04:07 Pacific
Reply:

I'm not sure why my script would add "ÿþ" to filename.scn. Aside from a single sequential read, the script does not touch the original.

Why the new file didn't add any text is easier to explain. It's probably either the errant "ÿþ"'s, or the file in Unicode, as VBScript opens things in ASCII by default.


0

Response Number 9
Name: Mechanix2Go
Date: September 28, 2007 at 03:16:39 Pacific
Reply:

As Razor2.3 indicates, if it's not ASCII [TEXT], all bets are off.

==================================
@echo off > newfile
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (filename.scn) do (
echo %%a >> newfile
if '%%a'=='Instance=track02' echo Instance=track03 >> newfile
)



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

M2



0

Response Number 10
Name: z4reg
Date: September 28, 2007 at 03:28:25 Pacific
Reply:

yes it's ASCII [TEXT]


0

Response Number 11
Name: z4reg
Date: September 28, 2007 at 06:35:11 Pacific
Reply:

@M2: your script is working and new file is created but its does wrong replacements.

correct:
------------------
Group=Group01
{
Instance=track01
Instance=track02
Instance=track03
}
------------------

Incorrect:This part must be untouch.
------------------
Instance=track02
Instance=track03
{
MeshFile=track02.mts CollTarget=True HATTarget=True ShadowReceiver=True
Response=VEHICLE,TERRAIN
}


Here is the sample & original file
http://www.zshare.net/download/3897385d825b38/


0

Sponsored Link
Ads by Google
Reply to Message Icon






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: Editing text file thru bat

Bat File edit text file www.computing.net/answers/programming/bat-file-edit-text-file/1653.html

How to edit a .bat file or text file?? www.computing.net/answers/programming/how-to-edit-a-bat-file-or-text-file/2682.html

Batch file to edit text file www.computing.net/answers/programming/batch-file-to-edit-text-file/19329.html