Hi VB experts! (First post)
I have a large number of data files for a game, they are text files but have various extensions (like .weapon or .shipsection). These files contain a code hook and a integer,boolean or string separated by a space and or tab. There are a set list of available code hook variables (around 250) but each text file only specifies relevant ones to be changed from default. I would like to have 2 Macros, one to import existing txt files and one to export. The goal is to be able to have a database to manage these individual txt files without editing them manually.
Here is one of the .txt files
In excel I have a page intended to be a export template containing all the existing code hook types. I want to create another page using the importer with columns for every hook instance and rows for each txt file instance. Column 1 must be the txt file name and the rest of the columns populated only if the hook is present and with the value associated. Export would work by reading values off the database for each row into the template page if they exist for each column and create a text file using tab as delimiter.
Here is the scripts I have so far:
Import:
Export:
I used a script with directory navigation but the import one does not work properly...Is this too much to ask of macros?
Thanks in advance for any responses
I have a large number of data files for a game, they are text files but have various extensions (like .weapon or .shipsection). These files contain a code hook and a integer,boolean or string separated by a space and or tab. There are a set list of available code hook variables (around 250) but each text file only specifies relevant ones to be changed from default. I would like to have 2 Macros, one to import existing txt files and one to export. The goal is to be able to have a database to manage these individual txt files without editing them manually.
Here is one of the .txt files
Code:
weapon
{
name 'PD Interceptor'
weaponclass pdmissile
weaponfamily missile
model1 "" // insert barrel model for tiny mounts here if we get them
model2 barrel_pdmissile.X // NOTE: model2 for tiny pd weapons in small mounts
requires WEP_PDtech
requires DRV_Ints
turretsize tiny
cost 4000
turretclass standard
burst_volleys 1
volley_period .2
recharge_time 8
muzzle_sound Sounds/Weapons/mis_interceptor_pd_muzzle.wav
muzzle_sound_minrange 60
muzzle_effect effects/Missile_dfire_muzzle.effect
muzzle_speed 100
solution_tolerance 360
icon_file GUI/WeaponIcons_Warhead.tga
icon_rect "128 192 64 64"
range 700
range_planet 0 // Same as 'range' because missile has a fixed time-to-live. (range should be a bit less than [netforcelimits] speed x ttl)
fc_requires_los true
fc_requires_inrange true
fc_requires_enemycolony false
fc_manual_target false
fc_manual_toggle false
fc_manual_launch false
fc_controllable false
fc_holdsfire false
blindfire false
pinpoint false
missile
{
tracking 1
pd 1
warhead_dam_scale 1 // add standard missile warhead damage modifier
impact_decal decals/WeaponHit.decal
impact_decal_width .5
impact_decal_height .5
impact_decal_depth .1
beam_origin -3
beam_length 5.5
impact_sound Sounds/Weapons/mis_interceptor_pd_impact.wav
impact_sound_minrange 200
impact_effect effects/mis_tarka_dfire_impact.effect
area_impact_effect "effects/collide_asteroid.effect"
Planet_Impact_Effect "effects/mis_pd_impact.effect"
thrust_sound Sounds/Weapons/mis_missile_travel.wav
speed 350
seek_attenuation 11 // Higher number = tighter turns/faster accel
ttl 2.5
model missile.X
health 15
dam 60
dam_radius 0
mass 25
thrust_effect effects/Missile_small_trail.effect
thrust_node EffectNode
dumbfire_period 0.5
dam_pop 0
dam_infra 0
dam_terra 0
}
rating_frate 3
rating_dam 3
rating_acc 10
rating_range 7
dam_est 60
}
In excel I have a page intended to be a export template containing all the existing code hook types. I want to create another page using the importer with columns for every hook instance and rows for each txt file instance. Column 1 must be the txt file name and the rest of the columns populated only if the hook is present and with the value associated. Export would work by reading values off the database for each row into the template page if they exist for each column and create a text file using tab as delimiter.
Here is the scripts I have so far:
Import:
Code:
' ---------------------- Directory Choosing Helper Functions -----------------------
' Excel and VBA do not provide any convenient directory chooser or file chooser
' dialogs, but these functions will provide a reference to a system DLL
' with the necessary capabilities
Private Type BROWSEINFO ' used by the function GetFolderNameB
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Function GetFolderNameB(Msg As String) As String
' returns the name of the folder selected by the user
Dim bInfo As BROWSEINFO, path As String, r As Long
Dim X As Long, pos As Integer
bInfo.pidlRoot = 0& ' Root folder = Desktop
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
' the dialog title
Else
bInfo.lpszTitle = Msg ' the dialog title
End If
bInfo.ulFlags = &H1 ' Type of directory to return
X = SHBrowseForFolder(bInfo) ' display the dialog
' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal X, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetFolderNameB = Left(path, pos - 1)
Else
GetFolderNameB = ""
End If
End Function
'---------------------- END Directory Chooser Helper Functions ----------------------
Sub ImportManyTXTIntoColumns()
'Author: Jerry Beaucaire
'Date: 2/24/2012
'Summary: From a specific folder, import TXT files 1 file per column
Dim fPath As String, fTXT As String
Application.ScreenUpdating = False
fPath = GetFolderNameB("Choose the folder to import .weapon files from:")
If fPath = "" Then
MsgBox ("You didn't choose an import directory. Nothing will be imported.")
Exit Sub
End If
Set wsTrgt = ThisWorkbook.Sheets.Add 'new sheet for incoming data
NC = 1 'first column for data
fTXT = Dir(fPath & "*.txt") 'get first filename
Do While Len(fTXT) > 0 'process one at a time
'open the file in Excel
Workbooks.OpenText fPath & fTXT, Origin:=437
'put the filename in the target column
wsTrgt.Cells(1, NC) = ActiveSheet.Name
'copy column A to new sheet
Range("A:A").SpecialCells(xlConstants).Copy wsTrgt.Cells(2, NC)
ActiveWorkbook.Close False 'close the source file
NC = NC + 1 'next column
fTXT = Dir 'next file
Loop
Application.ScreenUpdating = True
End Sub
Export:
Code:
' ---------------------- Directory Choosing Helper Functions -----------------------
' Excel and VBA do not provide any convenient directory chooser or file chooser
' dialogs, but these functions will provide a reference to a system DLL
' with the necessary capabilities
Private Type BROWSEINFO ' used by the function GetFolderName
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Function GetFolderName(Msg As String) As String
' returns the name of the folder selected by the user
Dim bInfo As BROWSEINFO, path As String, r As Long
Dim X As Long, pos As Integer
bInfo.pidlRoot = 0& ' Root folder = Desktop
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
' the dialog title
Else
bInfo.lpszTitle = Msg ' the dialog title
End If
bInfo.ulFlags = &H1 ' Type of directory to return
X = SHBrowseForFolder(bInfo) ' display the dialog
' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal X, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetFolderName = Left(path, pos - 1)
Else
GetFolderName = ""
End If
End Function
'---------------------- END Directory Chooser Helper Functions ----------------------
Public Sub DoTheExport()
Dim FName As Variant
Dim Sep As String
Dim wsSheet As Worksheet
Dim nFileNum As Integer
Dim csvPath As String
Sep = " "
'csvPath = InputBox("Enter the full path to export .weapon files to: ")
csvPath = GetFolderName("Choose the folder to export CSV files to:")
If csvPath = "" Then
MsgBox ("You didn't choose an export directory. Nothing will be exported.")
Exit Sub
End If
For Each wsSheet In Worksheets
wsSheet.Activate
nFileNum = FreeFile
Open csvPath & "\" & _
wsSheet.Name & ".shipsection" For Output As #nFileNum
ExportToTextFile CStr(nFileNum), Sep, False
Close nFileNum
Next wsSheet
End Sub
Public Sub ExportToTextFile(nFileNum As Integer, _
Sep As String, SelectionOnly As Boolean)
Dim WholeLine As String
Dim RowNdx As Long
Dim ColNdx As Integer
Dim StartRow As Long
Dim EndRow As Long
Dim StartCol As Integer
Dim EndCol As Integer
Dim CellValue As String
Application.ScreenUpdating = False
On Error GoTo EndMacro:
If SelectionOnly = True Then
With Selection
StartRow = .Cells(1).Row
StartCol = .Cells(1).Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With
Else
With ActiveSheet.UsedRange
StartRow = .Cells(1).Row
StartCol = .Cells(1).Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With
End If
For RowNdx = StartRow To EndRow
WholeLine = ""
For ColNdx = StartCol To EndCol
If Cells(RowNdx, ColNdx).Value = "" Then
CellValue = ""
Else
CellValue = Cells(RowNdx, ColNdx).Value
End If
WholeLine = WholeLine & CellValue & Sep
Next ColNdx
WholeLine = Left(WholeLine, Len(WholeLine) - Len(Sep))
Print #nFileNum, WholeLine
Next RowNdx
EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
End Sub
I used a script with directory navigation but the import one does not work properly...Is this too much to ask of macros?
Thanks in advance for any responses