euverve Posted March 19, 2009 Posted March 19, 2009 (edited) How could i Replace a string '\' into '\\' in a text file Like this one. Source file (sample.txt) [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] "winchat"="%Windir%\winchat.exe" Destination file (sample2.txt) [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] "winchat"="%Windir%\\winchat.exe" The '\' in line [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] must not be replaced. Only the '\' in line "winchat"="%Windir%\winchat.exe" must be replaced to '\\'. I hope you got my point. Help appreciated. Edited March 19, 2009 by euverve
euverve Posted March 19, 2009 Author Posted March 19, 2009 StringReplace($FileContents, "%\", "%\\") Your script replaces only the first occurence of the string '\'. What if the source file contains more '\' string? Like this one: [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] "winchat"="%Windir%\folder1\folder2\winchat.exe" Should be: [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] "winchat"="%Windir%\\folder1\\folder2\\winchat.exe" Any help please.
Authenticity Posted March 19, 2009 Posted March 19, 2009 #include <Array.au3> Dim $sStr = '[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]' & @CRLF & _ '"winchat"="%Windir%\folder1\folder2\winchat.exe"' & @CRLF & _ '"winchat"="%Windir%\folder1\folder2\winchat.exe"' & @CRLF & _ '[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]' & @CRLF & _ '"winchat"="%Windir%\folder1\folder2\winchat.exe"' & @CRLF & _ '"winchat"="%Windir%\folder1\folder2\winchat.exe"' & @CRLF & _ '"winchat"="%Windir%\folder1\folder2\winchat.exe"' & @CRLF & _ '[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]' & @CRLF & _ '"winchat"="%Windir%\folder1\folder2\winchat.exe"' $aLines = StringSplit($sStr, @CRLF, 3) For $i = 0 To UBound($aLines)-1 If StringInStr($aLines[$i], '[') Then ContinueLoop $aLines[$i] = StringReplace($aLines[$i], '\', '\\') Next $sStr = _ArrayToString($aLines, @CRLF) ConsoleWrite($sStr & @LF) Please use the help file, waiting may take longer.
Moderators SmOke_N Posted March 19, 2009 Moderators Posted March 19, 2009 (edited) StringRegExpReplace($FileContents, "(%)?(\\\\|\\)", "\1\\\\")Edit:Fixed ExpressionEdit2:Never mind, I see you don't want the ones with the keys in it to be replaced. Edited March 19, 2009 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
euverve Posted March 19, 2009 Author Posted March 19, 2009 (edited) @ Sm0ke_N I have tested your script but it replaces all '\' string. Source.txt [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] "winchat"="%Windir%\folder1\folder2\winchat.exe" Result: [HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run] "winchat"="%Windir%\\folder1\\folder2\\winchat.exe" Codes used: $TextFileName = "Source.txt" $FileContents = FileRead($TextFileName) $FileContents = StringRegExpReplace($FileContents, "(%)?(\\\\|\\)", "\1\\\\") FileDelete($TextFileName) FileWrite($TextFileName, $FileContents) Expected Result: [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] "winchat"="%Windir%\\folder1\\folder2\\winchat.exe" @Authenticity What if i have a long list of lines to be replaced? Do I have to put it all in an array? How could I make the script read to a text file. Edited March 19, 2009 by euverve
KaFu Posted March 19, 2009 Posted March 19, 2009 $file = FileOpen("Source.txt", 0) If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf $FileContents = "" While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop if stringinstr($line,'"="') Then $line = StringRegExpReplace($line, "(%)?(\\\\|\\)", "\1\\\\") EndIf $FileContents &= $line & @crlf Wend FileClose($file) ConsoleWrite($FileContents) OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2025-May-18) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16)
euverve Posted March 19, 2009 Author Posted March 19, 2009 $file = FileOpen("Source.txt", 0) If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf $FileContents = "" While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop if stringinstr($line,'"="') Then $line = StringRegExpReplace($line, "(%)?(\\\\|\\)", "\1\\\\") EndIf $FileContents &= $line & @crlf Wend FileClose($file) ConsoleWrite($FileContents) Thanks kafu, I tested and it works well as I expected.
Authenticity Posted March 19, 2009 Posted March 19, 2009 (edited) Tested this one on my entire registry and it took about 20 seconds: $sStr = FileRead('C:\Backup.log') $sStr = StringRegExpReplace($sStr, '(?m)(^\[[^\]]*+\]|(?<!\\)(\\)(?!\\|\r|$))', '\1\2') FileWrite('C:\TempBackup.log', $sStr) So you won't get things like: [HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run] or \\\\%WorkingDir%\\\\\Temp or 13,0F,EE,00,00,00,00,\\ etc.. if you care not having these. Edited March 20, 2009 by Authenticity
euverve Posted March 20, 2009 Author Posted March 20, 2009 Thanks for another method, it works well. Thanks for the useful scripts.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now