Jump to content

Recommended Posts

Posted

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)

 

Posted (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 by Xandy
  • Moderators
Posted (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",...

Expand  

 

Edited by JLogan3o13

"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!

Posted (edited)
  On 5/31/2018 at 5:56 PM, mikell said:

What about a one-liner ?  :)

Expand  

Show off. :muttley:

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

  Reveal hidden contents

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)

userbar.png

Posted
  On 5/31/2018 at 8:20 PM, mikell said:

Wet blanket  :P
Should I have said "you can do the whole thing without a loop by using a single line of code" ?  :idiot:

Expand  

Na, what you said and exampled was just fine Mr. Clever. :lol:

My method, also did not require any loop of course .... despite me getting more loopy as I get older. o:)

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

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.

  Reveal hidden contents

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)

userbar.png

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...