Jump to content

String Replace in Files - (Moved)


ShupDogg
 Share

Recommended Posts

I am looking for a script to replace the string in 2 different files types.
I have .hod files and .ws files. These are AS/400 Session file types.
If you open the files with a text editor you can change the Host they are connecting to.

I need to change if needed to a Standard Host name.

.HOD file has a line host=X.X.X.X  or something of the sort and I need it to be host=DNSName

.WS file has a line HostName=X.X.X.X and needs to be HostName=DNSName

It would be nice to be able to run this script silently on the PC if running with a switch (can do with software deployment) and if a person wanted to run it manually they would get a pop-up on how many it changed. The bad thing is these files could be in quite a few different places on the PC. If I could put in the script the most common places to look for the files that would be good to. I also just found out that if the session is open and you change the string, when you close the session it changes the string back to what it was. I guess maybe error with "sessions need to be closed" if there is a certain exe running.

Thank you in advanced for any help you can provide.

 

Link to comment
Share on other sites

  • Moderators

Moved to the appropriate forum.

Moderation Team

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Welcome to the AutoIt forum.  Normally you should come up with a basic script so we can be able to help in solving issues you are facing.

BUT since it is your first time here and it is such an easy script, it would take me longer to explain what you should be doing than giving a frame that you could work on.

Next time, please, make an effort to create a runable script that we actually can execute.  If you still need help, provide example files that you want to modify.

#include <File.au3>

Local $aRoot[] = ["c:\Apps\Temp\","c:\users\" & @UserName & "AppData\local\"] ; add as much root folders as you want here
Local $aFile, $sFile, $sContent, $sDrive, $sDir, $sFileName, $sExtension
For $sFolder in $aRoot
  ConsoleWrite ($sFolder & @CRLF)
  $aFile = _FileListToArrayRec($sFolder, "*.HOD;*.WS", $FLTAR_FILES, $FLTAR_RECUR, Default, $FLTAR_FULLPATH)
  If @error Then ContinueLoop
  For $i = 1 To $aFile[0]
    $sFile = $aFile[$i]
    $sContent = FileRead($sFile)
    _PathSplit($sFile, $sDrive, $sDir, $sFileName, $sExtension)
    If $sExtension = ".HOD" Then
      $sContent = StringRegExpReplace($sContent, "(?is)(Host=)(\d+\.\d+\.\d+\.\d+)","$1DNSNAME")
    Else
      $sContent = StringRegExpReplace($sContent, "(?is)(HostName=)(\d+\.\d+\.\d+\.\d+)","$1DNSNAME")
    EndIf
    FileWrite($sDrive & $sDir & "New_" & $sFileName & $sExtension, $sContent)
  Next
Next

 

Edited by Nine
Link to comment
Share on other sites

Melba23,

Thank you for moving it to the appropriate forum.

Nine,

Thank you so much for the example script. I have been searching for a script for a few weeks now. I am usually really good at coming up with an idea but don't have the syntax knowledge to put it together. I will work with this and let you know how I come out.
 

Again Thank you!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...