LerN Posted April 16, 2017 Share Posted April 16, 2017 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 More sharing options...
Developers Jos Posted April 16, 2017 Developers Share Posted April 16, 2017 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 More sharing options...
Trong Posted April 16, 2017 Share Posted April 16, 2017 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 More sharing options...
LerN Posted April 16, 2017 Author Share Posted April 16, 2017 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 More sharing options...
Trong Posted April 16, 2017 Share Posted April 16, 2017 My English is very bad, you can tell me more? Post the original sample data and sample results! Regards, Link to comment Share on other sites More sharing options...
InunoTaishou Posted April 16, 2017 Share Posted April 16, 2017 Instead of trong doing it for you could you show us what you've tried? Link to comment Share on other sites More sharing options...
LerN Posted April 16, 2017 Author Share Posted April 16, 2017 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 More sharing options...
Trong Posted April 16, 2017 Share Posted April 16, 2017 Good Lucky expandcollapse popupLocal $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 More sharing options...
LerN Posted April 16, 2017 Author Share Posted April 16, 2017 Thank you trong ! Link to comment Share on other sites More sharing options...
Subz Posted April 17, 2017 Share Posted April 17, 2017 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 More sharing options...
LerN Posted April 17, 2017 Author Share Posted April 17, 2017 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 Link to comment Share on other sites More sharing options...
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