Mecrazycoder Posted September 5, 2009 Share Posted September 5, 2009 (edited) Hi friends, I wrote a script to give the path of given file name.Here is my script $drives=DriveGetDrive("all") For $i = 1 to $drives[0] FileChangeDir($drives[$i]) $search=FileFindFirstFile("firefox.exe") if $search =-1 Then ContinueLoop Else $path=FileFindNextFile($search) ExitLoop EndIf Next MsgBox(0,"File path is",$path).But instead of returning file path,it returns file name.Whats wrong with my code. Edited September 5, 2009 by Mecrazycoder [size="4"][font="Arial Narrow"][font="Garamond"]Attitude is a little thing that makes a big difference[/font][/font][/size][indent][/indent] Link to comment Share on other sites More sharing options...
nfaustin Posted September 5, 2009 Share Posted September 5, 2009 (edited) How about adding the drive path? $path= $drives[$i] & "\" & FileFindNextFile($search) ExitLoop Edited September 5, 2009 by nfaustin [font="Palatino Linotype"][size="2"]*** The information contained in this post should be considered and certified WORKS ON MY MACHINE ***[/size][/font][font="Palatino Linotype"][size="2"] [/size][/font] Link to comment Share on other sites More sharing options...
Mecrazycoder Posted September 5, 2009 Author Share Posted September 5, 2009 How about adding the drive path? $path= $drives[$i] & "\" & FileFindNextFile($search) ExitLoop No dude.Its not working [size="4"][font="Arial Narrow"][font="Garamond"]Attitude is a little thing that makes a big difference[/font][/font][/size][indent][/indent] Link to comment Share on other sites More sharing options...
Yashied Posted September 5, 2009 Share Posted September 5, 2009 (edited) Hi friends, I wrote a script to give the path of given file name.Here is my script $drives=DriveGetDrive("all") For $i = 1 to $drives[0] FileChangeDir($drives[$i]) $search=FileFindFirstFile("firefox.exe") if $search =-1 Then ContinueLoop Else $path=FileFindNextFile($search) ExitLoop EndIf Next MsgBox(0,"File path is",$path).But instead of returning file path,it returns file name.Whats wrong with my code. What you write can be done differently. For example, $sPath = '' $aDrives = DriveGetDrive('ALL') If IsArray($aDrives) Then For $i = 1 to $aDrives[0] If FileExists($aDrives[$i] & '\firefox.exe') Then $sPath = $aDrives[$i] & '\firefox.exe' ExitLoop EndIf Next EndIf MsgBox(0, 'File path is', $sPath) Your script looks for the file "firefox.exe" only in the root of each detected device. I think that you would like something else, dude. Edited September 5, 2009 by Yashied My UDFs: iKey | FTP Uploader | Battery Checker | Boot Manager | Font Viewer | UDF Keyword Manager | Run Dialog Replacement | USBProtect | 3D Axis | Calculator | Sleep | iSwitcher | TM | NetHelper | File Types Manager | Control Viewer | SynFolders | DLL Helper Animated Tray Icons UDF Library | Hotkeys UDF Library | Hotkeys Input Control UDF Library | Caret Shape UDF Library | Context Help UDF Library | Most Recently Used List UDF Library | Icons UDF Library | FTP UDF Library | Script Communications UDF Library | Color Chooser UDF Library | Color Picker Control UDF Library | IPHelper (Vista/7) UDF Library | WinAPI Extended UDF Library | WinAPIVhd UDF Library | Icon Chooser UDF Library | Copy UDF Library | Restart UDF Library | Event Log UDF Library | NotifyBox UDF Library | Pop-up Windows UDF Library | TVExplorer UDF Library | GuiHotKey UDF Library | GuiSysLink UDF Library | Package UDF Library | Skin UDF Library | AITray UDF Library | RDC UDF Library Appropriate path | Button text color | Gaussian random numbers | Header's styles (Vista/7) | ICON resource enumeration | Menu & INI | Tabbed string size | Tab's skin | Pop-up circular menu | Progress Bar without animation (Vista/7) | Registry export | Registry path jumping | Unique hardware ID | Windows alignment More... Link to comment Share on other sites More sharing options...
Mecrazycoder Posted September 5, 2009 Author Share Posted September 5, 2009 What you write can be done differently. For example, $sPath = '' $aDrives = DriveGetDrive('ALL') If IsArray($aDrives) Then For $i = 1 to $aDrives[0] If FileExists($aDrives[$i] & '\firefox.exe') Then $sPath = $aDrives[$i] & '\firefox.exe' ExitLoop EndIf Next EndIf MsgBox(0, 'File path is', $sPath) Your script looks for the file "firefox.exe" only in the root of each detected device. I think that you would like something else, dude. Dude,I am afraid this not i want.I want to find the full path of file specified.Anyway tanx for your response. [size="4"][font="Arial Narrow"][font="Garamond"]Attitude is a little thing that makes a big difference[/font][/font][/size][indent][/indent] Link to comment Share on other sites More sharing options...
nfaustin Posted September 5, 2009 Share Posted September 5, 2009 I think the FileFindNextFile($search) will return only the filename. In the Help file it said: Success: Returns a filename according to a previous call to FileFindFirstFile. [font="Palatino Linotype"][size="2"]*** The information contained in this post should be considered and certified WORKS ON MY MACHINE ***[/size][/font][font="Palatino Linotype"][size="2"] [/size][/font] Link to comment Share on other sites More sharing options...
Mecrazycoder Posted September 5, 2009 Author Share Posted September 5, 2009 I think the FileFindNextFile($search) will return only the filename.In the Help file it said:Success: Returns a filename according to a previous call to FileFindFirstFile.Yes you are right. I need to find the full path of the given file [size="4"][font="Arial Narrow"][font="Garamond"]Attitude is a little thing that makes a big difference[/font][/font][/size][indent][/indent] Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 5, 2009 Moderators Share Posted September 5, 2009 Mecrazycoder, I am afraid recursive file searching is a bit more complicated than you think! This script will give you the full path of a given file name: expandcollapse popup$sFile = "firefox.exe" ; or any other filename WITHOUT a path $aDrives = DriveGetDrive('ALL') If IsArray($aDrives) Then For $i = 1 to $aDrives[0] ConsoleWrite("Searching " & $aDrives[$i] & @CRLF) _RecFileFinder($aDrives[$i], $sFile) Next EndIf Func _RecFileFinder($sDrive = "", $sFile = "") Local $asFolderList[3] = [1], $sCurrentPath, $hSearch, $sName, $fFolder ; Check parameters If $sDrive = "" Or $sFile = "" Then Return SetError(1, 0, 0) ; Ensure trailing \ If StringRight($sDrive, 1) <> "\" Then $sDrive = $sDrive & "\" ; Add path to folder list $asFolderList[1] = $sDrive ; Search in listed folders While $asFolderList[0] > 0 ; Set path to search $sCurrentPath = $asFolderList[$asFolderList[0]] ConsoleWrite("Searching: " & $sCurrentPath & @CRLF) ; Reduce folder array count $asFolderList[0] -= 1 ; Get search handle $hSearch = FileFindFirstFile($sCurrentPath & "*") ; If folder empty move to next in list If $hSearch = -1 Then ContinueLoop ; Search folder While 1 $sName = FileFindNextFile($hSearch) ; Check for end of folder If @error Then ExitLoop ; Check for subfolder $fFolder = @extended ; @extended set in 3.3.1.1 + <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ;$fFolder = StringInStr(FileGetAttrib($sCurrentPath & $sName), "D") ; pre 3.3.1.1 <<<<<<<<<<<<<<<<<<<<<<<<<<<<< ; Add subfolder to folder list If $fFolder Then ; Increase folder array count $asFolderList[0] += 1 ; Double folder array size if too small (fewer ReDim needed) If UBound($asFolderList) <= $asFolderList[0] + 1 Then ReDim $asFolderList[UBound($asFolderList) * 2] ; Add subfolder to list $asFolderList[$asFolderList[0]] = $sCurrentPath & $sName & "\" EndIf ; Check filename against required value If Not $fFolder And $sName = $sFile Then ;This is where you can do what you want with the found files MsgBox(0, "Result", "Found " & $sCurrentPath & $sName) EndIf WEnd ; Close current search FileClose($hSearch) WEnd Return 1 EndFunc ;==>_RecFileFinder If you are running the Beta version of AutoIt, the script will work. If you are still using 3.3.0.0 or earlier, you need to alter it slightly - look for the <<<<<<<<<<<<<<< lines. The script tells you how the search is going in the SciTE console and pops up a messagebox when it finds a matching file. M23  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area  Link to comment Share on other sites More sharing options...
Mecrazycoder Posted September 5, 2009 Author Share Posted September 5, 2009 (edited) Mecrazycoder, I am afraid recursive file searching is a bit more complicated than you think! This script will give you the full path of a given file name: expandcollapse popup$sFile = "firefox.exe" ; or any other filename WITHOUT a path $aDrives = DriveGetDrive('ALL') If IsArray($aDrives) Then For $i = 1 to $aDrives[0] ConsoleWrite("Searching " & $aDrives[$i] & @CRLF) _RecFileFinder($aDrives[$i], $sFile) Next EndIf Func _RecFileFinder($sDrive = "", $sFile = "") Local $asFolderList[3] = [1], $sCurrentPath, $hSearch, $sName, $fFolder ; Check parameters If $sDrive = "" Or $sFile = "" Then Return SetError(1, 0, 0) ; Ensure trailing \ If StringRight($sDrive, 1) <> "\" Then $sDrive = $sDrive & "\" ; Add path to folder list $asFolderList[1] = $sDrive ; Search in listed folders While $asFolderList[0] > 0 ; Set path to search $sCurrentPath = $asFolderList[$asFolderList[0]] ConsoleWrite("Searching: " & $sCurrentPath & @CRLF) ; Reduce folder array count $asFolderList[0] -= 1 ; Get search handle $hSearch = FileFindFirstFile($sCurrentPath & "*") ; If folder empty move to next in list If $hSearch = -1 Then ContinueLoop ; Search folder While 1 $sName = FileFindNextFile($hSearch) ; Check for end of folder If @error Then ExitLoop ; Check for subfolder $fFolder = @extended ; @extended set in 3.3.1.1 + <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ;$fFolder = StringInStr(FileGetAttrib($sCurrentPath & $sName), "D") ; pre 3.3.1.1 <<<<<<<<<<<<<<<<<<<<<<<<<<<<< ; Add subfolder to folder list If $fFolder Then ; Increase folder array count $asFolderList[0] += 1 ; Double folder array size if too small (fewer ReDim needed) If UBound($asFolderList) <= $asFolderList[0] + 1 Then ReDim $asFolderList[UBound($asFolderList) * 2] ; Add subfolder to list $asFolderList[$asFolderList[0]] = $sCurrentPath & $sName & "\" EndIf ; Check filename against required value If Not $fFolder And $sName = $sFile Then ;This is where you can do what you want with the found files MsgBox(0, "Result", "Found " & $sCurrentPath & $sName) EndIf WEnd ; Close current search FileClose($hSearch) WEnd Return 1 EndFunc ;==>_RecFileFinder If you are running the Beta version of AutoIt, the script will work. If you are still using 3.3.0.0 or earlier, you need to alter it slightly - look for the <<<<<<<<<<<<<<< lines. The script tells you how the search is going in the SciTE console and pops up a messagebox when it finds a matching file. M23 Tanx for replying man.But the script you provided is not finding the location of the given file. Edited September 5, 2009 by Mecrazycoder [size="4"][font="Arial Narrow"][font="Garamond"]Attitude is a little thing that makes a big difference[/font][/font][/size][indent][/indent] Link to comment Share on other sites More sharing options...
Yashied Posted September 5, 2009 Share Posted September 5, 2009 (edited) @Mecrazycoder Melba23 is rights, you need to use a recursive calls, here's a simple example (but it may works slowly): #Include <File.au3> $sRoot = 'C:' $sFile = 'shell32.dll' MsgBox(0, '', 'Path for ' & $sFile & ' is "' & _FindFile($sRoot, $sFile) & '"') Func _FindFile($sRoot, $sFile) If FileExists($sRoot & '\' & $sFile) Then Return $sRoot EndIf Local $Result = '', $FileList = _FileListToArray($sRoot, '*', 2) If Not @error Then For $i = 1 To $FileList[0] $Result = _FindFile($sRoot & '\' & $FileList[$i], $sFile) If Not @error Then ExitLoop EndIf Next EndIf Return SetError(($Result = ''), 0, $Result) EndFunc ;==>_FindFile Edited September 5, 2009 by Yashied My UDFs: iKey | FTP Uploader | Battery Checker | Boot Manager | Font Viewer | UDF Keyword Manager | Run Dialog Replacement | USBProtect | 3D Axis | Calculator | Sleep | iSwitcher | TM | NetHelper | File Types Manager | Control Viewer | SynFolders | DLL Helper Animated Tray Icons UDF Library | Hotkeys UDF Library | Hotkeys Input Control UDF Library | Caret Shape UDF Library | Context Help UDF Library | Most Recently Used List UDF Library | Icons UDF Library | FTP UDF Library | Script Communications UDF Library | Color Chooser UDF Library | Color Picker Control UDF Library | IPHelper (Vista/7) UDF Library | WinAPI Extended UDF Library | WinAPIVhd UDF Library | Icon Chooser UDF Library | Copy UDF Library | Restart UDF Library | Event Log UDF Library | NotifyBox UDF Library | Pop-up Windows UDF Library | TVExplorer UDF Library | GuiHotKey UDF Library | GuiSysLink UDF Library | Package UDF Library | Skin UDF Library | AITray UDF Library | RDC UDF Library Appropriate path | Button text color | Gaussian random numbers | Header's styles (Vista/7) | ICON resource enumeration | Menu & INI | Tabbed string size | Tab's skin | Pop-up circular menu | Progress Bar without animation (Vista/7) | Registry export | Registry path jumping | Unique hardware ID | Windows alignment More... Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 5, 2009 Moderators Share Posted September 5, 2009 Mecrazycoder, The script does work, promise! I tested it several times and it is based on code I use every day. Did you alter the commented/live lines marked with <<<<<<<<< so only the one valid for your Autoit version was live? Is the file actually on your system? M23  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area  Link to comment Share on other sites More sharing options...
Mecrazycoder Posted September 5, 2009 Author Share Posted September 5, 2009 Mecrazycoder,The script does work, promise! I tested it several times and it is based on code I use every day.Did you alter the commented/live lines marked with <<<<<<<<< so only the one valid for your Autoit version was live?Is the file actually on your system?M23Ok dude let me check it. [size="4"][font="Arial Narrow"][font="Garamond"]Attitude is a little thing that makes a big difference[/font][/font][/size][indent][/indent] Link to comment Share on other sites More sharing options...
Mecrazycoder Posted September 5, 2009 Author Share Posted September 5, 2009 @Mecrazycoder Melba23 is rights, you need to use a recursive calls, here's a simple example (but it may works slowly): #Include <File.au3> $sRoot = 'C:' $sFile = 'shell32.dll' MsgBox(0, '', 'Path for ' & $sFile & ' is "' & _FindFile($sRoot, $sFile) & '"') Func _FindFile($sRoot, $sFile) If FileExists($sRoot & '\' & $sFile) Then Return $sRoot EndIf Local $Result = '', $FileList = _FileListToArray($sRoot, '*', 2) If Not @error Then For $i = 1 To $FileList[0] $Result = _FindFile($sRoot & '\' & $FileList[$i], $sFile) If Not @error Then ExitLoop EndIf Next EndIf Return SetError(($Result = ''), 0, $Result) EndFunc ;==>_FindFile Awesome dude its working.... [size="4"][font="Arial Narrow"][font="Garamond"]Attitude is a little thing that makes a big difference[/font][/font][/size][indent][/indent] Link to comment Share on other sites More sharing options...
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