Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dir() will not move to the next file

Status
Not open for further replies.

manguilla

Programmer
Jul 20, 2004
52
US
Hello everyone. I am having a problem with the Dir command not moving through the current folder to the next file. I am looking for specific files that begin with a name and when I find them, they will be copied to another folder. After the first file is found and copied, it will not move to the next file when I call Dir(). I will go to a null value for some reason. Please help. I have pasted the code below. Thanks again everyone.

manguilla



Module Module1

Sub Main()

Dim strDir As String

Dim strSource As String

Dim strNew As String

Dim strDest As String

Dim strFile As String

Dim I As Integer

Dim currFile As String

Dim compFile As String

Dim txt As String

Dim strNum As String

Dim currNum As String

Dim finNum As Integer

Dim N As Integer

Dim pos As Integer

Dim delFlag As Integer


strDir = "C:\Chad\Test\Win2000\"

strDest = "C:\Chad\Test\Captures\"

txt = "Capture"

currFile = Dir(strDest & "*.*")


If currFile <> "" Then

Do While currFile <> ""

strNum = Mid$(currFile, 1, 2)

If IsNumeric(strNum) Then

currNum = Int(strNum)

If finNum < currNum Then

finNum = currNum

End If

End If

currFile = Dir()

Loop

End If

If finNum = 0 Then

finNum = 10

Else

finNum = finNum + 1

End If


strFile = Dir(strDir & "*.*")

Do While strFile <> ""

pos = InStr(strFile, txt)

If pos > 0 Then

I = I + 1

strSource = strDir & strFile

strNew = strDest & finNum & "capture_" & I & ".log"

If Not IsFileLocked(strSource) Then

FileCopy(strSource, strNew)

delFlag = 1

'System.IO.File.Delete(strSource)

'strSource = strNew

End If

End If

'strFile = Dir(strDir & "*.*")

strFile = Dir()

If delFlag = 1 Then

System.IO.File.Delete(strSource)

delFlag = 0

End If

Loop

End Sub
 
This code will recursively iterate through a directory and any sub-directories, and loop through all files in each directory.


Private Sub RecurseDirectories(ByVal StartDirectory As String)
Dim d As Directory
Dim SubDirs() As String
Dim DirName As String
Dim FileNames() As String
Dim FileName As String

SubDirs = System.IO.Directory.GetDirectories(StartDirectory)
FileNames = System.IO.Directory.GetFiles(StartDirectory)

If SubDirs.Length > 0 Then
For Each DirName In SubDirs
RecurseDirectories(DirName)
Next
End If

For Each FileName In FileNames
'do something with filenames here
Next

End Sub

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
I applied the FileNames = System.IO.Directory.GetFiles(StartDirectory) part to my code and it helped me loop through all the files in both directories. Thanks again.

manguilla
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top