Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi
i want bat script to edit filename.scn ,which is editable thru text editor.---------S C N file Sample-----
CUBEASFSearchPath=SEASONDATA\CIRCUITS\CHINA\SHANGHAI
SearchPath=SEASONDATA\CIRCUITS\CHINAMASFile=SHANGHAI.MAS
MASFile=CHINA.MAS
MASFile=CHINAMAP.MASView=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.MASFind: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=track02Replace: Instance=track01
Instance=track02
Instance=track03
Thanks in advance ;)

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.

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-----
CUBEASFSearchPath=SEASONDATA\CIRCUITS\CHINA\SHANGHAI
SearchPath=SEASONDATA\CIRCUITS\CHINAMASFile=NEW_SHANGHAI.MAS
MASFile=CHINA.MAS
MASFile=CHINAMAP.MASView=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
}

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 ;)

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 WithFunction SReadWrite(sIn)
fOut.WriteLine sIn
SReadWrite = Trim(sIn)
End FunctionExplanation 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.

"Find: Instance=track01
Instance=track02Replace: Instance=track01
Instance=track02
Instance=track03"===============================
Not clear. Do you want to replace this:Instance=track01
Instance=track02with this?
Instance=track01
Instance=track02
Instance=track03
=====================================
If at first you don't succeed, you're about average.M2

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

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.

As Razor2.3 indicates, if it's not ASCII [TEXT], all bets are off.
==================================
@echo off > newfile
setLocal EnableDelayedExpansionfor /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

@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/

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

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