Author Topic: [TUTORIAL] Use Word to make dbr edits  (Read 1013 times)

0 Members and 1 Guest are viewing this topic.

Not Yet Rated!

Offline undefind

  • Full Member
  • *
  • Topic Author
  • Posts: 114
  • Country: us
  • Karma: +2/-0
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: -1
[TUTORIAL] Use Word to make dbr edits
« on: 10 August 2019, 00:40:52 »
The below code will loop through all the files within a selected folder whose name begins with "mi_" and ends with ".dbr". It will then search for all occurrences of the word "Rare" and replace it with "Quest". This is the same as using art manager to change the "ItemClassification" from "Rare" to "Quest". I finally got off my butt and decided to actually make this worth using. I edited the code below but it requires a userform to be created and placed within the userform's coding window.

What it does:

1. You select the number of search terms to use
2. Put those terms in the boxes labeled "Search for what?" i.e bat, or cat, or yeti
3. Put the text you want to replace it with in the boxes labeled "Replace with what?"
4. Click the checkbox if you want the program to cycle through all the subfolders
5. Click the "Browse" button to choose the folder to start in
6. Put an extension in the "File extension" text box, i.e. dbr. You don't have to use this feature but it can help if you are trying to narrow things down
7. The "OK" button starts, "Clear Form" clears the form, and "Exit" makes a sandwich

If you dont want to make a new userform I have also uploaded the document that contains both the macro and the userform here https://mega.nz/#F!300lGKzS!4DrmQZk6CWNQDeaU7GG76A

Just push the button to open the form. Be careful though, if you put an ambiguous search term in such as "X" or "mag" it will do its thing to ALL files that contain the letter "X" or files that contain the text "mag". However, this program only copies files and renames the copies so there shouldn't be an issue anyway.

Here is a pic of the form

Warning: If you are unfamiliar with VBA coding you should use this on a test project first. If you have questions on how to get started give me a shout. If enough people need to know I will edit this tutorial for beginners.


Spoiler for Hiden:

Option Explicit
Public fso As Object
Public MyNewFile As String
Public i As Integer
Public myPath As String
Public FldrPicker As FileDialog
Private Sub showme()
 
frmCopy_Rename.Show
 
End Sub
Private Sub Browsebtn_Click()
 
Application.ScreenUpdating = False
 
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
 
    With FldrPicker
      .Title = "Select A Target Folder"
      .AllowMultiSelect = False
        If .Show = 0 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
    End With
   
NextCode:
  myPath = myPath
    If myPath = "" Then
        MsgBox "You must select a folder!"
        Application.ScreenUpdating = True
        Exit Sub
    End If
 
End Sub
Sub SubFoldersloop(myFolder As Object)
 
Dim mySubFolder As Object
Dim myFile As Object
Dim Check(1 To 3)
Dim Rplace(1 To 3)
 
If SubFldrCheckBox.Value = True Then
    For Each mySubFolder In myFolder.subfolders
        SubFoldersloop mySubFolder
    Next
End If
i = 1
 
  Check(1) = CheckTextBox1.Value
  Check(2) = CheckTextBox2.Value
  Check(3) = CheckTextBox3.Value
  Rplace(1) = RplaceTextBox1.Value
  Rplace(2) = RplaceTextBox2.Value
  Rplace(3) = RplaceTextBox3.Value
 
Cycle:
For Each myFile In myFolder.Files
    If myFile Like "*" & Check(i) & "*" & extTextBox.Value Then
        MyNewFile = Replace(myFile, Check(i), Rplace(i))
        fso.copyfile myFile, MyNewFile
    End If
    DoEvents
Next
  If NumbSearchesComboBox.Value > i Then
    i = i + 1
    GoTo Cycle
  End If
 
End Sub
 
Private Sub Clearbtn_Click()
 
Call UserForm_Initialize
 
End Sub
 
 
Private Sub Exitbtn_Click()
 
Unload Me
 
End Sub
 
Private Sub OKbtn_Click()
 
Dim myFolder As Object
 
Set fso = CreateObject("Scripting.FileSystemObject")
 
SubFoldersloop fso.GetFolder(myPath)
 
MsgBox "Mission Success!"
Application.ScreenUpdating = True
 
End Sub


Private Sub UserForm_Initialize()
 
CheckTextBox1.Value = ""
CheckTextBox2.Value = ""
CheckTextBox3.Value = ""
RplaceTextBox1.Value = ""
RplaceTextBox2.Value = ""
RplaceTextBox3.Value = ""
NumbSearchesComboBox.Clear
 
With NumbSearchesComboBox
    .AddItem "1"
    .AddItem "2"
    .AddItem "3"
End With
 
End Sub







« Last Edit: 24 August 2019, 03:58:15 by undefind »
My mods and some other mods

https://mega.nz/#F!v5tGhawJ!BeMd_SSWpWZAeG4h3F2YFg

ATTENTION! .arz must be the same name as the name of the mod. i.e. custommaps --> DropSounds + Massbosses + Mis are epic + affixes are common

Database needs to have that long name (or change folder name)

Also on Steam

Not Yet Rated!

Offline undefind

  • Full Member
  • *
  • Topic Author
  • Posts: 114
  • Country: us
  • Karma: +2/-0
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: -1
Re: [TUTORIAL] Use Word to make dbr edits
« Reply #1 on: 10 August 2019, 17:51:26 »
fixed the code so hitting "Cancel" exits the subroutine.
My mods and some other mods

https://mega.nz/#F!v5tGhawJ!BeMd_SSWpWZAeG4h3F2YFg

ATTENTION! .arz must be the same name as the name of the mod. i.e. custommaps --> DropSounds + Massbosses + Mis are epic + affixes are common

Database needs to have that long name (or change folder name)

Also on Steam

Not Yet Rated!

Offline WNG

  • Forum Moderator
  • Full Member
  • *
  • Posts: 558
  • Country: ca
  • Karma: +7/-0
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: -4
Re: [TUTORIAL] Use Word to make dbr edits
« Reply #2 on: 10 August 2019, 20:48:04 »
I was actually searching for something like this but I wasn't sure how to proceed. I figured VBA would be the simplest but still I can't quite figure how to make it work.

How hard would it be to do the following :

I have a large volume of .dbrs I want to create copies of. All those .dbrs have a prefix and a suffix. I want to copy all those .dbrs and create a copy with a different prefix in the filename, and would appear in the same folder.

Not Yet Rated!

Offline undefind

  • Full Member
  • *
  • Topic Author
  • Posts: 114
  • Country: us
  • Karma: +2/-0
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: -1
Re: [TUTORIAL] Use Word to make dbr edits
« Reply #3 on: 10 August 2019, 23:23:53 »
not hard. can you give me a specific example?

or better yet, send me all the files with what you want done to them and i can show you how i would do it.

edit: im actually getting ready to do something similar with affixes right now. getting ready to write the code.
« Last Edit: 10 August 2019, 23:25:39 by undefind »
My mods and some other mods

https://mega.nz/#F!v5tGhawJ!BeMd_SSWpWZAeG4h3F2YFg

ATTENTION! .arz must be the same name as the name of the mod. i.e. custommaps --> DropSounds + Massbosses + Mis are epic + affixes are common

Database needs to have that long name (or change folder name)

Also on Steam

Not Yet Rated!

Offline WNG

  • Forum Moderator
  • Full Member
  • *
  • Posts: 558
  • Country: ca
  • Karma: +7/-0
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: -4
Re: [TUTORIAL] Use Word to make dbr edits
« Reply #4 on: 11 August 2019, 00:45:47 »
Here's an example :

There would be two parameters : the tag to search for, and the tag to replace it with.

A folder contains those .dbrs :

Code: [Select]
item_common.dbr
item_epic.dbr
item_legendary.dbr

I want to copy those files, and rename their copy by replacing the tag "item" with "armor". As a result, the content of the folder after executing the code would look like :


Code: [Select]
item_common.dbr
item_epic.dbr
item_legendary.dbr
armor_common.dbr
armor_epic.dbr
armor_legendary.dbr

Not Yet Rated!

Offline undefind

  • Full Member
  • *
  • Topic Author
  • Posts: 114
  • Country: us
  • Karma: +2/-0
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: -1
Re: [TUTORIAL] Use Word to make dbr edits
« Reply #5 on: 11 August 2019, 01:52:21 »
seems easy enough. give me a few minutes.
My mods and some other mods

https://mega.nz/#F!v5tGhawJ!BeMd_SSWpWZAeG4h3F2YFg

ATTENTION! .arz must be the same name as the name of the mod. i.e. custommaps --> DropSounds + Massbosses + Mis are epic + affixes are common

Database needs to have that long name (or change folder name)

Also on Steam

Not Yet Rated!

Offline undefind

  • Full Member
  • *
  • Topic Author
  • Posts: 114
  • Country: us
  • Karma: +2/-0
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: -1
Re: [TUTORIAL] Use Word to make dbr edits
« Reply #6 on: 11 August 2019, 03:13:58 »
Here's an example :

There would be two parameters : the tag to search for, and the tag to replace it with.

A folder contains those .dbrs :

Code: [Select]
item_common.dbr
item_epic.dbr
item_legendary.dbr

I want to copy those files, and rename their copy by replacing the tag "item" with "armor". As a result, the content of the folder after executing the code would look like :


Code: [Select]
item_common.dbr
item_epic.dbr
item_legendary.dbr
armor_common.dbr
armor_epic.dbr
armor_legendary.dbr

Code: [Select]
Sub CopyandRename()

Dim wd As Document
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog

Application.ScreenUpdating = False
 
 
Restart:   Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

    With FldrPicker
      .Title = "Select A Target Folder"
      .AllowMultiSelect = False
        If .Show = 0 Then myPath = ""
       

        myPath = .SelectedItems(1) & "\"
        If myPath = "" Then GoTo NextCode
    End With


NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings
 
 
 
 
  myExtension = "item_*.dbr"
  myFile = Dir(myPath & myExtension)

  Do While myFile <> ""
    Set wd = Word.Documents.Open(FileName:=myPath & myFile)
    DoEvents
   
    If myFile = "item_common.dbr" Then
        ActiveDocument.SaveAs2 myPath & "armor_common.dbr"
    ElseIf myFile = "item_epic.dbr" Then
        ActiveDocument.SaveAs2 myPath & "armor_epic.dbr"
    ElseIf myFile = "item_legendary.dbr" Then
        ActiveDocument.SaveAs2 myPath & "armor_legendary.dbr"
    End If


    ActiveWindow.Close

    DoEvents

    myFile = Dir
   
  Loop
 
GoTo Restart

ResetSettings:

    MsgBox "Task Complete!"

    Application.ScreenUpdating = True

End Sub

let me know if this works.

sorry it took so long, im drinking and i have 4 kids who wont stfu.
My mods and some other mods

https://mega.nz/#F!v5tGhawJ!BeMd_SSWpWZAeG4h3F2YFg

ATTENTION! .arz must be the same name as the name of the mod. i.e. custommaps --> DropSounds + Massbosses + Mis are epic + affixes are common

Database needs to have that long name (or change folder name)

Also on Steam

Not Yet Rated!

Offline WNG

  • Forum Moderator
  • Full Member
  • *
  • Posts: 558
  • Country: ca
  • Karma: +7/-0
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: -4
Re: [TUTORIAL] Use Word to make dbr edits
« Reply #7 on: 11 August 2019, 03:41:49 »
I understand a bit of the code, but if I understood it proprely, it might need a bit of a change.

Code: [Select]
myExtension = "item_*.dbr"
  myFile = Dir(myPath & myExtension)

  Do While myFile <> ""
    Set wd = Word.Documents.Open(FileName:=myPath & myFile)
    DoEvents
   
    If myFile = "item_common.dbr" Then
        ActiveDocument.SaveAs2 myPath & "armor_common.dbr"
    ElseIf myFile = "item_epic.dbr" Then
        ActiveDocument.SaveAs2 myPath & "armor_epic.dbr"
    ElseIf myFile = "item_legendary.dbr" Then
        ActiveDocument.SaveAs2 myPath & "armor_legendary.dbr"
    End If

Is there a way for it to replace all characters before the "_" for a set prefix instead of relying on ElseIfs? (since the suffix or number of items can vary)

Not Yet Rated!

Offline undefind

  • Full Member
  • *
  • Topic Author
  • Posts: 114
  • Country: us
  • Karma: +2/-0
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: -1
Re: [TUTORIAL] Use Word to make dbr edits
« Reply #8 on: 11 August 2019, 03:45:37 »
yeah, was hoping you wouldnt say that.

not sure i have time for that right now. ive been trying to work on something all day and have made little progress. i wanna get this done before i forget what im doing.
My mods and some other mods

https://mega.nz/#F!v5tGhawJ!BeMd_SSWpWZAeG4h3F2YFg

ATTENTION! .arz must be the same name as the name of the mod. i.e. custommaps --> DropSounds + Massbosses + Mis are epic + affixes are common

Database needs to have that long name (or change folder name)

Also on Steam

Not Yet Rated!

Offline WNG

  • Forum Moderator
  • Full Member
  • *
  • Posts: 558
  • Country: ca
  • Karma: +7/-0
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: -4
Re: [TUTORIAL] Use Word to make dbr edits
« Reply #9 on: 11 August 2019, 03:50:13 »
Uh, no pressure. I wouldn't like to put too much weight on anyone. It's best you resolve your more important matters first.

Not Yet Rated!

Offline undefind

  • Full Member
  • *
  • Topic Author
  • Posts: 114
  • Country: us
  • Karma: +2/-0
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: -1
Re: [TUTORIAL] Use Word to make dbr edits
« Reply #10 on: 11 August 2019, 03:57:30 »
its not important. im just drunk and everyone wants me to do modding stuff for them.

and the stuff im trying to do is more modding.
My mods and some other mods

https://mega.nz/#F!v5tGhawJ!BeMd_SSWpWZAeG4h3F2YFg

ATTENTION! .arz must be the same name as the name of the mod. i.e. custommaps --> DropSounds + Massbosses + Mis are epic + affixes are common

Database needs to have that long name (or change folder name)

Also on Steam

Not Yet Rated!

Offline undefind

  • Full Member
  • *
  • Topic Author
  • Posts: 114
  • Country: us
  • Karma: +2/-0
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: -1
Re: [TUTORIAL] Use Word to make dbr edits
« Reply #11 on: 11 August 2019, 20:57:27 »
Code: [Select]
Sub CopyandRename()

Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim MyLength As Long
Dim MyCopy As String
Dim MyText As String
Dim message As Variable
Dim fso As Object
Dim MyNewFile As String

Set fso = CreateObject("Scripting.FileSystemObject")

Application.ScreenUpdating = False
 
Result = MsgBox("This program will copy and rename .dbr files that you specify. Press OK to continue or Cancel to leave", vbOKCancel)
If Result = vbCancel Then Exit Sub

Restart:   Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

Result = MsgBox("Pick the folder you want to start in or click Cancel if you are done", vbOKCancel)
If Result = vbCancel Then Exit Sub

    With FldrPicker
      .Title = "Select A Target Folder"
      .AllowMultiSelect = False
        If .Show = 0 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
        If myPath = "" Then GoTo NextCode
    End With

NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings

  myExtension = "*.dbr"
  myFile = Dir(myPath & myExtension)
 
    Check1 = InputBox("What is the word that exists in all files you want to check for? i.e. common", , "common")
    If Check1 = vbNullString Or Check1 = "" Then
    Result = MsgBox("Clicking Cancel or leaving the text box empty exits this program")
    Exit Sub
    End If
    Check2 = InputBox("What is the next word that exists in all files you want to check for? i.e. epic", , "epic")
    If Check2 = vbNullString Or Check2 = "" Then
    Result = MsgBox("Clicking Cancel or leaving the text box empty exits this program")
    Exit Sub
    End If
    Check3 = InputBox("What is the next word that exists in all files you want to check for? i.e legendary", , "legendary")
    If Check3 = vbNullString Or Check3 = "" Then
    Result = MsgBox("Clicking Cancel or leaving the text box empty exits this program")
    Exit Sub
    End If
    Check4 = InputBox("What is the word you want to add to the beginning of the file name? i.e armor_. The resulting file will look like ""armor_common.dbr"" be sure to include the underscore or any other characters", , "armor_")
    If Check4 = vbNullString Or Check4 = "" Then
    Result = MsgBox("Clicking Cancel or leaving the text box empty exits this program")
    Exit Sub
    End If

  Do While myFile <> ""
    If myFile Like "*" & Check1 & "*.dbr" Then
        MyLength = InStr(1, myFile, Check1) - 1
        MyText = Left(myFile, MyLength)
        MyCopy = Replace(myFile, MyText, Check4)
        MyNewFile = myPath & MyCopy
        fso.copyfile myPath & myFile, MyNewFile
    ElseIf myFile Like "*" & Check2 & "*.dbr" Then
        MyLength = InStr(1, myFile, Check2) - 1
        MyText = Left(myFile, MyLength)
        MyCopy = Replace(myFile, MyText, Check4)
        MyNewFile = myPath & MyCopy
        fso.copyfile myPath & myFile, MyNewFile
    ElseIf myFile Like "*" & Check3 & "*.dbr" Then
        MyLength = InStr(1, myFile, Check3) - 1
        MyText = Left(myFile, MyLength)
        MyCopy = Replace(myFile, MyText, Check4)
        MyNewFile = myPath & MyCopy
        fso.copyfile myPath & myFile, MyNewFile
    End If
           
    DoEvents

    myFile = Dir
   
  Loop
 
GoTo Restart

ResetSettings:

    MsgBox "Task Complete!"

    Application.ScreenUpdating = True

End Sub


Sorry it took so long.
My mods and some other mods

https://mega.nz/#F!v5tGhawJ!BeMd_SSWpWZAeG4h3F2YFg

ATTENTION! .arz must be the same name as the name of the mod. i.e. custommaps --> DropSounds + Massbosses + Mis are epic + affixes are common

Database needs to have that long name (or change folder name)

Also on Steam

Not Yet Rated!

Offline undefind

  • Full Member
  • *
  • Topic Author
  • Posts: 114
  • Country: us
  • Karma: +2/-0
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: -1
Re: [TUTORIAL] Use Word to make dbr edits
« Reply #12 on: 24 August 2019, 03:59:13 »
I edited the original post to reflect a much better alternative. Super easy to rename files.
My mods and some other mods

https://mega.nz/#F!v5tGhawJ!BeMd_SSWpWZAeG4h3F2YFg

ATTENTION! .arz must be the same name as the name of the mod. i.e. custommaps --> DropSounds + Massbosses + Mis are epic + affixes are common

Database needs to have that long name (or change folder name)

Also on Steam

Tags: