Page 1 of 1 1
Topic Options
#180695 - 2007-09-18 09:19 PM File not open for writing
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Okay I am stumped...

 Code:
If Open(1,"SomeFile",5) = 0
  WriteLine(1,"A bunch of stuff")
EndIf


All I get are -4, File not open for writing

Yes it pseudo code, yes the permissions are correct (User running the script has full control on the folder containing the file)

What am I missing?
_________________________
Today is the tomorrow you worried about yesterday.

Top
#180697 - 2007-09-18 09:31 PM Re: File not open for writing [Re: Gargoyle]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Where is the PATH to the file?

This works for me

 Code:
If Open(1,"C:\TEMP\SomeFile.txt",5) = 0
  $wl = WriteLine(1,"A bunch of stuff"+@CRLF)
  $wl = Close (1)
EndIf

Top
#180698 - 2007-09-18 09:36 PM Re: File not open for writing [Re: NTDOC]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
The path is in the Open statement
 Code:
If Open ($FH2,"C:\Scripthome\logs\Audit_"+$input+".txt",5) = 0
This is the actual line from the script

I see the file is created but it does not want to write to it.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#180700 - 2007-09-18 09:47 PM Re: File not open for writing [Re: Gargoyle]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
Are you sure the $FH2 and $input variables are filled ?
Open() does not write to files without extentions btw (tried on the hosts file) Neither does FSO.

Top
#180701 - 2007-09-18 09:49 PM Re: File not open for writing [Re: Arend_]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
The variables must be working as the file is created as expected. I don't seem to have a problem till it tries to "Write" to it.

And it does have ".txt" extension

And the IF does not error out it is returning 0
_________________________
Today is the tomorrow you worried about yesterday.

Top
#180704 - 2007-09-18 10:19 PM Re: File not open for writing [Re: Gargoyle]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Here is the full code
 Code:
If Not @LOGONMODE
	     Break On
EndIf

$SO=SetOption("Explicit", "ON")
$SO=SetOption("NoMacrosInStrings", "ON")
$SO=SetOption("NoVarsInStrings", "ON")
$SO=SetOption("WrapAtEOL", "ON")

Dim $FH, $Date, $File, $Line, $Drive, $Element, $Input, $Type
Global $Array, $FH2

Use * "\\server\e$\audit" /user:"domainX\administrator" /password:"password"
$Drive = @RESULT
$File = DirPlus($Drive)
$FH = FreeFileHandle()
$FH2 = FreeFileHandle()
CLS
?"Search for username:"
Gets $input

If Open ($FH2,"C:\Scripthome\logs\Audit_"+$input+".txt",5) = 0

For Each $Element in $File
? $element
	If Open ($FH,$element,2) = 0
		$Line = ReadLine($FH)
			While @ERROR = 0
				$Array = Split ($Line," ")
					If $Array[2] = "DomainX\"+$Input+":"
						Select
							Case $Array[3] = "Logon"
								$Type = "On"
							Case $Array[4] = "Logoff"
								$Type = "Off"
							Case 1
								$Type = "Ignore"
						EndSelect
						Write($Type)
					EndIf
				$Line = ReadLine($FH)
			Loop
	EndIf
	Close ($FH)
Next

Close($FH2)

Else
	?@SERROR
EndIf
Use * /delete
Function Write($Type)
If $Type = "ON"
	WriteLine($FH2,$Array[0]+" "+$Array[1]+" Successful network Logon: User Name "+$Array[2]+@CRLF+"     "+"Workstation: "+$Array[12]+@CRLF)
EndIf
If $Type = "Off"
	WriteLine($FH2,$Array[0]+" "+$Array[1]+" User Logoff"+@CRLF)
EndIf
EndFunction
_________________________
Today is the tomorrow you worried about yesterday.

Top
#180709 - 2007-09-18 11:36 PM Re: File not open for writing [Re: Arend_]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
 Originally Posted By: apronk
Are you sure the $FH2 and $input variables are filled ?
Open() does not write to files without extentions btw (tried on the hosts file) Neither does FSO.

Huh?

I have no problem using the example at the top of this post to create "somefile" - only changes I made were to specify ".\" in front of the file name, and to add a Close(1) statement. Worked just fine.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#180710 - 2007-09-19 12:11 AM Re: File not open for writing [Re: Glenn Barnas]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Okay I removed the function out of the mix and changed the Select to

 Code:
Select
	Case $Array[3] = "Logon"
	WriteLine($FH2,$Array[0]+" "+$Array[1]+" Successful network Logon: User Name "+$Array[2]+@CRLF+"     "+"Workstation: "+$Array[12]+@CRLF)	
	Case $Array[4] = "Logoff"
	WriteLine($FH2,$Array[0]+" "+$Array[1]+" User Logoff"+@CRLF)
	Case 1
	$Type = "Ignore"


Still have the same results. Moved the file location to a different location (%temp%) still no effect.

Any ideas what is going on?
_________________________
Today is the tomorrow you worried about yesterday.

Top
#180711 - 2007-09-19 12:37 AM Re: File not open for writing [Re: Gargoyle]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
 Code:
$FH = FreeFileHandle()
$FH2 = FreeFileHandle()
How's that working out for you?

ISTR you cannot get a second file handle until AFTER you consume the first one with an Open().
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#180712 - 2007-09-19 12:42 AM Re: File not open for writing [Re: Les]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
THANK YOU Les -

That was the problem, as soon as I put one of them after a Open everthing worked.

Now I know
_________________________
Today is the tomorrow you worried about yesterday.

Top
#180713 - 2007-09-19 12:57 AM Re: File not open for writing [Re: Gargoyle]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
LOL
Common noobie mistake. :p
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#180714 - 2007-09-19 01:28 AM Re: File not open for writing [Re: Les]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4572
Loc: USA

 Quote:
ISTR you cannot get a second file handle until AFTER you consume the first one with an Open().


Interesting. I'm surprised I've never run across that, or ever seen someone else posting about it. Nice find Les.
_________________________
(... better days ahead)

Top
#180715 - 2007-09-19 01:39 AM Re: File not open for writing [Re: Allen]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
It has been posted once or twice... ya musta been sleeping. I think most ppl just trace it out and learn from their mistake. It's one of those slap your forehead things once you realize it. It's the neat-freaks that get bit cuz they like to pre-define all their vars in one block of code.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#180716 - 2007-09-19 03:01 AM Re: File not open for writing [Re: Les]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Yeah it's been discussed actually quite a few times as I recall. I know I answered some users about this a couple times myself.

Unless you do a lot of writing in your scripts you probably wouldn't run into it too often.

Top
Page 1 of 1 1


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

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.074 seconds in which 0.032 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