LerN

Help with replacing

11 posts in this topic

#1 ·  Posted

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

Share this post


Link to post
Share on other sites



#2 ·  Posted

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


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#3 ·  Posted

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,
 

Share this post


Link to post
Share on other sites

#4 ·  Posted

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

Share this post


Link to post
Share on other sites

#5 ·  Posted

My English is very bad, you can tell me more?
Post the original sample data and sample results!


Regards,
 

Share this post


Link to post
Share on other sites

#6 ·  Posted

Instead of trong doing it for you could you show us what you've tried?

Share this post


Link to post
Share on other sites

#7 ·  Posted

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

Share this post


Link to post
Share on other sites

#8 ·  Posted

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,
 

Share this post


Link to post
Share on other sites

#9 ·  Posted

Thank you trong !

Share this post


Link to post
Share on other sites

#10 ·  Posted

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

 

Share this post


Link to post
Share on other sites

#11 ·  Posted

Subz I already edited the first one to the way I want it to work , he didn't understand actually what I mean but I made it already so np :)

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