hexagon logo

Help with script to change text file

Hi everyone, I am still very much a novice with scripts but I have just worked on a very simple one to change the line in a text file.

This works perfectly for what I am trying to do, however I have to write the exact file name every time I want it to change the words, which is obviously slower than me going in and altering it by hand which is what I do now.

I am running this from excel by the way using a button.

Firstly I would like to know if what I want is possible. Secondly what I want is for me to be able to click a button in excel and it will make the required change to all the dmo files in a single folder without me having to change the file name.

So if I say this is the file path "S:\DMO Files from CMM" and it will search the folder and make the changes to all the files. Is this possible?

Basically I have no idea how to go about this at the moment so any help would be great.

Sub Button1_Click()

Const ForReading = 1

Const ForWriting = 2


Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("S:\DMO Files from CMM\LH Anodised Silver 02.02.2018 - 1.dmo", ForReading)


strText = objFile.ReadAll

objFile.Close

strNewText = Replace(strText, "FILNAM/'31862383.dmo'", "FILNAM/'31862383'")


Set objFile = objFSO.OpenTextFile("S:\DMO Files from CMM\LH Anodised Silver 02.02.2018 - 1.dmo", ForWriting)

objFile.WriteLine strNewText

objFile.Close


End Sub
Parents
  • Something like this?

    
    Sub Button1_Click()
    
    Const ForReading = 1
    
    Const ForWriting = 2
    
    'declare the object as a the type it is, this gives intelisence (predictive text) in the VB environment
    'makes find out what properties and methods exist for an object so much easier
    
    Dim objFSO As Scripting.FileSystemObject
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    Set objDir = objFSO.GetFolder("C:\temp\test folder")
    
    Dim objFile As Scripting.File
    
    For Each objFile In objDir.Files
    
    Dim ts As Scripting.TextStream
    
    Set ts = objFile.OpenAsTextStream(ForReading, TristateUseDefault)
    
    strText = ts.ReadAll
    
    strNewText = Replace(strText, "hello", "goodbye")
    
    Set ts = objFile.OpenAsTextStream(ForWriting, TristateUseDefault)
    
    ts.Write strNewText
    
    
    Next objFile
    
    
    
    End Sub
    
    
Reply
  • Something like this?

    
    Sub Button1_Click()
    
    Const ForReading = 1
    
    Const ForWriting = 2
    
    'declare the object as a the type it is, this gives intelisence (predictive text) in the VB environment
    'makes find out what properties and methods exist for an object so much easier
    
    Dim objFSO As Scripting.FileSystemObject
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    Set objDir = objFSO.GetFolder("C:\temp\test folder")
    
    Dim objFile As Scripting.File
    
    For Each objFile In objDir.Files
    
    Dim ts As Scripting.TextStream
    
    Set ts = objFile.OpenAsTextStream(ForReading, TristateUseDefault)
    
    strText = ts.ReadAll
    
    strNewText = Replace(strText, "hello", "goodbye")
    
    Set ts = objFile.OpenAsTextStream(ForWriting, TristateUseDefault)
    
    ts.Write strNewText
    
    
    Next objFile
    
    
    
    End Sub
    
    
Children
No Data