strate Posted October 25, 2004 Share Posted October 25, 2004 (edited) What I want to do is take a file that has a list of printer paths and make it so the user can edit this list. What the attached code does is reads the whole list except the one that matches what the user entered into a temporary file. The original file is deleted, the temp. file is then copied to the originals location, and finally the temp. file is deleted. Believe it or not, this actually works for me -kinda-, when it enters the new data it leaves a blank line between the text. It is caused prior to the new line being appended. How I want it- PRINTER1 on LPT1 PRINTER2 on LPT2 How I get it- PRINTER1 on LPT1 PRINTER2 on LPT2 expandcollapse popupCODE: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Declare $file="G:\Projects\AutoIt V3\Printers.txt" $TempFile="G:\Projects\AutoIt V3\Dump\Printers.txt" ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Declare ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; User $PrinterPath = InputBox("Label Creation","Please input printer path you would like to change:") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; User ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Finds printer request in $file / Body of Program $x=Stringsplit(StringReplace(FileRead($file,FileGetSize($file)),@lf,""),@cr) $start=0 for $i=1 to $x[0] if StringLeft($x[$i],30) = $PrinterPath then $start=1 $xx=Stringsplit($x[$i],",") next if $start =0 then msgbox(1,"System Message","Printer is not on file") Exit EndIf $x=Stringsplit(StringReplace(FileRead($file,FileGetSize($file)),@lf,""),@cr) $start=0 for $i=1 to $x[0] if StringLeft($x[$i],30)= $PrinterPath then $start=1 If $start = 0 Then $File2 = FileWrite($TempFile,$x[$i]&@CRLF) If $file2 = 0 Then MsgBox(0, "Error", "Unable to write file. - 1") Exit EndIf EndIf If $start = 1 AND $x[$i] <> $PrinterPath Then $File2 = FileWrite($TempFile,$x[$i]&@CRLF) If $file2 = 0 Then MsgBox(0, "Error", "Unable to write file. - 2") Exit EndIf EndIf if StringLeft($x[$i],30)= $PrinterPath then $start=1 next ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Finds Printer request in $file / Body of Program ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Deletes $file / Copies $TempFile to $file / Deletes $tempFile FileDelete($file) If FileExists($file) = 1 Then MsgBox(4096,"", $file&" wasn't deleted.") Exit EndIf FileCopy ($TempFile,$file,1) FileDelete($TempFile) If FileExists($TempFile) = 1 Then MsgBox(4096,"", $TempFile&" wasn't deleted.") Exit EndIf ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Deletes $file / Copies $TempFile to $file / Deletes $tempFile ;;;;;;;;; User Input $PrinterPath = InputBox("Label Creation","Please input new printer path:") ;;;;;;;;; Body $x=Stringsplit(StringReplace(FileRead($file,FileGetSize($file)),@lf,""),@cr) $start=0 for $i=1 to $x[0] if StringLeft($x[$i],30)= $PrinterPath then $start=1 if $start=1 then msgbox(1,"System Message","Printer already on file") Exit EndIf next ;;;;;; Writes new entry $File2 = FileWrite($file,$PrinterPath&@CRLF) If $file2 = 0 Then MsgBox(0, "Error", "Unable to write file. - 2") Exit EndIf Sorry for the horrible formatting. Any help would be great. For I am sure this could also be accomplished with less code. Edited October 26, 2004 by Larry INI TreeViewA bus station is where a bus stops, a train station is where a train stops. Onmy desk I have a work station... Link to comment Share on other sites More sharing options...
scriptkitty Posted October 26, 2004 Share Posted October 26, 2004 wow, I see a great case for ini files. $var = IniRead("C:\Temp\myfile.ini", "path1", "key", "NotFound") if $var="NotFound" then $PrinterPath = InputBox("Label Creation","Please input printer path you would like to change:") IniWrite("C:\Temp\myfile.ini", "path1", "key", $PrinterPath) else $x=msgbox(1,"Question","would you like to see what we have for printer 1?") if $x=1 then MsgBox(4096, "Result", $var) endif It is very easy to write clean ini files, and delete keys that are unused. The format is very easy to use, and since you said two printers, I think this might be the best solution. Look at the file it makes. I use a lot of filewriteline, and stringspliit, but you may find thiis far easier. AutoIt3, the MACGYVER Pocket Knife for computers. Link to comment Share on other sites More sharing options...
strate Posted October 26, 2004 Author Share Posted October 26, 2004 wow, I see a great case for ini files.$var = IniRead("C:\Temp\myfile.ini", "path1", "key", "NotFound") if $var="NotFound" then $PrinterPath = InputBox("Label Creation","Please input printer path you would like to change:") IniWrite("C:\Temp\myfile.ini", "path1", "key", $PrinterPath) else $x=msgbox(1,"Question","would you like to see what we have for printer 1?") if $x=1 then MsgBox(4096, "Result", $var) endifIt is very easy to write clean ini files, and delete keys that are unused. The format is very easy to use, and since you said two printers, I think this might be the best solution.Look at the file it makes. I use a lot of filewriteline, and stringspliit, but you may find thiis far easier.<{POST_SNAPBACK}>What I'm trying to do is create something so that in the future I can add printers, users, file names or whatever else needed in the future. Currently I have two printers but in the future talk souds like I may have 13 so being able to add to this list is important. section - The section name in the .ini file. key - The key name in the in the .ini file. How are these used? INI TreeViewA bus station is where a bus stops, a train station is where a train stops. Onmy desk I have a work station... Link to comment Share on other sites More sharing options...
scriptkitty Posted October 26, 2004 Share Posted October 26, 2004 (edited) run it and look at the file might be the best thing. Ini files are nice and organised.[path1]key=\\scriptkitty\laserjet99section - shown above as path1key - shown above as keyif you add more key names, they will populate under the section you give it, so you can make sections like Printer_Path, users, file names, and what ever else you need, and then put the info for each as a Key.It will be easier to understand if you play around a bit with it, look at the file it makes, etc.edit, oh yea, make sure you have a c:\temp folder or change my code a bit Edited October 26, 2004 by scriptkitty AutoIt3, the MACGYVER Pocket Knife for computers. Link to comment Share on other sites More sharing options...
strate Posted October 26, 2004 Author Share Posted October 26, 2004 (edited) run it and look at the file might be the best thing. Ini files are nice and organised.section - shown above as path1key - shown above as keyif you add more key names, they will populate under the section you give it, so you can make sections like Printer_Path, users, file names, and what ever else you need, and then put the info for each as a Key.It will be easier to understand if you play around a bit with it, look at the file it makes, etc.edit, oh yea, make sure you have a c:\temp folder or change my code a bit <{POST_SNAPBACK}>I changed the code a bit. What is the best method od searching a ini so I cannot enter the samething twice? Here is the code as of right now.$File = "G:\Projects\AutoIt V3\Prod. Recording\printers.ini" $C = InputBox("Label Creation","How many printer paths do you need to create:") $CO = 0 $P = 0 Do $PrinterPath = InputBox("Label Creation","Please input printer path you would like to create:") $x=Stringsplit(StringReplace(FileRead($file,FileGetSize($file)),@lf,""),@cr) $start=0 for $i=1 to $x[0] $P = $P +1 if StringLeft($x[$i],30)= $PrinterPath then $start=1 if $start=1 then msgbox(1,"System Message","Printer already on file") Exit EndIf next msgbox(0,"",$P) $var = IniRead($File, "Path"&$C, "key", "NotFound") if $Start = "1" then IniWrite($File,"Path"&$C,"key",$PrinterPath) msgbox(0,"",$var) else $x=msgbox(1,"Question","would you like to see what we have for printer 1?") if $x=1 then MsgBox(4096, "Result", $var) endif $CO = $CO +1 Until $C = $CO Edited October 26, 2004 by Larry INI TreeViewA bus station is where a bus stops, a train station is where a train stops. Onmy desk I have a work station... Link to comment Share on other sites More sharing options...
strate Posted October 26, 2004 Author Share Posted October 26, 2004 I figured I would throw this in here if anybody ever tried to search something like it again. Editting would be needed of course. Thanks for your help. $File = "G:\Projects\AutoIt V3\Prod. Recording\printers.ini" $CP = 0 $C = 0 $RL = 1 While 1 $line = FileReadLine($file,$RL) msgbox(1,"",$Line) $RL = $RL + 1 If $Line = "" Then Exitloop Wend $RL = $RL/2 $NumofPaths = InputBox("Label Creation","How many paths would you like to create:") Do $C = $C + 1 $PrinterPath = InputBox("Label Creations","Please input printer path you would like to create:") $x=Stringsplit(StringReplace(FileRead($file,FileGetSize($file)),@lf,""),@cr) $start=0 for $i=1 to $x[0] if StringLeft($x[$i],30)= "key="&$PrinterPath then $start=1 if $start=1 then msgbox(1,"System Message","Printer already on file") Exit EndIf next if $Start = "0" then IniWrite($File,"Path "&$RL,"key",$PrinterPath) msgbox(0,"",$PrinterPath&" Added to program") endif $RL = $RL + 1 Until $C = $NumofPaths INI TreeViewA bus station is where a bus stops, a train station is where a train stops. Onmy desk I have a work station... Link to comment Share on other sites More sharing options...
scriptkitty Posted October 27, 2004 Share Posted October 27, 2004 (edited) Take a look at this. It might serve ya a bit better. expandcollapse popup$File = "G:\Projects\AutoIt V3\Prod. Recording\printers.ini" $list="" $list2=@crlf $count=0 $counter=0 $badnum="" readini() do $delete = InputBox("Label Creations","Please input printer path you would like to eliminate:"& $list2) IniDelete($File,"PrinterPaths", "path" & $delete) if $delete<>"" then readini() until $delete="" $list_array=StringSplit($list,@cr) $NumofPaths = InputBox("Label Creation","How many paths would you like to create:" & @crlf & $list_array[0]-1&" on file") if int($NumofPaths)<1 then exit Do $PrinterPath = InputBox("Label Creations","Please input printer path you would like to create:") for $i=1 to $list_array[0] if $PrinterPath=$list_array[$i] or $PrinterPath="" then msgbox(1,"System Message","Printer already on file or invalid") continueloop(2) EndIf next writeini() $count= $count + 1 Until $Count = $NumofPaths func writeini() for $x=1 to 100 if StringInstr($badnum,"|"&$x&"|") then continueloop Iniwrite ( $File, "PrinterPaths", "path" & $x, $PrinterPath ) msgbox(0,"",$PrinterPath&" Added to program") exitloop next endfunc func readini() $list="" $list2=@crlf $counter=0 $badnum="|" For $i= 1 to 100 $line = IniRead ( $File, "PrinterPaths", "path" & $i, "Error" ) if $line="Error" then continueloop ;msgbox(1,"Printer Path " & $i,$line) $list=$list & $line & @cr $list2=$list2 &$i &" - "& $line & @crlf $counter=$counter+1 $badnum=$badnum & $i &"|" next endfunc It keeps the Ini pretty clean, and gives you up to 100 printer Paths, although once you get over 5, you should make a longer display for input box. It might be a good learning script now that you have done some writing of your own. the writeini function tries to write the lowet numbers it can, and fill in those blank ones you may have deleted. Edited October 27, 2004 by scriptkitty AutoIt3, the MACGYVER Pocket Knife for computers. Link to comment Share on other sites More sharing options...
strate Posted October 27, 2004 Author Share Posted October 27, 2004 Take a look at this. It might serve ya a bit better.expandcollapse popup$File = "G:\Projects\AutoIt V3\Prod. Recording\printers.ini" $list="" $list2=@crlf $count=0 $counter=0 $badnum="" readini() do $delete = InputBox("Label Creations","Please input printer path you would like to eliminate:"& $list2) IniDelete($File,"PrinterPaths", "path" & $delete) if $delete<>"" then readini() until $delete="" $list_array=StringSplit($list,@cr) $NumofPaths = InputBox("Label Creation","How many paths would you like to create:" & @crlf & $list_array[0]-1&" on file") if int($NumofPaths)<1 then exit Do $PrinterPath = InputBox("Label Creations","Please input printer path you would like to create:") for $i=1 to $list_array[0] if $PrinterPath=$list_array[$i] or $PrinterPath="" then msgbox(1,"System Message","Printer already on file or invalid") continueloop(2) EndIf next writeini() $count= $count + 1 Until $Count = $NumofPaths func writeini() for $x=1 to 100 if StringInstr($badnum,"|"&$x&"|") then continueloop Iniwrite ( $File, "PrinterPaths", "path" & $x, $PrinterPath ) msgbox(0,"",$PrinterPath&" Added to program") exitloop next endfunc func readini() $list="" $list2=@crlf $counter=0 $badnum="|" For $i= 1 to 100 $line = IniRead ( $File, "PrinterPaths", "path" & $i, "Error" ) if $line="Error" then continueloop ;msgbox(1,"Printer Path " & $i,$line) $list=$list & $line & @cr $list2=$list2 &$i &" - "& $line & @crlf $counter=$counter+1 $badnum=$badnum & $i &"|" next endfuncIt keeps the Ini pretty clean, and gives you up to 100 printer Paths, although once you get over 5, you should make a longer display for input box.It might be a good learning script now that you have done some writing of your own.the writeini function tries to write the lowet numbers it can, and fill in those blank ones you may have deleted.<{POST_SNAPBACK}>I receive an error that reads on Line 27 *File Location*continueloop(2)continueloop^ERRORError: Illegal text at the end of statement (one statement per line). INI TreeViewA bus station is where a bus stops, a train station is where a train stops. Onmy desk I have a work station... Link to comment Share on other sites More sharing options...
Developers Jos Posted October 27, 2004 Developers Share Posted October 27, 2004 I receive an error that reads on Line 27 *File Location*continueloop(2)continueloop^ERRORError: Illegal text at the end of statement (one statement per line).<{POST_SNAPBACK}>ContinueLoop (2) will only work with the current 103 unstable version. 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...
strate Posted October 27, 2004 Author Share Posted October 27, 2004 ContinueLoop (2) will only work with the current 103 unstable version.<{POST_SNAPBACK}>How could I get around this? I need to use a stable version. INI TreeViewA bus station is where a bus stops, a train station is where a train stops. Onmy desk I have a work station... Link to comment Share on other sites More sharing options...
Josbe Posted October 27, 2004 Share Posted October 27, 2004 ContinueLoop (2) will only work with the current 103 unstable version.<{POST_SNAPBACK}> AUTOIT > AutoIt docs / Beta folder - AutoIt latest beta Link to comment Share on other sites More sharing options...
Developers Jos Posted October 27, 2004 Developers Share Posted October 27, 2004 <{POST_SNAPBACK}>josbe, what do you mean ? 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...
Josbe Posted October 27, 2004 Share Posted October 27, 2004 josbe, what do you mean ?<{POST_SNAPBACK}>@JdeB: Sorry, forget it. I don't noticed this yet...(levels for ContinueLoop under v3.0.102) :"> AUTOIT > AutoIt docs / Beta folder - AutoIt latest beta Link to comment Share on other sites More sharing options...
scriptkitty Posted October 27, 2004 Share Posted October 27, 2004 (edited) Old version, with a few glitches removed: expandcollapse popup$File = "G:\Projects\AutoIt V3\Prod. Recording\printers.ini" $list="" $list2=@crlf $count=0 $counter=0 $badnum="" $err=2 readini() do $delete = InputBox("Label Creations","Please input printer path you would like to eliminate:"& $list2,"","",200,($counter*15)+120) IniDelete($File,"PrinterPaths", "path" & $delete) if $delete<>"" then readini() until $delete="" $list_array=StringSplit($list,@cr) $NumofPaths = InputBox("Label Creation","How many paths would you like to create:" & @crlf & $list_array[0]-1&" on file") if int($NumofPaths)<1 then exit Do $list_array=StringSplit($list,@cr) $PrinterPath = InputBox("Label Creations","Please input printer path you would like to create:") for $i=1 to $list_array[0] if $PrinterPath=$list_array[$i] or $PrinterPath="" then msgbox(1,"System Message","Printer already on file or invalid") $err=1 ContinueLoop EndIf next if $err=2 Then writeini() readini() $count= $count + 1 EndIf $err=2 Until $Count = $NumofPaths func writeini() for $x=1 to 100 if StringInstr($badnum,"|"&$x&"|") then continueloop Iniwrite ( $File, "PrinterPaths", "path" & $x, $PrinterPath ) msgbox(0,"",$PrinterPath&" Added to program") exitloop next endfunc func readini() $list="" $list2=@crlf $counter=0 $badnum="|" For $i= 1 to 100 $line = IniRead ( $File, "PrinterPaths", "path" & $i, "Error" ) if $line="Error" then continueloop ;msgbox(1,"Printer Path " & $i,$line) $list=$list & $line & @cr $list2=$list2 &$i &" - "& $line & @crlf $counter=$counter+1 $badnum=$badnum & $i &"|" next endfunc edit forgot to remove my path, fixed now. Edited October 27, 2004 by scriptkitty AutoIt3, the MACGYVER Pocket Knife for computers. 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