Jump to content

Recommended Posts

Posted (edited)

I have a program that will take a windows shortcut, look where it's targeting, and then find that file if it's been moved. Right now this part of the code kicks me out at soon as it gets to the while loop. An @error seems to be generated but I don't know why. 

 

#include <MsgBoxConstants.au3>
#include <File.au3>

Func ShortcutFixer($directoryToLookIn, $shortcutToRepair)


    Local $ShortcutTarget = FileGetShortcut($shortcutToRepair) ; Shortcut Target (This is the part of the .lnk that is actually broken.
    Local $shortcutDrive = "", $shortcutDir = "", $shortcutFileName = "", $shortcutExtension = "" ; Split the destination file path into its individual parts
    Local $aPathSplit = _PathSplit($ShortcutTarget[0], $shortcutDrive, $shortcutDir, $shortcutFileName, $shortcutExtension) ; Split the shortcut target into its individual parts
    Local $currentDrive = "", $currentDir = "", $currentFileName = "", $currentExtension = "" ; Declare the variables for the current drive being searched within.

    FileChangeDir($directoryToLookIn)
    ConsoleWrite("dir = " & $directoryToLookIn & @CRLF)
    $filehandle = FileFindFirstFile("*.*") ;$dir & "*.*")
    
    If $filehandle = -1 Then Return ;No file is found.
    While 1
        $file = FileFindNextFile($filehandle)
        If @error Then ExitLoop
        
        Local $bPathSplit = _PathSplit($file, $currentDrive, $currentDir, $currentFileName, $currentExtension) ; Split the path of the current file being examined.

        If $file <> '.' And $file <> '..' Then
            $att = FileGetAttrib($file) ;$dir &  $file) ; Later, this is used to check if the path is a directory instead of a file.

            If ($currentFileName & $currentExtension) = $shortcutFileName & $shortcutExtension Then ; The original file that the .lnk was targeted to has been found in this directory.

                ;<<<<< Repairs the shortcut >>>>>
                FileDelete($shortcutToRepair)
                FileCreateShortcut(@WorkingDir & "\" & $file, $shortcutToRepair)
                Exit
            EndIf

            If StringInStr($att, "D") > 0 Then ;<------ checks if file is a directory. If so, it recursively calls this again.
                ShortcutFixer($directoryToLookIn & "\" & $file, $shortcutToRepair)
                FileChangeDir($directoryToLookIn)
            EndIf
        EndIf

    WEnd

    Return

EndFunc   ;==>ShortcutFixer

 

Edited by writerturtle
Posted
  On 4/9/2016 at 10:23 AM, JohnOne said:

Expand.

Expand  

I am new to this so I don't know much more than what I've described. By using the console, I can see that the part of the code that writes error to console and then exits the loop is running when it should not be. Is there a way to see what kind of error is being generated?

Posted
  On 4/9/2016 at 12:45 PM, Miloud said:

Write this and see what happens

While 1
       @error=0 ;I think you may skip this line
       $sFileName = FileFindNextFile($hSearch)
       If @error Then
            ConsoleWrite("Error:"&@error);
            ExitLoop
       Else
            ConsoleWrite($sFileName)
       EndIf
         
    WEnd
EndFunc

 

Expand  

It says "Error: statement cannot be just an expression"

Posted
  On 4/9/2016 at 6:57 PM, markyrocks said:

@writerturtle looks like you're missing wend at the endof the code you posted.   Idk if that's what causing your issue though.

 

 

Expand  

No it's not. I just forgot to add that here. The code the much longer. What the program is doing is recursively searching through a whole directory for files that are broken, then once it finds a broken file, it recursively searches a whole new directory for each broken file's fix. The first recursive search works fine, but the second breaks the program.

Posted

From what I can tell I don't see how it's possible for filefindfirstfile () to have any kind of specific target  bc it's just finding the first file with a . where ever it searches first.  This may also be besides your issue but if there is only one file in the script directory which is probably where it looks first there may not be another file to find.  Maybe you should get a _filelisttoarray () from a directory and sort though that way.  

Posted

Why not use the wrapper functions instead? These have been tried and tested.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

I tried to remove extraneous info from the code but I see that it's causing a lot of confusion, so here is the code in full. The problem is that when I try it with a single file, it works fine, but when I start running multiple files through, it crashes.

It should open a shortcut, take the file portion out of its shortcut link, and look for that in an inputted folder and all its children. 

#include <MsgBoxConstants.au3>
#include <File.au3>

Func ShortcutFixer($directoryToLookIn, $shortcutToRepair)


    Local $ShortcutTarget = FileGetShortcut($shortcutToRepair) ; Shortcut Target (This is the part of the .lnk that is actually broken.
    Local $shortcutDrive = "", $shortcutDir = "", $shortcutFileName = "", $shortcutExtension = "" ; Split the destination file path into its individual parts
    Local $aPathSplit = _PathSplit($ShortcutTarget[0], $shortcutDrive, $shortcutDir, $shortcutFileName, $shortcutExtension) ; Split the shortcut target into its individual parts
    Local $currentDrive = "", $currentDir = "", $currentFileName = "", $currentExtension = "" ; Declare the variables for the current drive being searched within.

    FileChangeDir($directoryToLookIn)
    ConsoleWrite("dir = " & $directoryToLookIn & @CRLF)
    $filehandle = FileFindFirstFile("*.*") ;$dir & "*.*")
    
    If $filehandle = -1 Then Return ;No file is found.
    While 1
        $file = FileFindNextFile($filehandle)
        If @error Then ExitLoop
        
        Local $bPathSplit = _PathSplit($file, $currentDrive, $currentDir, $currentFileName, $currentExtension) ; Split the path of the current file being examined.

        If $file <> '.' And $file <> '..' Then
            $att = FileGetAttrib($file) ;$dir &  $file) ; Later, this is used to check if the path is a directory instead of a file.

            If ($currentFileName & $currentExtension) = $shortcutFileName & $shortcutExtension Then ; The original file that the .lnk was targeted to has been found in this directory.

                ;<<<<< Repairs the shortcut >>>>>
                FileDelete($shortcutToRepair)
                FileCreateShortcut(@WorkingDir & "\" & $file, $shortcutToRepair)
                Exit
            EndIf

            If StringInStr($att, "D") > 0 Then ;<------ checks if file is a directory. If so, it recursively calls this again.
                ShortcutFixer($directoryToLookIn & "\" & $file, $shortcutToRepair)
                FileChangeDir($directoryToLookIn)
            EndIf
        EndIf

    WEnd

    Return

EndFunc   ;==>ShortcutFixer

 

Edited by writerturtle
Posted
  On 4/9/2016 at 7:09 PM, markyrocks said:

From what I can tell I don't see how it's possible for filefindfirstfile () to have any kind of specific target  bc it's just finding the first file with a . where ever it searches first.  This may also be besides your issue but if there is only one file in the script directory which is probably where it looks first there may not be another file to find.  Maybe you should get a _filelisttoarray () from a directory and sort though that way.  

Expand  

I've included the full code below and edited the original topic as well. The full code checks for the file in other ways,

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
  • Recently Browsing   0 members

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