Jump to content

File handle returning -1


delray
 Share

Recommended Posts

File names, dates and times are returned but $hFile (file handle) always returns -1; why?

#include <GuiConstantsEx.au3>
#include <Date.au3>
#include <WindowsConstants.au3>
; Shows the filenames of all files with .txt extention.
$search = FileFindFirstFile("C:UsersrdwrayCookies*.*")
; Check if the search was successful
If $search = -1 Then
MsgBox(0, "Error", "No files/directories matched the search pattern")
Exit
EndIf
; Get current system time:
$tSys = _Date_Time_GetLocalTime()
$sTime =  _Date_Time_SystemTimeToDateTimeStr($tSys)
While 1
$file = FileFindNextFile($search)
If @error Then ExitLoop
If StringRight($file, 4) = ".txt" then
  $hFile = FileOpen($file) ; Get file handle.
  FileClose($hFile)
  MsgBox(0, 0, "File handle: " & $file & " : " & $hFile)
  $aTime = _Date_Time_GetFileTime($hFile)
  $aDate = _Date_Time_FileTimeToStr($aTime[2])
  If $aTime <> "" Then
   MsgBox(4096, "File:", $file & ": " & $aDate)
  Else
   MsgBox(4096, "File:", "No more files")
  EndIf
EndIf
WEnd
Edited by delray
Link to comment
Share on other sites

Change your FileOpen command to this:

$hFile = FileOpen("C:Usersrdwray" & $file) ; Get file handle.

You are only opening it by the file name without the path to it, so it doesn't open the file and errors.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Change your FileOpen command to this:

$hFile = FileOpen("C:Usersusername" & $file) ; Get file handle.

You are only opening it by the file name without the path to it, so it doesn't open the file and errors.

Still getting -1:

If StringRight($file, 4) = ".txt" then
  $hFile = FileOpen("C:Usersrdwray" & $file) ; Get file handle.
  FileClose($hFile)
  MsgBox(0, 0, "File handle: " & $file & " : " & $hFile)
  $aTime = _Date_Time_GetFileTime($hFile)
  $aDate = _Date_Time_FileTimeToStr($aTime[2])
  If $aTime <> "" Then
   MsgBox(4096, "File:", $file & ": " & $aDate)
  Else
   MsgBox(4096, "File:", "No more files")
  EndIf
EndIf
Edited by delray
Link to comment
Share on other sites

Try opening something other than cookie files, because that may be your problem. Also, the folder you're looking in isn't really a folder, it's a junction point, the actual cookies are stored in C:Users<user>AppDataRoamingMicrosoftWindowsCookies.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Try opening something other than cookie files, because that may be your problem. Also, the folder you're looking in isn't really a folder, it's a junction point, the actual cookies are stored in C:Users<user>AppDataRoamingMicrosoftWindowsCookies.

2 things, I messed up when I copied and pasted your code the first time and second it is now return 2. thanks BrewManNH.

A question now: If the file name is being rerturned, why is the full path required to get the handle?

Link to comment
Share on other sites

Because only the file name is returned, you can't open a file by it's file name unless the program can find it. It can't find it if you don't tell it where it is. A file called somerandomname.txt could be anywhere on the hard drive, you have to tell the script where that is.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Because only the file name is returned, you can't open a file by it's file name unless the program can find it. It can't find it if you don't tell it where it is. A file called somerandomname.txt could be anywhere on the hard drive, you have to tell the script where that is.

ok, understand, but a second issue has occurred, the _Date_Time_GetFileTime($hFile) function is failing and returns null in $aDate = _Date_Time_FileTimeToStr($aTime[2]). Should not the file handle be more than 1 digit normally? Matter of fact, it is returning 2 for every file.
Link to comment
Share on other sites

You're closing the file after every file open so it reuses the file handle I'm guessing. Also, the _Date_Time_GetFileTime function needs an open file handle to read the information, and you don't give it one because you've closed it. Move the FileClose statement from where it is, to just before the last EndIf statement so that it's not closed until you're done using it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

You're closing the file after every file open so it reuses the file handle I'm guessing. Also, the _Date_Time_GetFileTime function needs an open file handle to read the information, and you don't give it one because you've closed it. Move the FileClose statement from where it is, to just before the last EndIf statement so that it's not closed until you're done using it.

Still not working:

$path = "C:\Users\username\Cookies\"

While 1
$file = FileFindNextFile($search)
If @error Then ExitLoop
If StringRight($file, 4) = ".txt" then
  $hFile = FileOpen($path & $file) ; Get file handle.
  MsgBox(0, 0, "File handle: " & $file & " : " & $hFile)
  $aTime = _Date_Time_GetFileTime($hFile)
  $aDate = _Date_Time_FileTimeToStr($aTime[2])
  If $aTime <> "" Then
   MsgBox(4096, "File:", $file & ": " & $aDate)
  Else
   MsgBox(4096, "File:", "No more files")
  EndIf
  FileClose($hFile)
EndIf
WEnd
Link to comment
Share on other sites

Try using FileGetTime instead of the one you're using. Reading the help file for that function it looks like it has restrictions on how it works, FileGetTime doesn't appear to have those restrictions. Please read the help file in regards to how to use FileGetTime, as it uses a file path and not a handle.

Also, please don't PM me in the future.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...