Jump to content

FileGetShortcut($file1) doesn't always work


Recommended Posts

I'm trying to create a script that allows a user to run any file by dropping a shortcut (.lnk) to it into a predefined folder.

;-------------------------

; Shows the filenames of all files in the current directory.
 $search = FileFindFirstFile("Program1\*.lnk")  

; Check if the search was successful
 If $search = -1 Then
  MsgBox(0, "Error", "No files/directories matched the search pattern")
  Exit
 EndIf

 While 1
  $file1 = StringTrimRight(FileFindNextFile($search), 4) 
  MsgBox(4096, "File:", $file1 )
  If @error Then ExitLoop
  $details1 = FileGetShortcut($file1)
  MsgBox(4096, "Details:", $details1 )
  $path1 = $details1[0]
  MsgBox(0, "Path:", $path1 )
  If ( $var = "all" ) then
   Sleep(2000)
   Run ( $path1 )
  Else
   If ( $var = "some" ) then
    $__msgbox = MsgBox ( 4, "startup program 1", "Do you want to start" &@LF&@LF& $file1 )
    If NOT ( $__msgbox = 7 ) then
     Run ( $path1 )
     ExitLoop
    EndIf
    ExitLoop
   EndIf
  EndIf
 WEnd
; Close the search handle
 FileClose($search)

;-------------------------

The strange thing is, this works with some shortcuts, but not with others, and I have no idea why.

Depending on the shortcut I copy into the folder, either

1) everything works correctly or

2) I get the error:

Line 18 (File "<file path here.au3")

$path1 = $details1[0]
$path1 = $details1^ERROR

Error: Subscript used with non-Array variable.

I realize there may be many improvements that could be made to this code, but mostly I'd like to know why this works with some shortcuts but not with others?

TIA,

Edited by 8daysaweek.co.uk

[size=15]James :)[/size]www.8daysaweek.co.uk - A User-Focused OOo site, with Forums, Help Centre,Easy Installation CDs & OOo on USB Key

Link to comment
Share on other sites

FileGetShortcut() is failing as it needs a path to the shortcut that was searched. If I add FileChangeDir('Program1') immediately after FileFindFirstFile() then it works ok.

It is possibly a good idea to use the working directory retrieved from the shortcut to be used with the Run() function.

I would assume that the shortcuts that did work were perhaps in the script directory itself.

Link to comment
Share on other sites

Thanks for the tip MHz. Here is my correct code (that works :o):

Const $workingDir = @WorkingDir

;-------------------------

; Shows the filenames of all files in the current directory.
 $search = FileFindFirstFile("Program1\*.lnk")  
 FileChangeDir("Program1")
 
; Check if the search was successful
 If $search = -1 Then
  MsgBox(0, "Error", "No files/directories matched the search pattern")
  Exit
 EndIf

 While 1
  $file1 = StringTrimRight(FileFindNextFile($search), 4) 
;MsgBox(4096, "File:", $file1 )
  If @error Then ExitLoop
  $details1 = FileGetShortcut($file1)
;MsgBox(4096, "Details:", $details1[0] )
  $path1 = $details1[0]
;MsgBox(0, "Path:", $path1 )
  If ( $var = "all" ) then
   Sleep(2000)
   Run ( $path1 )
   FileChangeDir($WorkingDir)
   ExitLoop
  Else
   If ( $var = "some" ) then
    $__msgbox = MsgBox ( 4, "Start Program 1", "Do you want to start" &@LF&@LF& $file1 )
    If NOT ( $__msgbox = 7 ) then
     Run ( $path1 )
     FileChangeDir($WorkingDir)
     ExitLoop
    EndIf
   EndIf
  EndIf
 WEnd
; Close the search handle
 FileClose($search)

I would assume that the shortcuts that did work were perhaps in the script directory itself.

No, that's what is strange B) different shortcuts in exactly the same folder (i.e. Program1) behaved differently. Not sure why, but the above is working now, so thanks very much :graduated:

BFN,

Edited by 8daysaweek.co.uk

[size=15]James :)[/size]www.8daysaweek.co.uk - A User-Focused OOo site, with Forums, Help Centre,Easy Installation CDs & OOo on USB Key

Link to comment
Share on other sites

Hi vollyman,

It's just a code fragment, the variable was declared earlier. I am using a Beta, not sure if it's the latest... 3.1.1.79 on the help files?

Coincidentally I just finished the code today, (I spoke too soon before when I said "it works" I managed to find quite a few bugs when testing) here is some of it (it carries on in the same fashion :graduated:)...

$__msgbox = MsgBox ( 4, 'Startup Programs', 'Do you want to STOP all programs from starting?', 30 )
if ( $__msgbox = 6 ) then
 $var = "none"
Else
 $sleepA = IniRead("sleep.ini", "Prompt1", "sleep", "10000")
;MsgBox(4096, "Result", $sleepA)
 Sleep($sleepA)
 $__msgbox = MsgBox ( 4, 'Startup Some Programs', 'Do you only want to start SOME programs?', 10 )
 if ( $__msgbox = 6 ) then
  $var = "some"
 Else
  $var = "all"
  $sleepB = IniRead("sleep.ini", "Prompt2", "sleep", "10000")
 ;MsgBox(4096, "Result", $sleepB)
  Sleep($sleepB)
 EndIf
EndIf

;MsgBox(4096,"Var", $var)

If $var = "none" then
 Exit
Else

Const $workingDir = @WorkingDir

;-------------------------

; Shows the filenames of all files in the current directory.
 $search = FileFindFirstFile("P1Program1\*.lnk")  
 FileChangeDir("P1Program1")
  
; Check if the search was successful
 If $search <> -1 Then
  While 1
   $file1 = StringTrimRight(FileFindNextFile($search), 4) 
  ;MsgBox(4096, "File:", $file1 )
   If @error Then ExitLoop
   $details1 = FileGetShortcut($file1)
  ;MsgBox(4096, "Details:", $details1[0] )
   $path1 = $details1[0]
  ;MsgBox(0, "Path:", $path1 )
   If ( $var = "all" ) then
    $sleep1 = IniRead("sleep.ini", "P1Program1", "sleep", "10000")
   ;MsgBox(4096, "Result", $sleep1)
    Sleep($sleep1)
    Run ( $path1 )
    FileChangeDir($WorkingDir)
    ExitLoop
   Else
    If ( $var = "some" ) then
     $__msgbox = MsgBox ( 4, "Start Program 1", "Do you want to start" &@LF&@LF& $file1 )
     If NOT ( $__msgbox = 7 ) then
      Run ( $path1 )
     EndIf
     FileChangeDir($WorkingDir)
     ExitLoop
    EndIf
   EndIf
  WEnd
 Else
 ;MsgBox(0, "Error", "No files/directories matched the search pattern (1)")
  FileChangeDir($WorkingDir)
 ; Close the search handle
  FileClose($search)
 EndIf

;-------------------------

; Shows the filenames of all files in the current directory.
 $search = FileFindFirstFile("P1Program2\*.lnk")  
 FileChangeDir("P1Program2")
 
; Check if the search was successful
 If $search <> -1 Then
  While 1
   $file2 = StringTrimRight(FileFindNextFile($search), 4) 
   If @error Then ExitLoop
   $details2 = FileGetShortcut($file2)
   $path2 = $details2[0]
   If ( $var = "all" ) then
    $sleep2 = IniRead("sleep.ini", "P1Program2", "sleep", "10000")
    Sleep($sleep2)
    Run ( $path2 )
    FileChangeDir($WorkingDir)
    ExitLoop
   Else
    If ( $var = "some" ) then
     $__msgbox = MsgBox ( 4, "Start Program 2", "Do you want to start" &@LF&@LF& $file2 )
     If NOT ( $__msgbox = 7 ) then
      Run ( $path2 )
     EndIf
     FileChangeDir($WorkingDir)
     ExitLoop
    EndIf
   EndIf
  WEnd
 Else
 ;MsgBox(0, "Error", "No files/directories matched the search pattern (2)")
  FileChangeDir($WorkingDir)
 ; Close the search handle
  FileClose($search)
 EndIf

;-------------------------

I imagine that there might be a much "cleaner" way of doing it, but this is working for me :o ...so far B)

BFN,

[size=15]James :)[/size]www.8daysaweek.co.uk - A User-Focused OOo site, with Forums, Help Centre,Easy Installation CDs & OOo on USB Key

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...