Jump to content
Sign in to follow this  
euverve

Replace a string in a line from '\' to '\\'

Recommended Posts

euverve

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 by euverve

Share this post


Link to post
Share on other sites
euverve

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.

Share this post


Link to post
Share on other sites
Authenticity

#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.

Share this post


Link to post
Share on other sites
SmOke_N

StringRegExpReplace($FileContents, "(%)?(\\\\|\\)", "\1\\\\")

Edit:

Fixed Expression

Edit2:

Never mind, I see you don't want the ones with the keys in it to be replaced.

Edited 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.

Share this post


Link to post
Share on other sites
euverve

@ 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 by euverve

Share this post


Link to post
Share on other sites
KaFu

$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)

Share this post


Link to post
Share on other sites
euverve

$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.

Share this post


Link to post
Share on other sites
Authenticity

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 by Authenticity

Share this post


Link to post
Share on other sites
euverve

Thanks for another method, it works well. Thanks for the useful scripts.

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.