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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

searching a file directory 1

Status
Not open for further replies.

legos

Programmer
Jul 2, 2003
151
US
Hi all
quick question, so i hope this is easy.
Lets say i have a file folder that contains 1-10 files, but i do not know the exact amount, and i do not know the names of each individual files. Is there a way that i can rename each file in the folder and move them to a new location.

Durible Outer Casing to Prevent Fall-Apart
 
Repeated calls to the DIR() function will get you a list of the files in a folder.
There is another thread on the site at the moment that covers the use of the NAME function to rename files.

 
thanks, i already know how to rename the files, i just needed to find out how to get the names of all the files contained within the directory

Durible Outer Casing to Prevent Fall-Apart
 
Hi Legos,

You could make use of the FileSearch function, what I have done below is simply rename the files that are found in the "C:\CopyFrom\" Folder to a sequential number (ie. 1.txt, 2.txt etc....) into the "C:\CopyTo\" folder. Code is as follows:


Private Sub Main()
Dim I As Integer

With Application.FileSearch
.NewSearch
.LookIn = "C:\CopyFrom"
.SearchSubFolders = False
.FileType = msoFileTypeAllFiles
.Execute msoSortByFileName
If .FoundFiles.Count > 0 Then
For I = 1 To .FoundFiles.Count
FileCopy .FoundFiles(I), "C:\CopyTo\" & I & ".txt"
Next I
End If
End With
End Sub


Let me know if this helps.

Regards,
gkprogrammer
 
thanks alot, that is exactly how i did it.

Would anyone know how to rename files with succeeding letters instead of numbers?

ie. filea, fileb, filec, ect

Durible Outer Casing to Prevent Fall-Apart
 
You could try using an array, above you stated that there would be 1 - 10 files so I have only used 10 in my example but you could obviously expand on this it you wish:


Private Sub Main()
Dim I As Integer
Dim Placeholder() As String
Dim FolderLength As Integer

FolderLength = Len("C:\CopyFrom\")
With Application.FileSearch
.NewSearch
.LookIn = "C:\CopyFrom"
.SearchSubFolders = False
.FileType = msoFileTypeAllFiles
.Execute msoSortByFileName
If .FoundFiles.Count > 0 Then
ReDim Placeholder(.FoundFiles.Count)
For I = 1 To .FoundFiles.Count
Select Case I
Case 1
Placeholder(I) = "A"
Case 2
Placeholder(I) = "B"
Case 3
Placeholder(I) = "C"
Case 4
Placeholder(I) = "D"
Case 5
Placeholder(I) = "E"
Case 6
Placeholder(I) = "F"
Case 7
Placeholder(I) = "G"
Case 8
Placeholder(I) = "H"
Case 9
Placeholder(I) = "I"
Case 10
Placeholder(I) = "J"
End Select
Next I

For I = 1 To .FoundFiles.Count
FileCopy .FoundFiles(I), "C:\CopyTo\" & Mid$(.FoundFiles(I), FolderLength + 1, Len(.FoundFiles(I)) - (FolderLength + 4)) & Placeholder(I) & ".txt"
Next I
End If
End With
End Sub


Regards,
gkprogrammer
 
yea i was just hoping that there would be an easier way to go about it. thanks alot, that is all i need. something like
dim letter as char
letter = 'a'
for 0 to 10
(file naming script)
letter++
next

Durible Outer Casing to Prevent Fall-Apart
 
i came up with a simpler way that will do succesive letters using the chr(num) function.

num = 97
For i = 1 To .FoundFiles.count
letter = Chr(num)
path = .FoundFiles(i)
file = api & letter & ".tiff"
path2 = "C:\logs\test\" & file
Name path As path2
num = num + 1
Next i



Durible Outer Casing to Prevent Fall-Apart
 
That is a great way of doing it, very ingenious. I will have to bank this one away in the back of the brain.

Regards,
gkprogrammer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top