Jump to content

Help with replacing


LerN
 Share

Recommended Posts

Hi

I need your idea to make something for me ..

I have a .txt file contains some names like this

ahasdhashd

gadfas

sdgjfgjfd

qweqwehu

for ex: this is the names that i have ^ what i want is to [] between every name

for ex :

[ahasdhashd]

I have over 2000 name so i want your idea how can i make it ... It would be great if u can help me :)

Link to comment
Share on other sites

  • Developers

Just open the file, read each line and add the 2 characters before and after the read record before writing it to a new file.
Have a look at the Helpfile for information of File functions.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Welcome to Autoit Forum!

Try:

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

; Begin write test data
FileWriteLine(@ScriptDir & "\MyLove.txt", "Anri Sugihara")
FileWriteLine(@ScriptDir & "\MyLove.txt", "Arimura Kasumi")
FileWriteLine(@ScriptDir & "\MyLove.txt", "Ai Shinozaki")
FileWriteLine(@ScriptDir & "\MyLove.txt", "Mizuho Uehara")
FileWriteLine(@ScriptDir & "\MyLove.txt", "Agarie Hikari")
FileWriteLine(@ScriptDir & "\MyLove.txt", "Emiri Suzuhara")
; End write test data
Local $sFileIN = @ScriptDir & "\MyLove.txt"
Local $sFileOut = @ScriptDir & "\MyLove.txt"
_AddNameX($sFileIN, $sFileOut)
MsgBox($MB_ICONQUESTION, "Add []", "From>" & $sFileIN & @CRLF & FileRead($sFileIN) & @CRLF & "TO> " & $sFileOut & @CRLF & FileRead($sFileOut))

Func _AddNameX($sTextFileIN, $sTextFileOut = "")
    If Not FileExists($sTextFileIN) Then Return SetError(1, 0, 0)
    Local $sLine = FileReadToArray($sTextFileIN) ; StringSplit(FileRead($sTextFilePath)," ")
    If @error Then Return SetError(2, 0, 0)
    Local $sNewContent
    For $n = 0 To UBound($sLine) - 1
        If (StringStripWS($sLine[$n], 8) <> "") And (Not StringInStr($sLine[$n], "[") Or Not StringInStr($sLine[$n], "]")) Then $sNewContent &= "[" & $sLine[$n] & "]" & @CRLF
    Next
    ConsoleWrite($sNewContent & @CRLF)
    If $sTextFileOut = "" Then $sTextFileOut = $sTextFileIN
    Local $hFileOpen = FileOpen($sTextFileOut, $FO_OVERWRITE + $FO_UTF8)
    FileWrite($hFileOpen, $sNewContent)
    Return SetError(@error, FileClose($hFileOpen), 1)
EndFunc   ;==>_AddNameX

 

Regards,
 

Link to comment
Share on other sites

I love you trong !

But in this case i will need another help ...
Can u make a function after writing [] to everyname can u copy the name like this :
this is the name : asdhjasd

and after using the script it's gonna be [asdhjasd]

but i want it like this :

[asdhjasd]

asdhjasd

if u can help would be great <3

Link to comment
Share on other sites

Just now, LerN said:

I love you trong !

But in this case i will need another help ...
Can u make a function after writing [] to everyname can u copy the name like this :
this is the name : asdhjasd

and after using the script it's gonna be [asdhjasd]

but i want it like this :

[asdhjasd]

asdhjasd

if u can help would be great <3

What i mean is :
if i have a .txt file with these names :
asdfhg
basdf
dsfsgf
Your script works in adding [] in everyname successfully 
so after using your funcs it will be like this :

[asdfhg]
[basdf]
[dsfsgf]

but i want it like this :
[asdfhg]

name = asdfhg
[basdf]

name = asdfhg
[dsfsgf]

name = dsfsgf

Link to comment
Share on other sites

Good Lucky

Local $sFileIN = @ScriptDir & "\MyLove.txt"
; Begin write test data
Local $hFileOpen = FileOpen($sFileIN, $FO_OVERWRITE + $FO_UTF8)
FileWrite($hFileOpen, "Anri Sugihara" & @CRLF & "Arimura Kasumi" & @CRLF & "Ai Shinozaki" & @CRLF & "Mizuho Uehara" & @CRLF & "Agarie Hikari" & @CRLF & "Emiri Suzuhara" & @CRLF)
FileClose($hFileOpen)
; End write test data

Local $sFileOut = @ScriptDir & "\MyLoveNEW.txt"

_AddNameX($sFileIN, $sFileOut)
MsgBox($MB_ICONQUESTION, "Add []", "From>" & $sFileIN & @CRLF & FileRead($sFileIN) & @CRLF & "TO> " & $sFileOut & @CRLF & FileRead($sFileOut))
_AddNameToINI($sFileIN, $sFileOut)
MsgBox($MB_ICONQUESTION, "Add []", "From>" & $sFileIN & @CRLF & FileRead($sFileIN) & @CRLF & "TO> " & $sFileOut & @CRLF & FileRead($sFileOut))

Func _AddNameX($sTextFileIN, $sTextFileOut = "")
    If Not FileExists($sTextFileIN) Then Return SetError(1, 0, 0)
    Local $sLine = FileReadToArray($sTextFileIN) ; StringSplit(FileRead($sTextFilePath)," ")
    If @error Then Return SetError(2, 0, 0)
    Local $sNewContent
    For $n = 0 To UBound($sLine) - 1
        If (StringStripWS($sLine[$n], $STR_STRIPALL) <> "") And (Not StringInStr($sLine[$n], "[") Or Not StringInStr($sLine[$n], "]")) Then $sNewContent &= "[" & $sLine[$n] & "]" & @CRLF & "Name=" & $sLine[$n] & @CRLF & @CRLF
    Next
    ConsoleWrite($sNewContent & @CRLF)
    If $sTextFileOut = "" Then $sTextFileOut = $sTextFileIN
    Local $hFileOpen = FileOpen($sTextFileOut, $FO_OVERWRITE + $FO_UTF8)
    FileWrite($hFileOpen, $sNewContent)
    Return SetError(@error, FileClose($hFileOpen), 1)
EndFunc   ;==>_AddNameX

Func _AddNameToINI($sTextFileIN, $sTextFileOut = "")
    If Not FileExists($sTextFileIN) Then Return SetError(1, 0, 0)
    Local $sLine = FileReadToArray($sTextFileIN) ; StringSplit(FileRead($sTextFilePath)," ")
    If @error Then Return SetError(2, 0, 0)
    Local $sNewContent = "[Name]" & @CRLF
    For $n = 0 To UBound($sLine) - 1
        If (StringStripWS($sLine[$n], $STR_STRIPALL) <> "") Then $sNewContent &= ($n + 1) & "=" & $sLine[$n] & @CRLF
    Next
    ConsoleWrite($sNewContent & @CRLF)
    If $sTextFileOut = "" Then $sTextFileOut = $sTextFileIN
    Local $hFileOpen = FileOpen($sTextFileOut, $FO_OVERWRITE + $FO_UTF8)
    FileWrite($hFileOpen, $sNewContent)
    Return SetError(@error, FileClose($hFileOpen), 1)
EndFunc   ;==>_AddNameToINI

 

Regards,
 

Link to comment
Share on other sites

Wouldn't it be simpler just to use the Ini functions?  For example:

;~ Begin write test data
Local $sTxt_FileName = @ScriptDir & "\FileName.txt"
Local $hFileOpen = FileOpen($sFileIN, 10)
FileWrite($hFileOpen, "Anri Sugihara" & @CRLF & "Arimura Kasumi" & @CRLF & "Ai Shinozaki" & @CRLF & "Mizuho Uehara" & @CRLF & "Agarie Hikari" & @CRLF & "Emiri Suzuhara" & @CRLF)
FileClose($hFileOpen)
;~ End write test data

Local $sIni_FileName = @ScriptDir & "\FileName.ini"
Local $aFileName = FileReadToArray($sTxt_FileName)
    If @error Then Exit
For $i = 0 To UBound($aFileRead) - 1
    IniWrite($sIni_FileName, $aFileName[$i], "name", $aFileName[$i])
Next

 

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