I am posting here again some of the Foxpro type String manipulation commands that I regularily use with AHK`s FileAppend.
The original post is @ Ask-For-Help Topic: Printing an ASCII formatted text file in Windows.
Functions Supplementing FileAppend
SetWidth() : SetWidth increases a String's length by adding spaces to it and aligns it Left/Center/Right. ( Requires Space() )SetWidth(Str,Width,AlignText) { If (AlignText!=0 and AlignText!=1 and AlignText!=2) AlignText=0 if AlignText=0 { RetStr= % (Str)Space(Width) StringLeft,RetStr,RetStr,%Width% } If AlignText=1 { Spaces:=(Width-(StrLen(Str))) RetStr= % Space(Round(Spaces/2))(Str)Space(Spaces-(Round(Spaces/2))) } if AlignText=2 { RetStr= % Space(Width)(Str) StringRight,RetStr,RetStr,%Width% } Return RetStr }SetWidth() supplements AHK's FileAppend() in creating well formatted Text Files. Try windir.ahk, which is a demo script that lists "Windows folder contents" (in a tabular format) to windir.txt
NumStr()NumStr( Value, Width, Dec, PadB4="" ) { AFF := A_FormatFloat SetFormat, Float, 0.%Dec% Value += 0.00 Loop If (StrLen(Value) < Width AND PadB4!="") Value := PadB4 Value else Break SetFormat, Float, %AFF% Return Value }Use NumStr() to format a float . You may also use NumStr() to pad leading characters (any character!) to a numeric string.
Examples: Number=21.3263
NumStr(Number,10,2,"_") returns "_____21.33"
NumStr(Number,10,0,"0") returns "0000000021"
NumStr(Number,10,2) returns "21.33" (Note: 21.33 is leaded by 5 spaces which I am not able to reproduce)
FileAppend, % "ItemCode" NumStr(Number,5,0,0) , Temp.txt outputs "ItemCode00021" to temp.txt
Try the Demo DiskStat.ahk or view its output DiskStat.txt. This demo uses NumStr() and SetWidth()
Edit: 2007-11-22
I use this this Shorter version now:NumFormat( Number=0, Width=3, Decimal=0, PadChar="" ) { VarSetCapacity( Padding, Width, Asc( PadChar ) ) OutLen := StrLen( Number := Round( Number, Decimal ) ) Return SubStr( Padding . Number , 1 - ( OutLen >= Width ? Outlen : Width ) ) }
Replicate()Replicate( Chr=" ", X=1 ) { Return VarSetCapacity( V, VarSetCapacity(V,VarSetCapacity(V,64)>>32)+X, Asc(Chr) ) ? V : }Example: This creates a horizontal ruling 80 characters wide
FileAppend, % replicate(chr(196),80), filename.txt
Space()Space(Width) { Loop,%Width% Space=% Space Chr(32) Return Space }SubStr() : AHK's StringMid as a FunctionThis is simpler:
FileAppend,% "Hello" Space(3) "World",filename.txt
rather than
FileAppend,Hello%A_Space%%A_Space%%A_Space%World",filename.txtSubstr(String,SPos,Chars) { StringMid,String,String,%SPos%,%Chars% Return String }String Case : Convert a string to UPPER / lower / Proper (Sentence) case.UPPER(String) { StringUpper,String,String Return String }LOWER(String) { StringLower,String,String Return String }PROPER(String) { StringLower,String,String,T Return String }Date Functions : These functions manipulate AHK's FormatTime ... FormatTime() would suffice all needs & other date functions are Superfluous & available with FoxproFormatTime(TimeString,Format) { FormatTime, FormattedTime , TimeString, %Format% return Formattedtime }CDOW(DATETIME) { FormatTime,CDOW,DATETIME,dddd Return CDOW }CMONTH(DATETIME) { FormatTime,CMONTH,DATETIME,MMMM Return CMONTH }DATE(DATETIME) { FormatTime,Date,DATETIME,dd-MM-yyyy Return Date }TIME(DATETIME) { FormatTime,Time,DATETIME,hh:mm:ss tt Return Time }TIME24(DATETIME) { FormatTime,Time,DATETIME,HH:mm:ss Return Time }DOW(DATETIME) { FormatTime,DOW,DATETIME,WDay Return DOW }
Thanks for reading this patiently.......
Edit: 2006/02/03 - Added code for function "Replicate()"
Edit: 2006/02/05 - Added code for function "Space()"
Edit: 2006/02/06 - Added code for function "SetWidth()" / Download the demo windir.ahk or view sample output windir.txt
Edit: 2006/02/12 - Added code for function "SubStr()"
Edit: 2006/02/18 - Added code for FormatTime() and other Superfluous date functions
Edit: 2006/02/19 - Added code for Upper() Lower() Proper()
Edit: 2006/02/20 - Added code for function "NumStr()" / Download the demo DiskStat.ahk or view sample output DiskStat.txt
Thank you all. Regards,