writerturtle Posted April 9, 2016 Posted April 9, 2016 (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. expandcollapse popup#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 April 10, 2016 by writerturtle
JohnOne Posted April 9, 2016 Posted April 9, 2016 On 4/9/2016 at 9:28 AM, writerturtle said: An @error seems to be generated Expand Expand. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
Miloud Posted April 9, 2016 Posted April 9, 2016 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
writerturtle Posted April 9, 2016 Author Posted April 9, 2016 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?
writerturtle Posted April 9, 2016 Author Posted April 9, 2016 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"
markyrocks Posted April 9, 2016 Posted April 9, 2016 (edited) @writerturtle looks like you're missing wend at the endof the code you posted. Idk if that's what causing your issue though. Edited April 9, 2016 by markyrocks Reveal hidden contents "I Believe array math to be potentially fatal, I may be dying from array math poisoning"
writerturtle Posted April 9, 2016 Author Posted April 9, 2016 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.
markyrocks Posted April 9, 2016 Posted April 9, 2016 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. writerturtle 1 Reveal hidden contents "I Believe array math to be potentially fatal, I may be dying from array math poisoning"
guinness Posted April 9, 2016 Posted April 9, 2016 Why not use the wrapper functions instead? These have been tried and tested. UDF List: Reveal hidden contents _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 parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018
writerturtle Posted April 10, 2016 Author Posted April 10, 2016 (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. expandcollapse popup#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 April 10, 2016 by writerturtle
writerturtle Posted April 10, 2016 Author Posted April 10, 2016 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,
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now