jswilcox1980

Replace String in multiple files.

33 posts in this topic

Hello everyone i will try to explain what i am trying to achieve here, i have been trying to figure this out on my own using help files/examples and other references with no luck.  I have a file that is output everytime my customer completes a scan which is an .INI configuration settings file that looks like this....

[INFO]
PCID=12345
Version=Cercon design 2.3.5.1
SerialNo=101110
Case started=1899-12-30 14:17:28
Last saved=2010-02-23 15:36:50
Data source=External scanner

What i need to happen is the PCID=12345  needs to be changed to a specified 6 digit number, i could make this hard coded to just do the operation but a more generic option with Input boxes would be nice.   Also when these files are created they are created with a uniquely named folder with a matching *INI file name  i would like to either select the files through a dialog box or just process the *ini files in the entire folder.

Folder
    204567_100100
            204567_100100.scan
            204567_100100.ini
     204567_100101
            204567_100101.scan
            204567_100101.ini
     204567_100102
            204567_100102.scan
            204567_100102.ini

 

Here is the code i have been playing with to try and get it to work, i have been successful by specifying one single file by using the example but this would be more work than just manually editing the file.  But if i could automate it this would save a ton of time.

 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; *** Start added by AutoIt3Wrapper ***
#include <Constants.au3>
; *** End added by AutoIt3Wrapper ***
#include <File.au3>


Local $sFind = InputBox("Search for the following", "Enter the 5 Digit 3shape Site ID to be replaced", "12345")
Local $sReplace = InputBox("Replace with this entry", "Enter the 6 Digit PCID to be replaced", "445566")
Local $sFileName = "C:\ReplaceStringInFile.test"

Local $iMsg = "This will Replace the 5 digit 3shape Site ID " & $sFind & " With the 6 Digit Cercon PCID " & $sReplace & "at the following file location " & $sFileName

MsgBox($MB_SYSTEMMODAL, ":*:*:*  ATTENTION  *:*:*:", $iMsg)

Local $iRetval =_ReplaceStringInFile($sFileName, $sFind, $sReplace)
If $iRetval = -1 Then
    MsgBox($MB_SYSTEMMODAL, "ERROR", "The pattern could not be replaced in file: " & $sFileName & " Error: " & @error)
    Exit
Else
    MsgBox($MB_SYSTEMMODAL, "INFO", "Found " & $iRetval & " occurances of the pattern: " & $sFind & " in the file: " & $sFileName)
EndIf

$iMsg = FileRead($sFileName, 1000)

 

 

Any help or direction would be greatly appreciated!

 

Thanks 

 

Jeremy

Share this post


Link to post
Share on other sites



Welcome to Autoit and the forum!

I would use _FileListToArrayRec. It searches the specified directory plus all subdirectories for the needed files.
Then loop through this array and read each file into a variable (function FileRead) and replace the string using function StringReplace. When the string was found and replaced ( number of replacements performed is stored in the @extended macro) then use FileWrite to save the file.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2016-08-18 - Version 1.4.6.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2016-12-04 - Version 1.2.2.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Something like this using _FileListToArrayRec. You would want to modify your error checking output from MsgBox to maybe a log file:

#include <Array.au3>
#include <File.au3>


Local $sFind = InputBox("Search for the following", "Enter the 5 Digit 3shape Site ID to be replaced", "42")
Local $sReplace = InputBox("Replace with this entry", "Enter the 6 Digit PCID to be replaced", "420")
Local $sDirectory = @DesktopDir & "\Test"
Local $aFiles = _FileListToArrayRec($sDirectory, "*.ini", $FLTAR_FILES, $FLTAR_RECUR, Default, $FLTAR_FULLPATH)
Local $iRetval = 0

For $i = 1 to $aFiles[0]
    If _ReplaceStringInFile($aFiles[$i], $sFind, $sReplace) Then $iRetval += 1
Next

ConsoleWrite($iRetval & @CRLF)

Edit: Too slow :)

Edited by JLogan3o13

When you're dead, you don't know you're dead - it's only difficult for those that know you. It's the same way when you're stupid...

My Scripts: SCCM UDFInclude Source with Compiled Script, Windows Firewall UDF

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I was going to suggest the same, except try using the dedicated IniWrite() function it should simplify the process.

I created this folder structure

Scans
  123
    123.inf
  345
    345.inf
  456
    456.inf

In the .INF File was exactly what you posted in the first post.

This example worked well for me.

#Include <File.au3>

$aFiles = _FileListToArrayRec(@ScriptDir & "\Scans", "*.ini", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)

For $i =1 to $aFiles[0]
    Local $sInicurrent = IniRead($aFiles[$i], "INFO", "PCID", "Not Found!")
    Local $sChange = InputBox($aFiles[$i], "Enter New PCID", $sInicurrent, "", @DesktopWidth/2)
If NOT @error Then
    IniWrite($aFiles[$i], "INFO", "PCID", $sChange)
EndIf
Next

I used the title of the Inputbox to see what file I was currently editing, if you do not want to change an item hit cancel or close the input box and it will skip it.  For more visibility I would probably have a SplashTextOn() showing the file name or file structure or something but I just wanted to show the most simple example I could come up with.

Better Yet I would use IniRead() before the inputbox so I could populate the default replace with the current string.

The only thing I was unsure of is if you wanted an input box for each ini file as I did here, or you just wanted one inputbox to pick a static number to automatically process on all files.

It would be easy to modify to do the later, but I figured you wanted more control over each file.

Edit: Put that feature in the example.

 

Edit: Update all files with a static change

#Include <File.au3>

Local $sDirectory = @ScriptDir & "\Scans"
Local $aFiles = _FileListToArrayRec($sDirectory, "*.ini", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
Local $sChange = InputBox("Update All .INI Files In " & $sDirectory, "Enter New PCID", "", "", @DesktopWidth/2)

For $i =1 to $aFiles[0]
    IniWrite($aFiles[$i], "INFO", "PCID", $sChange)
Next

MsgBox(0, "", "All INI File Updates Are Complete!")

 

Edited by ViciousXUSMC

Share this post


Link to post
Share on other sites

Thanks you all for your quick replies....

Jlogan i was able to get your script to work great Thankyou!  

Viscious  Your Ini Read/write idea was a great idea but i was unsuccesful in getting it to work.  

Now i would like to see if i can combine a separate task into this  I actually need to remove the last 4 characters from the file and folder names:  Right now the this is how the folders are setup:

 

Current folder/file setup
Desktop\530100_001837_000\
         \530100_001837_000
                530100_001837_000.3sz
                530100_001837_000_000.ini

Now that i can edit the internal PCID in the .ini file i also need to remove the "_000"  from the folders and files.  before today i was having my customer use Lupas rename and Right crop 4 characters separately but if i could make this into one functional executable it would be awesome.... i just dont know how to incorporate the StringTrimRight command into folder and file use within the current script.

What i want the end results to be
Desktop\530100_001837\
         \530100_001837\
                530100_001837.3sz
                530100_001837_000.ini

Thanks again for any directions or help.

Share this post


Link to post
Share on other sites

Can you please confirm I am understanding you correctly (because if I am, it looks like it is going to be ugly)? The top example is how it looks today, and you want to rename both the files and the folders the what you have in the second example? If this is correct, a couple of other questions:

  • How many other files in the directory? You basically have to do a Move on the files and their folders in order to rename them; I would suggest listing all files in the directory, renaming them, then moving the directory itself. If each directory has a large number of files, however, this is going to be a PITA.
  • Are you always going to be removing that format of the name (_xxx)? Or is there possibility it will change?

When you're dead, you don't know you're dead - it's only difficult for those that know you. It's the same way when you're stupid...

My Scripts: SCCM UDFInclude Source with Compiled Script, Windows Firewall UDF

Share this post


Link to post
Share on other sites

in the parent directory there is a possiblity of being HUNDREDS if not thousands of files/folders named incorrectly,  this is a software bug that doesnt look like it may ever get corrected.  so i may have my customer copy the files he needs to rename into a separate "working" directory folder to be the "processing" folder.  Yes for this script it will be ALWAYS the same format ...removing the last 4 characters from the file name.  Each directory folder that will be uniquely named incrmentally like my first post will only have the 2 files in it just like my most recent post shows.

Share this post


Link to post
Share on other sites

Could be a job for a regular expression, if all the questions in @JLogan3o13 post are answered.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

My solution works for me, not sure why not for you :(

As for Rename - I came up with something like this.  I have not tuned it out yet.  What needs to be done is renaming all the files first (So do a file list and run the loop and process, and then run a folders only list and process) and the rename part I need to come up with a RegEx or other string() function that accurately trims the extra characters.

I ran this on a test structure like yours and it ran super fast (no moving of files) but it renamed some of the folders and thus missed the files inside them, and I did not figure out the trim part yet I just had them named a number based on the loop iteration. 

#Include <File.au3>
#Include <Array.au3>

Local $sDirectory = @ScriptDir & "\Test"
Local $aFiles = _FileListToArrayRec($sDirectory, "*", $FLTAR_FILESFOLDERS, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)

;_ArrayDisplay($aFiles)

For $i = 1 to $aFiles[0]
    If StringRegExp($aFiles[$i],".*?_000(.3sz|.ini)?$") Then
        Run(@ComSpec & " /k REN " & '"' & $aFiles[$i] & '" "' & $i & '"')
    EndIf
Next

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

By god I think I did it, I am not good at RegEx but I tried this and got perfect results even with doubling up the _000_000 and it will not capture anything for a change if it does not end in _000

#Include <File.au3>

Local $sDirectory = @ScriptDir & "\Test"
Local $aFiles = _FileListToArrayRec($sDirectory, "*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
Local $aFilesName = _FileListToArrayRec($sDirectory, "*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_NOPATH)
Local $aFolders = _FileListToArrayRec($sDirectory, "*", $FLTAR_FOLDERS, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
Local $aFoldersName = _FileListToArrayRec($sDirectory, "*", $FLTAR_FOLDERS, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_NOPATH)

For $i = 1 to $aFiles[0]
    If StringRegExp($aFiles[$i],".*?_000(.3sz|.ini)?$") Then
        Run(@ComSpec & " /k REN " & '"' & $aFiles[$i] & '" "' & StringRegExpReplace($aFilesName[$i],   "_000(.3sz|.ini)?$", "$1") & '"')
    EndIf
Next

For $i = 1 to $aFolders[0]
    If StringRegExp($aFolders[$i],".*?_000(.3sz|.ini)?$") Then
        Run(@ComSpec & " /k REN " & '"' & $aFolders[$i] & '" "' & StringRegExpReplace($aFoldersName[$i],   "_000(.3sz|.ini)?$", "$1") & '"')
    EndIf
Next

Edit: I should point out that just like my previous example its going off exactly what you posted as an example, if your file names are different or you have other file extensions they need to be added in.

$sString = "Double_000S-530100_001837_000_000.ini"
$sTest = StringRegExpReplace($sString, "_000(.3sz|.ini)?$", "$1")
MsgBox(0, "", $sTest)

$sString2 = "WontIncludeBATFiles530100_001837_000_000.bat"
$sTest2 = StringRegExpReplace($sString2, "_000(.3sz|.ini)?$", "$1")
MsgBox(0, "", $sTest2)

$sString3 = "DoNotChangeMe"
$sTest3 = StringRegExpReplace($sString3, "_000(.3sz|.ini)?$", "$1")
MsgBox(0, "", $sTest3)

$sString4 = "FolderWithNoExtension_000"
$sTest4 = StringRegExpReplace($sString4, "_000(.3sz|.ini)?$", "$1")
MsgBox(0, "", $sTest4)

 

Edited by ViciousXUSMC

Share this post


Link to post
Share on other sites

Why not temporarily replace and check @extended instead? Now you're calling the regex engine twice, when one is suffice.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

By god I think I did it, I am not good at RegEx but I tried this and got perfect results even with doubling up the _000_000 and it will not capture anything for a change if it does not end in _000

#Include <File.au3>

Local $sDirectory = @ScriptDir & "\Test"
Local $aFiles = _FileListToArrayRec($sDirectory, "*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
Local $aFilesName = _FileListToArrayRec($sDirectory, "*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_NOPATH)
Local $aFolders = _FileListToArrayRec($sDirectory, "*", $FLTAR_FOLDERS, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
Local $aFoldersName = _FileListToArrayRec($sDirectory, "*", $FLTAR_FOLDERS, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_NOPATH)

For $i = 1 to $aFiles[0]
    If StringRegExp($aFiles[$i],".*?_000(.3sz|.ini)?$") Then
        Run(@ComSpec & " /k REN " & '"' & $aFiles[$i] & '" "' & StringRegExpReplace($aFilesName[$i],   "_000(.3sz|.ini)?$", "$1") & '"')
    EndIf
Next

For $i = 1 to $aFolders[0]
    If StringRegExp($aFolders[$i],".*?_000(.3sz|.ini)?$") Then
        Run(@ComSpec & " /k REN " & '"' & $aFolders[$i] & '" "' & StringRegExpReplace($aFoldersName[$i],   "_000(.3sz|.ini)?$", "$1") & '"')
    EndIf
Next

Edit: I should point out that just like my previous example its going off exactly what you posted as an example, if your file names are different or you have other file extensions they need to be added in.

I will stand corrected i must have used your first bit of code earlier your INIWRITE works GREAT!  i will try and test this bit of code now....

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Why not temporarily replace and check @extended instead? Now you're calling the regex engine twice, when one is suffice.

Because I am not that awesome :) still learning feel free to fix up my code and show me what you mean.

Edited by ViciousXUSMC

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

So you want me to code it for you? Well I will give you a clue...

Local $sTemp = Null
For $i = 1 to $aFiles[0]
    $sTemp = StringRegExpReplace($aFilesName[$i],   "_000(.3sz|.ini)?$", "$1")
    If Not @extended Then ContinueLoop ; Skip replacement if nothing was replaced i.e. @extended == 0

    Run(@ComSpec & " /k REN " & '"' & $aFiles[$i] & '" "' & $sTemp & '"')
Next

 

Edited by guinness

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

By god I think I did it, I am not good at RegEx but I tried this and got perfect results even with doubling up the _000_000 and it will not capture anything for a change if it does not end in _000

#Include <File.au3>

Local $sDirectory = @ScriptDir & "\Test"
Local $aFiles = _FileListToArrayRec($sDirectory, "*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
Local $aFilesName = _FileListToArrayRec($sDirectory, "*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_NOPATH)
Local $aFolders = _FileListToArrayRec($sDirectory, "*", $FLTAR_FOLDERS, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
Local $aFoldersName = _FileListToArrayRec($sDirectory, "*", $FLTAR_FOLDERS, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_NOPATH)

For $i = 1 to $aFiles[0]
    If StringRegExp($aFiles[$i],".*?_000(.3sz|.ini)?$") Then
        Run(@ComSpec & " /k REN " & '"' & $aFiles[$i] & '" "' & StringRegExpReplace($aFilesName[$i],   "_000(.3sz|.ini)?$", "$1") & '"')
    EndIf
Next

For $i = 1 to $aFolders[0]
    If StringRegExp($aFolders[$i],".*?_000(.3sz|.ini)?$") Then
        Run(@ComSpec & " /k REN " & '"' & $aFolders[$i] & '" "' & StringRegExpReplace($aFoldersName[$i],   "_000(.3sz|.ini)?$", "$1") & '"')
    EndIf
Next

Edit: I should point out that just like my previous example its going off exactly what you posted as an example, if your file names are different or you have other file extensions they need to be added in.

 

$sString = "Double_000S-530100_001837_000_000.ini"
$sTest = StringRegExpReplace($sString, "_000(.3sz|.ini)?$", "$1")
MsgBox(0, "", $sTest)

$sString2 = "WontIncludeBATFiles530100_001837_000_000.bat"
$sTest2 = StringRegExpReplace($sString2, "_000(.3sz|.ini)?$", "$1")
MsgBox(0, "", $sTest2)

$sString3 = "DoNotChangeMe"
$sTest3 = StringRegExpReplace($sString3, "_000(.3sz|.ini)?$", "$1")
MsgBox(0, "", $sTest3)

 

This worked renaming the files but did NOT rename the folders.... also it opened about a dozen command prompt windows...any ideas why?

Share this post


Link to post
Share on other sites

It's calling cmd.exe so you're going to get windows. I believe @ViciousXUSMC left it at /k right now so you could see what is going on. Eventually you can change that to /c so the window doesn't stay open.

 


When you're dead, you don't know you're dead - it's only difficult for those that know you. It's the same way when you're stupid...

My Scripts: SCCM UDFInclude Source with Compiled Script, Windows Firewall UDF

Share this post


Link to post
Share on other sites

Yeah just came back in to say put /C in the final code lol I use /k for testing.

Share this post


Link to post
Share on other sites

Yeah just came back in to say put /C in the final code lol I use /k for testing.

/c closes most but not all and its still hanging up not  renaming the folders

 

Share this post


Link to post
Share on other sites

So you want me to code it for you? Well I will give you a clue...

Local $sTemp = Null
For $i = 1 to $aFiles[0]
    $sTemp = StringRegExpReplace($aFilesName[$i],   "_000(.3sz|.ini)?$", "$1")
    If Not @extended Then ContinueLoop ; Skip replacement if nothing was replaced i.e. @extended == 0

    Run(@ComSpec & " /k REN " & '"' & $aFiles[$i] & '" "' & $sTemp & '"')
Next

 

Got what you mean now, see if replaced and if so continue, I came up with RegExReplace after the fact when I was figuring out the trim part, so already had the first part in place, never backwards engineered it for performance.  But that's why your a Dev and I am a novice especially with RegEx I am super new at that.

Share this post


Link to post
Share on other sites

/c closes most but not all and its still hanging up not  renaming the folders

 

Hmm care to post your folders that are not being renamed and the full file structure?

Share this post


Link to post
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