Link to home
Start Free TrialLog in
Avatar of MadAd1
MadAd1

asked on

Delete lines from a text file

I play a game called BF2.  For reasons known only to the authors, on each load it attempts to ping every server in a history list that it saved in a text file.  This can add serious delays in starting the game, not only for servers that have moved IP but also current servers that can take up to 5 seconds apiece to relay information.

One problem is that other stuff exists in the text file so simply clearing the file is not an option.  I decided to look into how to make a simple batch file that would delete the necessary lines before the game runs- however it appears its not a trivial task and (as you guys probably know) deletions can only be done by a few methods, one reading into memory and only writing back the info that needs to be kept.

Now while I can tinker with a few very basic scripting tasks, this one is a little beyond my current abilities (im a hardware guy) so after googling somewhat, and scratching my head over 3rd party programs such as sed and parsomatic, I decided to ask you guys if you could take a look and see if theres a simple way to do this.

Requirements of the script: on double click
1) If exists, delete a variable amount of lines with the string addServerHistory from general.con
2) Run the game with its shortcut switches intact

See below for a sample of a general.con file. In this example there are 3 addServerHistory lines to delete, however
there may be none, ten even twenty lines to delete and the position of the first addServerHistory may or may not be at the same location.

It would be unusual for a general.con to exceed 200 lines, this example has 151 so we are not expecting large processing delays.  Ideally it would use wsh or someother script method that does not rely on third party programming external to windows xp as others in the game community may wish to use this script too.

The path to general.con may vary from user to user, and there may be multiple copies of general.con on any one machine.  My current path is:

C:\Documents and Settings\Ad\My Documents\Battlefield 2\Profiles\0003\General.con

However there is also a general.con in 0004, 0002 and 0001 which need not be altered


Shortcut information:
An untypical bf2 shortcut can carry all sorts of information, from user account login to screen resolutions, I have exampled one below however not all users may have the same information in their shortcut.  



_______________________begin general.con example____________________________

GeneralSettings.setPlayedVOHelp "HUD_HELP_SPAWNSCREEN_KITTAB_pickArmyAndKit"
GeneralSettings.setPlayedVOHelp "HUD_HELP_GAMEPLAY_exitToCapture"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_HANDHELD_SUBMACHINEGUN_CONTROLS_toggleFireModes"
GeneralSettings.setPlayedVOHelp "HUD_HELP_VEHICLE_CONTROLS_exitVehicle"
GeneralSettings.setPlayedVOHelp "HUD_HELP_PLAYER_CONTROLS_zoomAltFire"
GeneralSettings.setPlayedVOHelp "HUD_HELP_GAMEPLAY_climbLadder"
GeneralSettings.setPlayedVOHelp "HUD_HELP_PLAYER_CONTROLS_sprint"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_HANDHELD_SNIPERRIFLE_CONTROLS_switchToScopeView"
GeneralSettings.setPlayedVOHelp "USRIF_M24"
GeneralSettings.setPlayedVOHelp "HUD_HELP_MENU_CONTROLS_callMedicWhenManDown"
GeneralSettings.setPlayedVOHelp "HUD_HELP_MENU_CONTROLS_spawnScreen"
GeneralSettings.setPlayedVOHelp "HUD_HELP_VEHICLE_F35_PILOT_CONTROLS_engageHoverEngines"
GeneralSettings.setPlayedVOHelp "air_f35b"
GeneralSettings.setPlayedVOHelp "HUD_HELP_VEHICLE_HELO_PILOT_CONTROLS_deployFlares"
GeneralSettings.setPlayedVOHelp "HUD_HELP_VEHICLE_HELO_PASSENGER_CONTROLS_switchToGuidedMissiles"
GeneralSettings.setPlayedVOHelp "switchToGuidedMissiles"
GeneralSettings.setPlayedVOHelp "HUD_HELP_PLAYER_CONTROLS_parachute"
GeneralSettings.setPlayedVOHelp "HUD_HELP_SPAWNSCREEN_KITTAB_pickSpawnPoint"
GeneralSettings.setPlayedVOHelp "HUD_HELP_PLAYER_CONTROLS_moveMouseToLook"
GeneralSettings.setPlayedVOHelp "HUD_HELP_PLAYER_CONTROLS_fireLeftMouse"
GeneralSettings.setPlayedVOHelp "HUD_HELP_VEHICLE_TANK_DRIVER_CONTROLS_smoke"
GeneralSettings.setPlayedVOHelp "HUD_HELP_MENU_CONTROLS_changeWeapon"
GeneralSettings.setPlayedVOHelp "HUD_HELP_GAMEPLAY_captureFlag"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_STATIONARY_CONTROLS_enterStationaryWeapon"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WORLD_PLAYER_RADAR_friendly"
GeneralSettings.setPlayedVOHelp "HUD_HELP_MENU_CONTROLS_COMMOROSE_specificSpotted"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WORLD_PLAYER_ARTILLERY_friendly"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WORLD_PLAYER_UAV_TRAILER_friendly"
GeneralSettings.setPlayedVOHelp "HUD_HELP_SPAWNSCREEN_SQUADTAB_joinSquad"
GeneralSettings.setPlayedVOHelp "HUD_HELP_SQUAD_squadJoined"
GeneralSettings.setPlayedVOHelp "HUD_HELP_SPAWNSCREEN_spawnOnSL"
GeneralSettings.setPlayedVOHelp "HUD_HELP_VEHICLE_TANK_TURRET_CONTROLS_duckInTurret"
GeneralSettings.setPlayedVOHelp "DuckInTurret"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEPAON_HANDHELD_GENERAL_reloadWeapon"
GeneralSettings.setPlayedVOHelp "HUD_HELP_VEHICLE_JET_LOWHEALTH_useFriendlyRepairStation"
GeneralSettings.setPlayedVOHelp "HUD_HELP_VEHICLE_AA_DRIVER_CONTROLS_fireSecondaryWeapon"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_HANDHELD_WRENCH_fixStuff"
GeneralSettings.setPlayedVOHelp "HUD_HELP_KIT_ENGINEER_NAMETAG_indicatorBar"
GeneralSettings.setPlayedVOHelp "HUD_HELP_KIT_ENGINEER_inVehicle"
GeneralSettings.setPlayedVOHelp "HUD_HELP_VEHICLE_JET_LOWAMMO_flyOverFriendlyAirField"
GeneralSettings.setPlayedVOHelp "HUD_HELP_VEHICLE_JET_PILOT_CONTROLS_toggleWeaponsShortcut"
GeneralSettings.setPlayedVOHelp "HUD_HELP_VEHICLE_APC_DRIVER_CONTROLS_fireSecondaryWeapon"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_STATIONARY_AT_CONTROLS_guideMissile"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_HANDHELD_ASSAULTRIFLE_CONTROLS_switchToGrenades"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_HANDHELD_ASSAULTRIFLE_CONTROLS_switchToRifle"
GeneralSettings.setPlayedVOHelp "HUD_HELP_VEHICLE_HELO_PASSENGER_CONTROLS_TVGUIDED_clickToGuide"
GeneralSettings.setPlayedVOHelp "HUD_HELP_MENU_CONTROLS_COMMOROSE_slcommoRoseAvailable"
GeneralSettings.setPlayedVOHelp "HUD_HELP_SQUAD_LEADER_VOIP_commanderChannel"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_HANDHELD_HANDGRENADE_CONTROLS_rollGrenade"
GeneralSettings.setPlayedVOHelp "ushgr_m67"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_HANDHELD_ATMINE_vehicles"
GeneralSettings.setPlayedVOHelp "HUD_HELP_SQUAD_LEADER_mainMapRightMouseMenu"
GeneralSettings.setPlayedVOHelp "HUD_HELP_SQUAD_squadCreated"
GeneralSettings.setPlayedVOHelp "HUD_HELP_VEHICLE_APC_PASSENGER_CONTROLS_firePassengerWeapon"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_STATIONARY_AA_CONTROLS_lockOnTone"
GeneralSettings.setPlayedVOHelp "HUD_HELP_SPAWNSCREEN_KITTAB_trySquads"
GeneralSettings.setPlayedVOHelp "HUD_HELP_PLAYER_CONTROLS_radioMessages"
GeneralSettings.setPlayedVOHelp "HUD_HELP_MENU_CONTROLS_largeMap"
GeneralSettings.setPlayedVOHelp "HUD_HELP_PLAYER_CONTROLS_lieDown"
GeneralSettings.setPlayedVOHelp "HUD_HELP_MENU_CONTROLS_scoreBoard"
GeneralSettings.setPlayedVOHelp "HUD_HELP_PLAYER_CONTROLS_crouch"
GeneralSettings.setPlayedVOHelp "HUD_HELP_VEHICLE_HELO_LOWHEALTH_useFriendlyRepairStation"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WORLD_PLAYER_RADAR_enemy"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WORLD_PLAYER_ARTILLERY_enemy"
GeneralSettings.setPlayedVOHelp "HUD_HELP_PLAYER_CONTROLS_moveForwardsBackwards"
GeneralSettings.setPlayedVOHelp "HUD_HELP_PLAYER_CONTROLS_jump"
GeneralSettings.setPlayedVOHelp "HUD_HELP_VEHICLE_GENERAL_CONTROLS_enterVehicle"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WORLD_PLAYER_UAV_TRAILER_enemy"
GeneralSettings.setPlayedVOHelp "HUD_HELP_PLAYER_CONTROLS_strafeLeftRight"
GeneralSettings.setPlayedVOHelp "HUD_HELP_SPAWNSCREEN_SQUADTAB_createSquad"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_commanderApply"
GeneralSettings.setPlayedVOHelp "HUD_HELP_MENU_CONTROLS_zoomMap"
GeneralSettings.setPlayedVOHelp "RURIF_AK101"
GeneralSettings.setPlayedVOHelp "HUD_HELP_KIT_MEDIC_inVehicle"
GeneralSettings.setPlayedVOHelp "HUD_HELP_KIT_MEDIC_NAMETAG_indicatorBar"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_HANDHELD_SHOCKPADDLES_CONTROLS_reviveTeamMates"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_HANDHELD_HEALBAG_GENERAL_holdHealBagToHealLocally"
GeneralSettings.setPlayedVOHelp "medikit"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_HANDHELD_LMG_GENERAL_watchTempGauge"
GeneralSettings.setPlayedVOHelp "HUD_HELP_KIT_SUPPORT_NAMETAG_indicatorBar"
GeneralSettings.setPlayedVOHelp "HUD_HELP_SPAWNSCREEN_SQUADTAB_applyCommander"
GeneralSettings.setPlayedVOHelp "HUD_HELP_KIT_SUPPORT_inVehicle"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_HANDHELD_AMMOBAG_CONTROLS_throwAmmoBagDown"
GeneralSettings.setPlayedVOHelp "ammokit"
GeneralSettings.setPlayedVOHelp "HUD_HELP_VEHICLE_JET_GUNNER_CONTROLS_switchToLaserMissiles"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_MINIMAP_description"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_MAP_QUICKMENU"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_SATELLITESCAN_description"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_UAV_description"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_ARTILLERY_description"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_SUPPLIES_description"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_MAP_FILTERS"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_placeArtillery"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_artilleryFired"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_UAVplaced"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_placeUAV"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_placeSupplies"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_HANDHELD_C4_CONTROLS_switchToDetonator"
GeneralSettings.setPlayedVOHelp "c4_explosives"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_SQUADLIST_clickSquadList"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_SQUADLIST_commoMenu"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_MAP_SATELLITEVIEW_moveKeys"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_uavDamaged"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_MAP_REQUESTS_received"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_radarDamaged"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_artilleryDamaged"
GeneralSettings.setPlayedVOHelp "HUD_HELP_MENU_COMMOROSE_slCommoRoseFirstUse"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_SQUADLIST_orderIcon"
GeneralSettings.setPlayedVOHelp "HUD_HELP_COMMANDER_VOIP_allSquadLeadersChannel"
GeneralSettings.setPlayedVOHelp "HUD_HELP_SPAWNSCREEN_KITTAB_useUnlock"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_HANDHELD_CLAYMORE_GENERAL_mineHasDirectionalBlast"
GeneralSettings.setPlayedVOHelp "usmin_claymore"
GeneralSettings.setPlayedVOHelp "gbrif_l96a1"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_HANDHELD_ZLINE_CONTROLS_useZipLine"
GeneralSettings.setPlayedVOHelp "HUD_HELP_PLAYER_CONTROLS_GasMask"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_HANDHELD_HANDGRENADE_CONTROLS_flashbang"
GeneralSettings.setPlayedVOHelp "HUD_HELP_WEAPON_HANDHELD_GHOOK_CONTROLS_useGrapplingHookUp"
GeneralSettings.setPlayedVOHelp "HUD_HELP_PLAYER_CONTROLS_NightVision"
GeneralSettings.setPlayedVOHelp "ats_tow"
GeneralSettings.setSortOrder 0
GeneralSettings.setSortKey ""
GeneralSettings.setNumRoundsPlayed 0
GeneralSettings.setServerFilter ""
GeneralSettings.addServerHistory "193.160.156.170" 29900 "TV2 - 5 JETS" 87
GeneralSettings.addServerHistory "193.160.156.173" 29901 "TV2 - 26 DRAGON 2" 96
GeneralSettings.addServerHistory "193.160.156.176" 29901 "Battlefield2.no Server #8 Dragon+Kubra" 3
GeneralSettings.setHUDTransparency 60.3361
GeneralSettings.setCrosshairColor 255 0 0 255
GeneralSettings.setBuddytagColor 0 0 0
GeneralSettings.setSquadtagColor 0 255 0
GeneralSettings.setMinimapRotate 1
GeneralSettings.setMinimapTransparency 93.0419
GeneralSettings.setViewIntroMovie 1
GeneralSettings.setOutOfVoting 0
GeneralSettings.setBFTVSaveDirectory ""
GeneralSettings.setConfirmQuit 1
GeneralSettings.setMapIconAlphaTransparency 190.76
GeneralSettings.setUseAdvancedServerBrowser 0
GeneralSettings.setUseBots 1
GeneralSettings.setMaxBots 16
GeneralSettings.setMaxBotsIncludeHumans 1
GeneralSettings.setBotSkill 1
GeneralSettings.setLastAwardsCheckDate 1161730798
GeneralSettings.setAllowPunkBuster 1
GeneralSettings.setItemSelectionReverseItems 0
GeneralSettings.setToggleFilters 63490
GeneralSettings.setAutoReload 1
GeneralSettings.setAutoReady 1
GeneralSettings.setConnectionType 4
GeneralSettings.setLCDDisplayModes 0

___________________________end general.con example_____________________________________

_________________________begin shortcut switch example___________________________________

"F:\Games\Battlefield 2\BF2.exe" +menu 1 +fullscreen 1 +username *~blaster~* +playerPassword cornflakes

__________________________end shortcut switch example____________________________________



Thats it I think...sorry its a bit long but I wanted to outline all the parameters as clearly as possible, if something cant be done, or is hard to do, please say so.  Ideally it would be as user friendly as possible so it can be posted on bulletin boards for others to use too, the main 2 problems are outlined above as variable locations of general.con and variable types of switches in the shortcut.

Thanks a lot

Ad

Avatar of MadAd1
MadAd1

ASKER

PS: no thats not my real gaming name or password :)
grep --invert-match addServerHistory input.txt > output.txt

this should simply output all lines that DON'T contain the word "addServerHistory" (case-sensitive). you can add another option for case insensitivity. Grep is available on windows via cygwin.

alternatively in windows xp you could do

find /v "addServerHistory" input.txt > output.txt

here i've assumed that your input file is input.txt and filtered result is placed in output.txt since you'd appreciate not overwriting your original files :0)

HTH
Avatar of Mike Tomlinson
Here is a VBScript (.vbs) to remove those lines:

Option Explicit

Dim fileName
fileName = "C:\Documents and Settings\Ad\My Documents\Battlefield 2\Profiles\0003\General.con"

Dim fso, f
Const ForReading = 1, ForWriting = 2

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(fileName) Then
   ' Read in the whole file
   Set f = fso.OpenTextFile(fileName, ForReading, False)
   Dim lines ' Seperate the file out into individual lines
   lines = Split(f.ReadAll, vbCrLf)    
   f.Close

   ' Set lines we don't want to blank ones
   Dim i, beginning
   For i = LBound(lines) To UBound(lines)
      If Left(lines(i), 32) = "GeneralSettings.addServerHistory" Then
         lines(i) = ""
      End If
   Next

   ' Put the file back together as one string
   lines = Join(lines, vbCrLf)

   ' Take out the multiple blank lines left behind
   If Left(lines, 2) = vbCrLf Then
      lines = Mid(lines, 3)
   End If
   While Instr(lines, vbCrlf & vbCrLf) > 0
      lines = Replace(lines, vbCrLf & vbCrLf, vbCrLf)
   Wend

   ' Overwrite the existing File with the changes
   Set f = fso.OpenTextFile(fileName, ForWriting, True)
   f.Write(lines)  
   f.Close
End If
Variable explaination:
FIXPATH is the directory which holds your general.con
FIXFILE is the name of the file you want to clean, in this case: general.con
TEMPFILE is a just a tempfile it gets removed
EXECCMD is the command you want to execute after the script is complete.

Create a batch file with the following code:

------------------------ BEGIN CUTTING BELOW THIS LINE --------------------------
@ECHO OFF
SET FIXPATH="C:\Documents and Settings\Ad\My Documents\Battlefield 2\Profiles\0003\"
SET FIXFILE="general.con"
SET TEMPFILE="C:\Documents and Settings\Ad\My Documents\Battlefield 2\Profiles\0003\general.tmp"
SET EXECCMD=""F:\Games\Battlefield 2\BF2.exe" +menu 1 +fullscreen 1 +username *~blaster~* +playerPassword cornflakes"

FOR /F "usebackq eol=; tokens=1,2* delims= " %%i IN (`type %FIXPATH%%FIXFILE%`) DO IF NOT "%%i"=="GeneralSettings.addServerHistory" ECHO %%i %%j >> %TEMPFILE%
DEL %FIXPATH%%FIXFILE%
RENAME %TEMPFILE% %FIXFILE%
%EXECCMD%
------------------------ STOP CUTTING HERE --------------------------
Avatar of MadAd1

ASKER

chhokra_expert: Thanks for the reply, however grep and cygwin seem to be additional installables, my console does not recognise grep and I cant seem to find a cygwin dll on my machine (fully updated sp2 instal) so im at a loss knowing how to run it.  While the find /v suggestion seemed to run without errors from a dos console, it didnt do anything.  Yes overwriting the file was required as the game will not run without it.

Idle_Mind: Thanks for the reply, your script does indeed remove the required lines but does not include running the game once terminated.  Ideally id like the script to be a one hit replacement for a shortcut. I tried setting a wshshell.run but as expected it spat it back at me with an error ( ')' expected) at the point after the first shortcut. Oh how I wish I understood wsh scripting properly :(

mattcarver: Thanks for the reply, I can almost understand that and with the setpath commands like that it makes it very user friendly for passing on to non-coders via bulletin boards. It removes the lines as required but the major problem is that it does not run the game either.  I have doubly made sure that the correct path is entered for EXECCMD, with both sets of "" "" but no run.

I had a tinker with it and if I remove all the switches and the outer " ", it does attempt to run, however the menu switch is essential for the game starting correctly so it only gets to the loading screen then errors exactly the same way as if switches are stripped from the normal windows shortcut. In its most simple format, it refuses to run even with ""F:\Games\Battlefield 2\BF2.exe"".  It runs to the same error as described with ""F:\Games\Battle~1\BF2.exe"" but again refuses as soon as switches are added to the 8.3 format. So it seems windows is baulking at two " " sets for some reason?



So all in all, good suggestions so far but theres still that last hurdle of getting the game to actually run. I cannot describe how frustrating it is not being able to sort this out myself.
Let's get rid of EXECCMD and just put your program as the last line in the batch file.  Check to see if this works.

------------------------ BEGIN CUTTING BELOW THIS LINE --------------------------
@ECHO OFF
SET FIXPATH="C:\Documents and Settings\Ad\My Documents\Battlefield 2\Profiles\0003\"
SET FIXFILE="general.con"
SET TEMPFILE="C:\Documents and Settings\Ad\My Documents\Battlefield 2\Profiles\0003\general.tmp"

FOR /F "usebackq eol=; tokens=1,2* delims= " %%i IN (`type %FIXPATH%%FIXFILE%`) DO IF NOT "%%i"=="GeneralSettings.addServerHistory" ECHO %%i %%j >> %TEMPFILE%
DEL %FIXPATH%%FIXFILE%
RENAME %TEMPFILE% %FIXFILE%
"F:\Games\Battlefield 2\BF2.exe" +menu 1 +fullscreen 1 +username *~blaster~* +playerPassword cornflakes
------------------------ STOP CUTTING HERE --------------------------
Try adding this to the bottom of my script:

cmd = Chr(34) & "F:\Games\Battlefield 2\BF2.exe" & Chr(34) & " +menu 1 +fullscreen 1 +username *~blaster~* +playerPassword cornflakes"
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec(cmd)
Avatar of MadAd1

ASKER

Ok, after much fiddling and much googling ive learned a lot but seem to know nothing still.

mattcarver: Thanks for the reply but nothing still, no run.  As soon as I remove the switches it attempts to run, the bf2 loading screen appears but of course errors.  On top of that Ive found a bug- when parsing or writing, it misses off secondary characters from 3 of the lines.

GeneralSettings.setCrosshairColor 255 0 0 255
GeneralSettings.setBuddytagColor 0 0 0
GeneralSettings.setSquadtagColor 0 255 0

becomes
GeneralSettings.setCrosshairColor 255
GeneralSettings.setBuddytagColor 0
GeneralSettings.setSquadtagColor 0

Obviously that is a problem - I was wondering why I was losing my crosshair colour but on investigation found that.


Idle_Mind:  Thanks for the reply.  Had a long session this evening reading through wsh syntax but still no further.  I added in the lines you specified however the first error was "Variable is undefined: 'cmd'.  So I added a Dim cmd which shut that up but created a Variable is undefined: objShell error.

A quick Dim objShell before the line cured that (why I do not know as they appear to be core commands) and suddenly 2 things, first a Variable is undefined: objExecObject pops up and at the same time the game attempts to run, but I can tell its without the switches as it errors out in the usual place.

Ive tried different approaches, most by trial and error but got nowhere.  All the little tutorial examples on wsh create object seem to focus on running trivial programs like notepad or passing a doc file to word and nothing about passing command switches...I cant belive its not possible, It cant be the first time anyones tried to run a program from a script surely?
Avatar of MadAd1

ASKER

Oh and just for simplicities sake, lets make the path and switches

F:\Games\Battlefield 2\BF2.exe" +menu 1 +fullscreen 1

rather than dragging all the account info into it...if/when the above runs then the rest can be added after.
Sorry for the late response.  I fixed the bug with the cutoff.  I also changed the double quotes to surround the entire command string.  I think that may have been the problem,  Here you are:

------------------------ BEGIN CUTTING BELOW THIS LINE --------------------------
@ECHO OFF
SET FIXPATH="C:\Documents and Settings\Ad\My Documents\Battlefield 2\Profiles\0003\"
SET FIXFILE="general.con"
SET TEMPFILE="C:\Documents and Settings\Ad\My Documents\Battlefield 2\Profiles\0003\general.tmp"

FOR /F "usebackq tokens=1* delims= " %%i IN (`type %FIXPATH%%FIXFILE%`) DO IF NOT "%%i"=="GeneralSettings.addServerHistory" ECHO %%i %%j >> %TEMPFILE%
DEL %FIXPATH%%FIXFILE%
RENAME %TEMPFILE% %FIXFILE%
"F:\Games\Battlefield 2\BF2.exe +menu 1 +fullscreen 1 +username *~blaster~* +playerPassword cornflakes"
Avatar of MadAd1

ASKER

I dont know what to say guys....is this task impossible maybe?

Thanks for repaing the script mattcarver, that definately fixed the missing chrs bug however still no luck with the shortcuts...I have tried with one set of "", two sets, nothing seems to work, well, until the switches are removed and then as mentioned before, it errors out.

The only progress I have made is by putting in and executing the script from the battlefield 2 folder, removing the filepath and just using BF2.exe +menu 1 +fullscreen 1 (no quotes) as the target line - now that actually loads the game further but only as far as the accounts screen and as soon as I retry with the added in correct +username *~blaster~* +playerPassword cornflakes details, or, use any path behind the bf2.exe, then its back to no go.

I think idle_mind has given up already and I wouldnt blame you if you did too, I didnt realise it would be such an insummountable task.  Because of the response to the BF2.exe +menu 1 +fullscreen 1 and not any more, im beginning to think theres some special interraction between C written game engines and WinXP shortcuts that cannot be triggererd in dos or wsh.

I did try using the either script as the first part of a multi file bookmark but it would only execute the script and not the game shortcut. No matter what I put in it comes out "F:\Games\Battlefield 2\clr3.bat" "F:\Games\Battlefield 2\BF2.exe" +menu 1 +fullscreen 1 *~blaster~* +playerPassword cornflakes

Dont really know where to go from here.
ASKER CERTIFIED SOLUTION
Avatar of mattcarver
mattcarver
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MadAd1

ASKER

OK! now we are in business....at least on the DOS side, Im getting a bf2link.lnk is not a valid win 32 application from the VBS even with my dim patchups, but at least we've got progress on one front, I thought wed never get there, many thanks for being patient matt.

_____________INTERIM CODE _______________

@ECHO OFF
SET FIXPATH="%USERPROFILE%\My Documents\Battlefield 2\Profiles\0003\"
SET FIXFILE="General.con"
SET TEMPFILE="%TEMP%\General.con"

FOR /F "usebackq tokens=1* delims= " %%i IN (`type %FIXPATH%%FIXFILE%`) DO IF NOT "%%i"=="GeneralSettings.addServerHistory" ECHO %%i %%j >> %TEMPFILE%
MOVE %TEMPFILE% %FIXPATH%
bf2link.lnk
_____________END CODE______________________


Right as you can see ive streamlined it a little :) Do you think this will be robust (even though it works for me)?  I  didnt trust anyone to work out adding general.tmp to the end of the path no matter how well I explained. Also using %userprofile% means thats that user interaction is reduced to just one thing, adding in the correct profile number.


Now the points are yours matt, as the following is outside the original question, but im thinking that to reduce user interaction to zero i need to parse a file called Global.con one directory up that holds a text string pointing to which the live account is. Ive made a stab at what it might look like but I dont know how to parse the data properly.

_____________GLOBAL.CON CONTENTS_____________________

GlobalSettings.setDefaultUser "0003"
GlobalSettings.setNamePrefix ""

______________END GLOBAL.CON__________________________


Obviously 0003 is my setting, its numbered from 1, and I wouldn't expect people to have more than 9 accounts but it might happen.

In my playing about I got this far

______________TEST CODE__________________________________

@ECHO OFF
SET LIVEPRO=""
SET FIXPATH="%USERPROFILE%\My Documents\Battlefield 2\Profiles\%LIVEPRO%\"
SET FIXFILE="General.con"
SET TEMPFILE="%TEMP%\General.con"
SET GLOBFILE="%USERPROFILE%\My Documents\Battlefield 2\Profiles\Global.con"

FOR /F "usebackq tokens=1* delims=" %%k IN (%GLOBFILE%) DO IF "%%k"=="GlobalSettings.setDefaultUser" SET LIVEPRO=%%l

FOR /F "usebackq tokens=1* delims= " %%i IN (`type %FIXPATH%%FIXFILE%`) DO IF NOT "%%i"=="GeneralSettings.addServerHistory" ECHO %%i %%j >> %TEMPFILE%
MOVE %TEMPFILE% %FIXPATH%
pause
rem bf2link.lnk

__________________________END TEST CODE___________________________________

My thinking is making a variable LIVEPRO and using that to hold the parsed 4 digits from Global.con and plug that in with FIXPATH.  That way it would always pick the live account but I cant get the damn thing to run- I just get back 2x 'system cannot find the file specified' errors.- is it my syntax or my attempt at a variable variable?

Thanks
Avatar of MadAd1

ASKER

I still cant get this right

if I do a test script with just this lot in

SET GLOBFILE="%USERPROFILE%\My Documents\Battlefield 2\Profiles\Global.con"
SET TESTFILE="%TEMP%\test.con"
FOR /F "usebackq tokens=1*" %%k IN (%GLOBFILE%) DO IF "%%k"=="GlobalSettings.setDefaultUser" ECHO %%l >> %TESTFILE%

I get exactly what I expect, a file called test.con in temp with the text "0003" inside it.  However in the main script perhaps %LIVEPRO% gets ""0003"" passed to it?? Or maybe my SET LIVEPRO=%%l instead of the ECHO is messing things up?

The only errors I get are the 2x 'system cannot find the file specified' errors, that is solely from your line not finding the full FIXPATH variable (which is understandable) but other than that it appears error free.

But does not work.

If I do this

_______________________TEST________________________________
@ECHO OFF
SET LIVEPRO="0003"
SET FIXPATH="%USERPROFILE%\My Documents\Battlefield 2\Profiles\%LIVEPRO%\"
SET FIXFILE="General.con"
SET TEMPFILE="%TEMP%\General.con"
SET GLOBFILE="%USERPROFILE%\My Documents\Battlefield 2\Profiles\Global.con"


FOR /F "usebackq tokens=1*" %%k IN (%GLOBFILE%) DO IF "%%k"=="GlobalSettings.setDefaultUser" SET LIVEPRO="%%l"

FOR /F "usebackq tokens=1* delims= " %%i IN (`type %FIXPATH%%FIXFILE%`) DO IF NOT "%%i"=="GeneralSettings.addServerHistory" ECHO %%i %%j >> %TEMPFILE%
MOVE %TEMPFILE% %FIXPATH%

___________________________END TEST__________________________________

It runs but completely ignores my FOR /F line, because if I change the 0003 to 0002 (expecting it to plug 0003 instead of the 0002 set) again its broken therefore its evident that its not setting the LIVEPRO variable properly.

argggghhh!
Avatar of MadAd1

ASKER

FIXED! (at last).  Used %%~ to remove the outer quotes from the 003, plus had to make sure LIVEPRO sets before its read.  If theres a way to optimise it, feel free to comment.

__________________________FINAL SCRIPT__________________________
@ECHO OFF
rem dont pester me, just run

rem find out which profile last run
SET GLOBFILE="%USERPROFILE%\My Documents\Battlefield 2\Profiles\Global.con"
FOR /F "usebackq tokens=1* delims= " %%k IN (%GLOBFILE%) DO IF "%%k"=="GlobalSettings.setDefaultUser" SET LIVEPRO="%%~l"

rem clear server history
SET FIXPATH="%USERPROFILE%\My Documents\Battlefield 2\Profiles\%LIVEPRO%\"
SET FIXFILE="General.con"
SET TEMPFILE="%TEMP%\General.con"
FOR /F "usebackq tokens=1* delims= " %%i IN (`type %FIXPATH%%FIXFILE%`) DO IF NOT "%%i"=="GeneralSettings.addServerHistory" ECHO %%i %%j >> %TEMPFILE%
MOVE %TEMPFILE% %FIXPATH%

rem load new shortcut because extended bf2 switches wont run from batch
bf2link.lnk

_______________________END SCRIPT__________________________________


So there we have it, apart from the hassle of copying over a shortcut, theres no input needed. Im still going to investigate why I can't use the full set of switches in a batch file but for now ive got something close.

Thank you very much to mattcarver for code and inspiration and thanks also to idle_mind for trying.