Jump to content

Help with Find and replace a string in all files in a folder


Recommended Posts

Good Afternoon,

                 I have been searching the forum for hours and still cannot figure out why this script does not work.  No errors but does not replace anything.   I have checked my regex with the regex coach.  That part at least in The Regex Coach works.   Any help please

#include <file.au3>
#include <Array.au3>
#include <String.au3>
#include <StringConstants.au3>
#include <Date.au3>
#include <FileConstants.au3>

Global $sPath = "C:\Test"
Global $mPath = "C:\PRPATemp"
Global $sFolder = _FileListToArray($sPath, Default, $FLTA_FILES)


;ShellExecute("C:\Users\user82.PPMCINC\Desktop\WorkingScripts\PRPAMammoChargeFileMove.vbs")

$sfolder = "C:\PRPATemp"
$aFileList = _FileListToArray($sfolder, "*", $FLTA_FILES)

;_ArrayDisplay($aFileList)

Global $aArray

For $i = 1 To $aFileList[0]
        _FileReadToArray($aFileList[$i], $aArray, 0)
        StringRegExpReplace($aArray, "QEGTPACS(.*)2\.1", "QEGTPACS.1.1111111|P|2.1")
    _FileWriteFromArray($aFileList[$i], $aArray)
    Next

 

Link to comment
Share on other sites

  • Moderators

xcaliber13,

An array cannot be accessed as a single entity by StringRegExpReplace as you try to do in that script. You will either have to convert the array contents to a string and then recreate the array after the manipulation - or loop through each element individually to action the RegEx.

M23

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

Melba23,

             Thank you for the reply.   I started out  this way:

#include <file.au3>
#include <Array.au3>
#include <String.au3>
#include <StringConstants.au3>
#include <Date.au3>
#include <FileConstants.au3>

Global $sPath = "C:\Test"
Global $mPath = "C:\PRPATemp"
Global $sFolder = _FileListToArray($sPath, Default, $FLTA_FILES)


;ShellExecute("C:\Users\user82.PPMCINC\Desktop\WorkingScripts\PRPAMammoChargeFileMove.vbs")

$sfolder = "C:\PRPATemp"
$aFileList = _FileListToArray($sfolder, "*", $FLTA_FILES)

;_ArrayDisplay($aFileList)

Global $sFile
Global $str
For $i = 1 To $aFileList[0]
        $sFile = FileOpen($aFileList[$i], 2)
        $str = StringRegExpReplace($sFile, "QEGTPACS(.*)2\.1", "QEGTPACS.1.1111111|P|2.1")
        FileWrite($sFile, $str)
    Next

But still  script completes with no errors and still nothing is replaced.   I am stumped or stupid.  Where have I gone wrong?

Link to comment
Share on other sites

  • Moderators

xcaliber13,

FileOpen merely opens the file, it does not read it - try using FileRead.

M23

Edit: And  you will need to append the path to the filenames in the array - or set the $bReturnPath parameter in _FileListToArray to get the full path into the array. You do have a copy of the Help file handy?

Edited by Melba23

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

Melba23,

               Thank you your help.  With your directions I was finally able to finish the script.   Here is what works for me.

#include <file.au3>
#include <Array.au3>
#include <String.au3>
#include <StringConstants.au3>
#include <Date.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

;;This section searchs for QEGTPACS in each file and if found replaces

Global $sPath = "C:\PRPATemp\"
Global $sFolder = _FileListToArray($sPath, Default, 0, True)

;_ArrayDisplay($sFolder)

For $i = 1 to $sFolder[0]
  Local $sPathAndFileName = $sFolder[$i]
    Local $sFileOpen = FileOpen($sPathAndFileName)
    Local $sFileRead = FileRead($sFileOpen)
    Global $sFile
    Global $NewFile = $sPathAndFileName &"New"
    If StringInStr($sFileRead, "QEGTPACS") Then
        $sFile = StringRegExpReplace($sFileRead, "QEGTPACS(.*)2\.1", "QEGTPACS\.1\.1111111\|P\|2.1", 1)
        _FileCreate($NewFile)
        FileWrite($NewFile, $sFile)
        FileMove($NewFile, "C:\PRPATEMP2")
    Else
        ConsoleWrite("Not found in: " & $sPathAndFileName[$i] & @CRLF)
    EndIf
Next

Again Thank you for your help

Link to comment
Share on other sites

  • Moderators

xcaliber13,

Glad I could help.

M23

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

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...