Page 1 of 2 12>
Topic Options
#94581 - 2002-10-20 04:57 AM Best options/best way?
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Looking for the best ways/arguments to do the following operations in KiXtart..

1. Getting a major file version
Short version
code:
$RegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE"
$outmaj = SUBSTR((GETFILEVERSION(READVALUE($RegKey,""))), 1, (INSTR(GETFILEVERSION(READVALUE($RegKey,"")), ".")-1))
?$outmaj

Long version
code:
$RegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE"
$outver = (GETFILEVERSION(READVALUE($RegKey,"")))
$pos = (INSTR(GETFILEVERSION(READVALUE($RegKey,"")), ".")-1)
$outmaj = SUBSTR($outver, 1, $pos)
?$outmaj

2. Check for Existence of direcotry
Option one, look for the attributes
code:
IF GetFileAttr( "C:\ScriptTest" ) & 16
ELSE
MD "C:\ScriptTest"
ENDIF

Using EXIST to test for a folder
code:
IF (EXIST("C:\ScriptTest\nul") <> 1)
MD "C:\ScriptTest"
ENDIF

3. Look for a file existence
Option 1
code:
IF EXIST("C:\ScriptTest\Program.exe")
ELSE
COPY @LSERVER+"Programs\Program.exe" "C:\ScriptTest"
ENDIF

Option 2
code:
IF (1 <> EXIST("C:\ScriptTest\Program.exe"))
COPY @LSERVER+"Programs\Program.exe" "C:\ScriptTest"
ENDIF

Option 3
code:
IF (EXIST("C:\ScriptTest\Program") <> 1)
COPY @LSERVER+"Programs\Program.exe" "C:\ScriptTest"
ENDIF

Thanks,

Kent

[ 20. October 2002, 08:06: Message edited by: kdyer ]
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#94582 - 2002-10-20 05:35 AM Re: Best options/best way?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Yo Kent - why did you post this in general discussions - wouldn't this be a better fit for the FAQ forum ? This is totally kix-worthy.
Top
#94583 - 2002-10-20 06:11 AM Re: Best options/best way?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Shawn,
Did you miss the question? Nobody (except me) could reply if it were in FAQ.

Kent,
For item 1, not sure what in particular you are after. I don't have Outlook, but will comment on my observations. You ReadValue() twice instead of assigning to a var and working with the var. You use GetFileVersion() which doesn't work if the file is in use. FSO would be better.

For item 2 & 3, Are you avoiding the use of NOT for KiX 3.x compatabilty?

Otherwise, it's pretty much six of one, half dozen of the other...
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#94584 - 2002-10-20 06:29 AM Re: Best options/best way?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
ah - yup - ok - im on the same page as you guys now - maybe scripts then ?

item 1 no comment

item 2 - agree with Les, use NOT or the second option ... i would avoid the reverse logic in option one - or turn the expression around like this:

code:
IF (GetFileAttr( "C:\ScriptTest" ) & 16) = 0
MD "C:\ScriptTest"
ENDIF

item 3 - use NOT or option 3

my 2 cents cdn.

[ 20. October 2002, 06:29: Message edited by: Shawn ]

Top
#94585 - 2002-10-20 08:00 AM Re: Best options/best way?
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
The reason I bring this up is that we are overhauling our logon scripts and I was trying think of how do things more efficiently or the best way..

I think I have the answers I need..

#1 - Option 1. It was merely an experiment to see how well Kix handles nested FUNCTIONS. You could Use WINWORD.EXE, or IEXPLORE.EXE as alternatives. The way I have this presented it would be very difficult to debug if there was something wrong with the script.

#2 - #3, I like the IF..ELSE..ENDIF Logic. We have our clients up on 4.02.. So, sure the NOT operator would be fine. However, when we are creating FAQs, and responses to forum posts the scripts we should create these for all target audiences, right?

Thanks!

Kent

[ 20. October 2002, 08:01: Message edited by: kdyer ]
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#94586 - 2002-10-20 04:21 PM Re: Best options/best way?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
1) I'd prefer the long version, however, I would only use one readvalue and store the result in a variable.

2) I'd go with option 2 but use the NOT EXIST() format.

3) I'd go with option 3, but, as in 2), would use the NOT EXIST() format.
_________________________
There are two types of vessels, submarines and targets.

Top
#94587 - 2002-10-21 06:52 AM Re: Best options/best way?
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
This has been added in as an FAQ -
File Operations


With some modifications. [Smile]

Thanks!

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#94588 - 2002-10-28 06:07 PM Re: Best options/best way?
Bxn Offline
Getting the hang of it

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
I just wonder if the commands launched after the tests aren't a little ... 'premature'.
I mean :

2. Check for Existence of direcotry
Option one, look for the attributes

code:
--------------------------------------------------------------------------------

IF GetFileAttr( "C:\ScriptTest" ) & 16ELSE MD "C:\ScriptTest"ENDIF

--------------------------------------------------------------------------------

Is telling us that "C:\ScriptTest" is not a directory, but it could be a file.
In this case, if we try to create such a folder, an error 183 would certainly occur.

Using EXIST to test for a folder

code:
--------------------------------------------------------------------------------

IF (EXIST("C:\ScriptTest\nul") <> 1) MD "C:\ScriptTest"ENDIF

--------------------------------------------------------------------------------
If a file called C:\ScriptTest exists, the Exist() function returns 1, so the folder is not created. But it doesn't exist.

3. Look for a file existence
Option 1

code:
--------------------------------------------------------------------------------

IF EXIST("C:\ScriptTest\Program.exe")ELSE COPY @LSERVER+"Programs\Program.exe" "C:\ScriptTest"ENDIF

--------------------------------------------------------------------------------

Option 2

code:
--------------------------------------------------------------------------------

IF (1 <> EXIST("C:\ScriptTest\Program.exe")) COPY @LSERVER+"Programs\Program.exe" "C:\ScriptTest"ENDIF

--------------------------------------------------------------------------------

If A folder named C:\ScriptTest\Program.exe exists, the copy won't be done (and couldn't be done) but this file doesn't exist.

Option 3

code:
--------------------------------------------------------------------------------

IF (EXIST("C:\ScriptTest\Program") <> 1) COPY @LSERVER+"Programs\Program.exe" "C:\ScriptTest"ENDIF

If A file or a folder named C:\ScriptTest\Program exists, the copy won't be done but the file C:\ScriptTest\Program.Exe doesn't exist.

My conclusion would be these tests are certainly good for what the titles say (searching if xxx is a directory etc ...), but perhaps not for conclude we can or cannot create folders or copy files.

Am I wrong ?

Top
#94589 - 2002-10-29 05:34 PM Re: Best options/best way?
Bxn Offline
Getting the hang of it

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
Prepare your riot guns guys !

I hope my previous post was not too dry ...
[Frown]
To correct this eventuality, I hope you'll be pleased with this illustration.

You will now be allowed to shoot at me for the reason you prefer : this code is faulty, misconstructed, puzzling for newbees, makes easy things harder, or whatever you'll find !

code:
 ;Trying To Create A Folder If Necessary
$FolderNameToCheck = "C:\ScriptTest"
$Result = -1

If Exist($FolderNameToCheck)
If GetFileAttr($FolderNameToCheck) & 16
$Result = 1
Else
$Result = -2
EndIf
Else
MD $FolderNameToCheck
$CommandError = @Error
$CommandSError = @SError
If $CommandError = 0
$Result = 0
Else
$Result = -3
$CommandError = CStr($CommandError)
EndIf
EndIf

Select
Case $Result = 0
? "Folder " + $FolderNameToCheck + " Successfully Created"
Case $Result = 1
? "Folder " + $FolderNameToCheck + " Already Exists"
Case $Result = -2
? "Can't create Folder " + $FolderNameToCheck
? "Duplicated File/Folder Name"
Case $Result = -3
? "An Error Has Occured During " + $FolderNameToCheck + " Folder Creation"
? "[" + $CommandSError + "] " + $CommandSError
Case 1
? "UnExpected Result"
EndSelect

If $Result >= 0
? "Ok, We Can Go On"
Else
? "Something Has Happend, We Have To Check "
EndIf

That was my attempt to prevent unexpected errors in my own scripts.
I'm sure you'll find easier, safer or more pertinent ways and I'd be glad to hear from you ...

Top
#94590 - 2002-10-30 04:58 PM Re: Best options/best way?
Bxn Offline
Getting the hang of it

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
Almost Two days without reaction ...
Please, let me know if I smell Camembert, and I won't pollute the board anymore ... [Confused]

Top
#94591 - 2002-10-30 05:02 PM Re: Best options/best way?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
bxn, well.
I saw a comment in:
http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic;f=10;t=000056#000001

anyway, I have to test some of your arguments to see that they are true...
_________________________
!

download KiXnet

Top
#94592 - 2002-10-30 05:09 PM Re: Best options/best way?
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Since I started this...

I will respond..

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#94593 - 2002-10-30 05:09 PM Re: Best options/best way?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
My flame thrower is thoroughly holstered because to be honest, I don't see anything flamable with your code. The only thing I might say is that it would make a better UDF, than a stand-alone section of code. In fact, this looks as if it was/is a UDF ... if this is the case, the only thing I might critique is the fact that UDF's shouldn't pump-out console messages (for a couple of reasons) unless maybe this "feature" was parameterized.

Is this the kind of feedback you were looking for ?

-Shawn

Top
#94594 - 2002-10-30 05:19 PM Re: Best options/best way?
Bxn Offline
Getting the hang of it

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
Shawn,

Thanx for your reply.
I fully understand (i think) your remarks and agree with all of them.
The code I provided was just as illustration of the potential pbs I previously had remarked in my scripts and the console messages should of course be 'remarked' in an exploitation context.
I also introduced them for comments purposes for all of you to test/read.
As I previously wrote, all sorts of remarks from you would have been well appreciated.
I just wanna add that I wanted to make these precisions, because I felt that this topic was adequately placed in FAQ section for everyone, but could have 'as is' induce, one day or another, some puzzling errors.
Thanx again.

Top
#94595 - 2002-10-30 05:21 PM Re: Best options/best way?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I interpret BXN's code as a proof-of-concept with all that console output and not production code.

I still think an FSO solution would be nice to include as somtimes files are in use.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#94596 - 2002-10-30 05:34 PM Re: Best options/best way?
Bxn Offline
Getting the hang of it

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
Les, (?)
Files In Use Issue is an interesting way to explore.
But I have to confess I'm confused with the term "FSO" ...
Could you enlight me please ???

Top
#94597 - 2002-10-30 05:50 PM Re: Best options/best way?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
FSO = FileSystemObject [Big Grin]
_________________________
There are two types of vessels, submarines and targets.

Top
#94598 - 2002-10-30 06:03 PM Re: Best options/best way?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I'l see Jens post and raise it... [Big Grin]
code:
Break on
$BasePath = "%windir%\system32\config\"
$strFile = "appevent.evt","secevent.evt","sysevent.evt"
For each $element in $strFile
$objFSO = CreateObject("Scripting.FileSystemObject")
? $BasePath + $element + " = "
$objFSO.GetFile($BasePath + $element).Size
Next
$objFSO = ""
get $_

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#94599 - 2002-10-31 08:19 AM Re: Best options/best way?
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Did some testing and here is what I was able to throw together..

bxn, you are correct about error 183.

This hopefully addresses point one.

We'll just focus on the file/folder existence thingy.

First way - using fso:

code:
 ; - Test environment: Windows XP
; - Kix Version: 4.12
; - Kent Dyer
; - 30-October-2002
; - version .8
; - Test: Create a simple text file called ScripTest and insert some text in it
; - Findings: a file name and folder name of the same name cannot exist on the same volume
; - Thanks: to bxn from http://kixtart.org for pointing out the error 183 when a file exists with
; - same name as the folder

; - using fso per Les
BREAK ON
CLS
;Trying To Create A Folder as Necessary
$foldernametocheck = "C:\ScriptTest"
$lenfolder = Len($foldernametocheck)
$foldername = substr($foldernametocheck,4,$lenfolder)

; - First check to see if this is a file
IF EXIST($foldernametocheck) AND NOT (GETFILEATTR($foldernametocheck) & 16)
? $foldernametocheck + " appears to exist as a file"
?'Press Y and <ENTER> to continue and rename the file and create the folder'
?'Press N and <ENTER> to exit'
?'>'
GETS $k
IF $k = 'Y'
SHELL "%comspec% /c ren " + $foldernametocheck + " " + $foldername + "1"
ReportFolderStatus($foldernametocheck)
ELSE
RETURN
ENDIF
ELSE
?$foldernametocheck + " Appears to exist as a folder."
SLEEP 2
ENDIF

FUNCTION ReportFolderStatus($fldr)
; - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/jsmthFolderExists.asp
DIM $fso, $msg
$fso = CreateObject("Scripting.FileSystemObject")
IF ($fso.folderexists($fldr))
?$fldr + " exists."
ELSE
?$fldr + " doesn't exist."
?"Creating now"
MD $foldernametocheck
IF @error <> 0
?@error
SLEEP 2
ENDIF
ENDIF
ENDFUNCTION
? "File/folder checks complete"
? "Press any Key to exit"
GET $l

Second way - using Kixtart:
code:
 ; - Test environment: Windows XP
; - Kix Version: 4.12
; - Kent Dyer
; - 30-October-2002
; - version .8
; - Test: Create a simple text file called ScripTest and insert some text in it
; - Findings: a file name and folder name of the same name cannot exist on the same volume
; - Thanks: to bxn from http://kixtart.org for pointing out the error 183 when a file exists with
; - same name as the folder
;
; - using Kixtart commands
BREAK ON
CLS
;Trying To Create A Folder as Necessary
$foldernametocheck = "C:\ScriptTest"
$lenfolder = Len($foldernametocheck)
$foldername = substr($foldernametocheck,4,$lenfolder)

; - First check to see if this is a file
IF EXIST($foldernametocheck) AND NOT (GETFILEATTR($foldernametocheck) & 16)
? $foldernametocheck + " appears to exist as a file"
?'Press Y and <ENTER> to continue and rename the file and create the folder'
?'Press N and <ENTER> to exit'
?'>'
GETS $k
IF $k = 'Y'
SHELL "%comspec% /c ren " + $foldernametocheck + " " + $foldername + "1"
?"Creating "+ $foldernametocheck + " now"
MD $foldernametocheck
IF @error <> 0
?@error
SLEEP 2
ENDIF
ELSE
RETURN
ENDIF
ENDIF
IF EXIST($foldernametocheck) AND (GETFILEATTR($foldernametocheck) & 16)
?$foldernametocheck + " Appears to exist as a folder."
SLEEP 2
RETURN
ELSE
?"Creating " + $foldernametocheck + " now"
MD $foldernametocheck
IF @error <> 0
?@error
SLEEP 2
ENDIF
ENDIF
? "File/folder checks complete"
? "Press any Key to exit"
GET $l

Thank bxn for pointing the error condition out..

Thanks Les for pointing out the option for fso.

Now, to look at the Program existence thing. [Smile]

Cheers,

Kent

[ 03. November 2002, 18:00: Message edited by: kdyer ]
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#94600 - 2002-10-31 08:37 AM Re: Best options/best way?
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Folder/Program check..

This hopefully addresses point two and three.

Here is the corrected version:

code:
 ; - Test environment: Windows XP
; - Kix Version: 4.12
; - Kent Dyer
; - 30-October-2002
; - version .8
; - Test: Create a Folder in a sub-directory with the name of Program.exe
; - Findings: a file name and folder name of the same name cannot exist on the same volume
; - Thanks: to bxn from http://kixtart.org for pointing out the error 183 when a file exists with
; - same name as the folder
;
; - using Kixtart commands
BREAK ON
CLS
;Trying To Create A Folder and copy a file as Necessary
$folder = "C:\ScriptTest"
$program = "Program.exe"

IF (GETFILEATTR($folder) & 16) ; - Check to not only see if the file exists, but the folder too
IF EXIST($folder + "\" + $program)
IF EXIST($folder + "\" + $program) AND (GETFILEATTR($folder + "\" + $program) & 16)
?$folder + "\" + $program + " appears to be a folder - please correct"
SLEEP 2
RETURN
ELSE
?$folder + "\" + $program + " appears to be correct"
ENDIF
ELSE
COPY @lserver+"Programs\"+ $program $folder
IF @error <> '0'
?@error
ENDIF
ENDIF
ELSE
?'Press Y and <ENTER> to continue and create the folder and copy the file'
?'Press N and <ENTER> to exit'
?'>'
GETS $k
IF $k = 'Y'
MD $folder
IF @error <> '0'
?@error + ' Error while creating the directory'
ENDIF
COPY @lserver+"Programs\"+ $program $folder
IF @error <> '0'
?@error
ENDIF
ELSE
RETURN
ENDIF
ENDIF
? "File/folder checks complete"
? "Press any Key to exit"
GET $l

Thanks again,

Kent

[ 31. October 2002, 09:06: Message edited by: kdyer ]
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
Page 1 of 2 12>


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 265 anonymous users online.
Newest Members
Timothy, Jojo67, MaikSimon, kvn317, kixtarts2025
17874 Registered Users

Generated in 0.086 seconds in which 0.03 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org