Jump to content

Error in FileFindNextFile


Recommended Posts

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
Link to comment
Share on other sites

7 hours ago, JohnOne said:

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?

Link to comment
Share on other sites

4 hours ago, 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

 

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

Link to comment
Share on other sites

1 minute ago, 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.

 

 

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.

Link to comment
Share on other sites

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.  

Link to comment
Share on other sites

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

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

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
Link to comment
Share on other sites

5 hours ago, 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.  

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

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