BryanVest Posted April 11, 2012 Posted April 11, 2012 (edited) I am working on a script that prints a sticker with the number. I need this serialized so I am looking to have it open the text file with read access, read the line into variable $palletnum then close the text file. Then add a 1 to the end of whats in $palletnum. The only problem is I have a letter in the front so it doesnt see how to add and just replaces the whole thing with 1. Is there a way to add to the number part and not the A in the front? The other way I can have it is to have the printing software I will be passing the number to add an A to the front(very easy), but I need the number to be 7 digits. Right now I have 0000001 in the file and when it adds 1 it just makes it 2. Is there a way to have it keep the 0's? Code: $palletfile = FileOpen("c:\palletnum.txt",0) $palletnum = FileReadLine($palletfile) FileClose($palletfile) $palletnew = $palletnum + 1 FileOpen("C:\palletnum.txt",2) FileWrite($palletfile,$palletnew) FileClose($palletfile) Msgbox(-1,"",$palletnum) Edited April 11, 2012 by BryanVest
GMK Posted April 11, 2012 Posted April 11, 2012 You could use StringRegExp to get just the number from $palletnum, add 1, then use StringFormat to put the placeholders.
BryanVest Posted April 11, 2012 Author Posted April 11, 2012 You could use StringRegExp to get just the number from $palletnum, add 1, then use StringFormat to put the placeholders.Would you have an example of such? Sorry, I have never used StringRegExp before and the help files a little confusing.
BryanVest Posted April 11, 2012 Author Posted April 11, 2012 Nevermind I was able to figure it out. Thank you for letting me know what functions to use $palletfile = FileOpen("c:palletnum.txt",0) $palletnum = FileReadLine($palletfile) FileClose($palletfile) $palletread = StringRegExp($palletnum, '([0-9]{7})', 1) $oldpallet = $palletread[0] $newpallet = $oldpallet +1 $formatpallet = StringFormat("%07i", $newpallet) FileOpen("C:palletnum.txt",2) FileWrite($palletfile,$formatpallet) FileClose($palletfile) Msgbox(-1,"",$formatpallet)
Spiff59 Posted April 11, 2012 Posted April 11, 2012 (edited) There are many ways to go about it. Here's more food for thought: $palletfile = @ScriptDir & "palletnum.txt" $hFile = FileOpen($palletfile) $oldpallet = FileRead($hFile) FileClose($hFile) $prefix = StringLeft($oldpallet, 1) $number = StringTrimLeft($oldpallet, 1) + 1 $newpallet = $prefix & StringRight("000000" & $number, 7) $hFile = FileOpen($palletfile, 2) FileWrite($hFile, $newpallet) FileClose($hFile) Edit: Your last version does remove the leading alpha character, but that could be easily corrected without moving away from StringRegExp and StringFormat. Also, your second FileOpen isn't assigning the resulting handle to any variable, so you're just gettting lucky if the $palletfile variable works. Edited April 11, 2012 by Spiff59
kylomas Posted April 11, 2012 Posted April 11, 2012 @spiff59, Another, simpler method $prefix = StringLeft($oldpallet, 1) $number = StringTrimLeft($oldpallet,1) + 1 $newpallet = $prefix & $number The bit about the file handle is interesting. This argues that $palletfile still contains a pointer (handle) to the file that can be used on subsequent opens. It would be interesting to hear from an expert about this! kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
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