Subject Re: Computer serial number
From Mervyn Bick <invalid@invalid.invalid>
Date Sat, 20 Apr 2024 13:11:53 +0200
Newsgroups dbase.getting-started

On 2024/04/20 10:38, Heinz Kesting wrote:

> At the moment I can not spent time trying to get the string out of the
> textfile without those unwanted spaces in the first place, but you can
> easily remove them afterwards with the following small routine.
>
> for i = 1 to len(mserial_no)
>     if substr(mserial_no, i, 1) = [ ]           // a BLANK is found
>        mserial_no = stuff(mserial_no, i, 1, []) // removes each BLANK
>     endif
> next i
>
> ? ":" + mserial_no + ":" // now all BLANKS should be removed
>
> Hope this helps, kind regards, Heinz
>

I'm afraid, although it's not difficult, it's not quite that easy. :-(

The file created by "wmic bios get serialnumber" is UTF-16 encoded. This
means that there is a leading 0xFF and 0xFE in the file as a BOM (byte
order mark) and each character is saved as two bytes.  Rachpal hasn't
mentioned it but this is why printing the first line from the file would
display as ÿþS e r i a l N u  m b e r

Although they show as spaces when the serial number is displayed on
screen there are actually nulls (chr(0)) not spaces (chr(32)) in the
text string saved in the file.  Unfortunately stuff() doesn't seem to
know what to do with nulls so changing substr(mserial_no, i, 1) = [ ] to
substr(mserial_no, i, 1) = chr(0) won't do the job.

As an aside, whenever one removes characters from a string it is better
to start from the end and work back to the beginning i.e for i =
len(mserial_no) to 1 step -1.  It would be off-topic to discuss this
here but one should accept this as being inscribed on a tablet of stone.
:-)

There is probably more than one way to do this but rewriting to a new
file without the BOM and the nulls is perhaps as good as any.

Rachpal, using RUN opens a Command window and although it's open for a
very short time the "flash" is extremely annoying.  Rather use
runHidden() from the dUFLP.  If you don't already have a copy of the
dUFLP (dBase Users Function Library) you can get it from Ken Mayer's
website http://www.goldenstag.net/dbase/index.htm#duflp  The
instructions for installing the dUFLP are on the website.

Using Web-News from the link on the dBASE website is a quick way to
access the newsgroups but it soon becomes a PITA.  If your email client
supports NNTP feeds you can access the newsgroups at news.dbase.com and
have posts delivered like your email instead if having to access
Web-News regularly to see if there is anything new. If it doesn't,
consider downloading Thunderbird to access the newsgroups.  If you do
use Thunderbird remember you need to use the "Follow up" button, not the
"Reply" button to respond to messages.


************* Start of program **********
set procedure to :duflp:miscApi.prg
runHidden("wmic bios get serialnumber > serial_no.txt")
inkey(0.5) //Give Windows time to finish writing the file to disk.
            //If you get an "In use..." error increase time slightly.
f = new file()
f.open("serial_no.txt")
fOut = new file()
fOut.create('serial_no1.txt') //Create() overwrites an existing file.
do while not f.eof()
    cRead = f.read(1)
       if asc(cRead) > 0 and asc(cRead) <= 128
          fOut.write(cRead)
       endif
enddo
f.close()
fOut.close()
f = new file()
f.open('serial_no1.txt')
mserial_head = f.gets()
mserial_no = f.gets()
f.close()
erase serial_no.txt //Get rid of file with UTF-16 encoding
erase junk.bat //Batch file created by runHidden()
clear
? mserial_head
? mserial_no
close procedure :duflp:miscApi.prg
******** End of program *************

Mervyn.