Jump to content

Getting a directory listing


Recommended Posts

Hey guys,

Just wanted to know if there is any function like the MS-DOS 'dir' command to get a listing of all the files and directories in a particular folder ? I know that I can run the DOS prompt, and then use 'dir' to output the results to a file.

I am working with a network drive and a local one for an FTP transfer situation in which I need to find if a folder on the local machine already exists on the server. In that case I have to go and download a folder created after the one I already have. It would help immensely if there is a function out there that I dont know about. Any help in this matter is greatly appreciated.

Thanks

Link to comment
Share on other sites

Thanks everyone for the link to the _FileSearch function. I just needed to list the directories in a particular folder which is located on a networked drive. I can hence not use the 'dir' command. For some weird reason, FilGetAttrib is not working like it should.

I try to look for the attribute letter "d" in the string returned by the FileGetAttrib function but it does not return anything and ends up listing all the files along with the directories. Is there a bug logged against this function ? or am I doing something horribly stupid.

Link to comment
Share on other sites

You are probably doing something strange. In fact I always used code like this (not in func usually, but it give the idea) for detecting if a file is a folder or not, even in network path (e.g. \\sever\c$\test).

Func _IsDir($sPath)
   If StringInStr(FileGetAttrib($sPath),'d') Then
      Return 1
   Else
      Return 0
   EndIf
EndFunc

Edit: _FileSeach functions are not 'just a wrapper' of FileFindFirstFile and FileFindNext file since they can go in subdirectories! It is not so banal as it seems.

If you don't think so, try to make your own.

Edit2: This is my _FileSeach func...

CODE

Func _FileSearch($sIstr, $iSF)

; $iSF can sum up.

; $iSF = 4 means don't return also folders, only files.

; $iSF = 2 means stop at the first found.

; $iSF = 1 means looking in subfolders.

; An array is returned with the full path of all files found. The pos [0] keeps the number of elements.

Local $sIstr, $iSF, $sCriteria, $sBuffer, $iH, $iH2, $sCS, $sCF, $sCF2, $sCP, $sFP, $sOutPut = '', $aNull[1]

$sCP = StringLeft($sIstr, StringInStr($sIstr, '\', 0, -1))

If $sCP = '' Then $sCP = @WorkingDir & '\'

$sCriteria = StringTrimLeft($sIstr, StringInStr($sIstr, '\', 0, -1))

If $sCriteria = '' Then $sCriteria = '*.*'

;To begin we seek in the starting path.

$sCS = FileFindFirstFile($sCP & $sCriteria)

While $sCS <> - 1

$sCF = FileFindNextFile($sCS)

If @error Then

FileClose($sCS)

ExitLoop

EndIf

If $sCF = '.' Or $sCF = '..' Then ContinueLoop

If Not (BitAND($iSF, 4) And StringInStr(FileGetAttrib($sCP & $sCF), 'd')) Then

$sOutPut = $sOutPut & $sCP & $sCF & @LF

If BitAND($iSF, 2) Then ExitLoop

EndIf

Wend

;And after, if needed, in the rest of the folders.

If (BitAND($iSF, 2) Or BitAND($iSF, 3) And $sOutPut = '') Or (BitAND($iSF, 1) And Not BitAND($iSF, 2)) Then

$sBuffer = @CR & $sCP & '*' & @LF ;The buffer is set for keeping the given path plus a *.

Do

$sCS = StringTrimLeft(StringLeft($sBuffer, StringInStr($sBuffer, @LF, 0, 1) - 1), 1) ;current search.

$sCP = StringLeft($sCS, StringInStr($sCS, '\', 0, -1)) ;Current search path.

$iH = FileFindFirstFile($sCS)

While $iH <> - 1

$sCF = FileFindNextFile($iH)

If @error Then

FileClose($iH)

ExitLoop

EndIf

If $sCF = '.' Or $sCF = '..' Then ContinueLoop

If StringInStr(FileGetAttrib($sCP & $sCF), 'd') Then

$sBuffer = @CR & $sCP & $sCF & '\*' & @LF & $sBuffer ;Every folder found is added in the begin of buffer

$sFP = $sCP & $sCF & '\' ; for future search

$iH2 = FileFindFirstFile($sFP & $sCriteria) ; and checked with the criteria.

While $iH2 <> - 1

$sCF2 = FileFindNextFile($iH2)

If @error Then

FileClose($iH2)

ExitLoop

EndIf

If $sCF2 = '.' Or $sCF2 = '..' Then ContinueLoop

If Not (BitAND($iSF, 4) And StringInStr(FileGetAttrib($sFP & $sCF2), 'd')) Then

$sOutPut = $sOutPut & $sFP & $sCF2 & @LF ;Found items are put in the Output.

If BitAND($iSF, 2) Then

FileClose($iH2)

FileClose($iH)

ExitLoop 3

EndIf

EndIf

Wend

EndIf

Wend

$sBuffer = StringReplace($sBuffer, @CR & $sCS & @LF, '')

Until $sBuffer = ''

EndIf

If $sOutPut = '' Then

$aNull[0] = 0

Return $aNull

Else

Return StringSplit(StringTrimRight($sOutPut, 1), @LF)

EndIf

EndFunc ;==>_FileSearch

Edited by ezzetabi
Link to comment
Share on other sites

I feel like an idiot upopn having found a workaround so quickly after posting my last message. I found a way to differentiate between directories and files if for some reason the 'StringInStr($text, FileGetAttrib($file, 'd'))' function thingy doesnt work.

The FileGetTime function returns an error everytime you try to get the creation date of a folder but never on a file. So I am using that instead to list out only the folders in a particular folder in my program currently. Nothing brilliant really...just a simple workaround that did it.

Just wanna say a big thank you to all the helpful replies and posts and specifically the _FileSearch function. Really pushed me in the right direction. Here is the code if anyone is interested.

Global $dir_list[100];

local $i = 0;

; Shows the filenames of all files in the current directory, note that "." and ".." are returned.

$search = FileFindFirstFile($dir & "*.*")

; Check if the search was successful

If $search = -1 Then

MsgBox(0, "Error", "No files/directories matched the search pattern")

Exit

EndIf

While 1

$file = FileFindNextFile($search)

If @error Then ExitLoop

if $file = "." OR $file = ".." then ContinueLoop

if (StringInStr($dir & "*",FileGetAttrib ($file), "D")) then

$t = FileGetTime($dir & $file, 1);

If @error Then

$dir_list[$i] = $file;

MsgBox(0, "Directory found", $file); $i = $i + 1;

EndIf

endif

WEnd

; Close the search handle

FileClose($search)

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