TheCrimsonCrusader Posted May 31, 2018 Share Posted May 31, 2018 Greetings, I have a list of computer names that are one line after another. After I read each line, I am writing them to a new file where it will be written as following for each computer name: "%computer_name%", So basically enclosing the computer name entry in quotes followed by a comma. However, instead of writing to a new file with one being written one line after another, I would like them to be all on the same line aka the very first line of the newly written text file. I tried the _FileWriteToLine function with writing to line 1, but was still writing one line after another trying that instead, so I was clearly doing something wrong. Code below and thanks for any assistance that can be provided. #NoTrayIcon #RequireAdmin #include <file.au3> $file = FileOpen("c:\UserMachines.txt", 0) While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop ;MsgBox(0,'',$line) FileWrite("c:\NewFile.txt",chr(34) & $line & chr(34) & "," & @CRLF) WEnd FileClose($file) Link to comment Share on other sites More sharing options...
Xandy Posted May 31, 2018 Share Posted May 31, 2018 (edited) Removing the & @CRLF works for me to place them all on one line. #NoTrayIcon #RequireAdmin ;#include <file.au3> $file = FileOpen(@ScriptDir & "\test.txt", 0) While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop ;MsgBox(0,'',$line) FileWrite(@ScriptDir & "\test2.txt", Chr(34) & $line & Chr(34) & ",") WEnd FileClose($file) Edit: Fixed code I see you wanted the quotes Edited May 31, 2018 by Xandy Human Male Programmer (-_-) Xandy About (^o^) Discord - Xandy Programmer MapIt (Tile world editor, Image Tile Extractor, and Game Maker) Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted May 31, 2018 Moderators Share Posted May 31, 2018 (edited) You could also just read the entire first file into an array, then convert to string and output: #include <file.au3> Local $aPCs = FileReadToArray(@DesktopDir & "\UserMachines.txt") Local $sString For $sServer In $aPCs $sString &= '"' & $sServer & '", ' Next FileWrite(@DesktopDir & "\newfile.txt", StringTrimRight($sString, 2)) My text file has LC-PC001 - 020, so output file would look like this: Quote "LC-PC001", "LC-PC002", "LC-PC003", "LC-PC004", "LC-PC005", "LC-PC006", "LC-PC007", "LC-PC008", "LC-PC009", "LC-PC010", "LC-PC011", "LC-PC012",... Edited May 31, 2018 by JLogan3o13 aa2zz6 and Xandy 2 "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
mikell Posted May 31, 2018 Share Posted May 31, 2018 What about a one-liner ? Local $sString = StringTrimRight(StringRegExpReplace(FileRead(@DesktopDir & "\UserMachines.txt"), '(\N+)\R?', '"$1",'), 1) Msgbox(0,"", $sString) ; FileWrite(@DesktopDir & "\newfile.txt", $sString) Link to comment Share on other sites More sharing options...
TheSaint Posted May 31, 2018 Share Posted May 31, 2018 (edited) 9 hours ago, mikell said: What about a one-liner ? Show off. @OP - Of course there are many ways. You could do a complete FileRead and FileWrite, add a double quote to start and finish, and also replace carriage returns with a double quote plus comma and a space. Actually you probably wouldn't need the finish one, but will probably need to strip the trailing comma and space. Edited June 1, 2018 by TheSaint Make sure brain is in gear before opening mouth! Remember, what is not said, can be just as important as what is said. Spoiler What is the Secret Key? Life is like a Donut If I put effort into communication, I expect you to read properly & fully, or just not comment. Ignoring those who try to divert conversation with irrelevancies. If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it. I'm only big and bad, to those who have an over-active imagination. I may have the Artistic Liesense to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage) Link to comment Share on other sites More sharing options...
TheCrimsonCrusader Posted May 31, 2018 Author Share Posted May 31, 2018 Ah crap, I forgot to remove the @CRLF after testing some other things which was part of the problem. Doh't! Anyway, thanks for the assist everyone! Much appreciated! Link to comment Share on other sites More sharing options...
mikell Posted May 31, 2018 Share Posted May 31, 2018 1 hour ago, TheSaint said: Show off. Wet blanket Should I have said "you can do the whole thing without a loop by using a single line of code" ? Link to comment Share on other sites More sharing options...
TheSaint Posted June 1, 2018 Share Posted June 1, 2018 7 hours ago, mikell said: Wet blanket Should I have said "you can do the whole thing without a loop by using a single line of code" ? Na, what you said and exampled was just fine Mr. Clever. My method, also did not require any loop of course .... despite me getting more loopy as I get older. It did however, require more than one line of code. It seems though, that none of our code examples were required, as the OP just had a Brain Fart ... don't we all occasionally. Still, I am guessing our endeavors may help someone .... sometime. Make sure brain is in gear before opening mouth! Remember, what is not said, can be just as important as what is said. Spoiler What is the Secret Key? Life is like a Donut If I put effort into communication, I expect you to read properly & fully, or just not comment. Ignoring those who try to divert conversation with irrelevancies. If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it. I'm only big and bad, to those who have an over-active imagination. I may have the Artistic Liesense to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage) 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