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

Search and Rename files

Status
Not open for further replies.

lilboi

Programmer
Dec 22, 2003
146
CA
Hi there,

I've search around this section for the search type I've been looking for but couldn't find it.

I know it's something 'bout using FileSystemObject but what I need is to search for files starting with ERROR__*.*
and not a *.EXE or anything.

How would I go about doing so? And any direction on renaming it as well?

Thanks!
 
To provide more detail...a program, when there's an error, would create a file ERROR__<number>.<number>

I have to search for those files, then parse each one of it and log it into the database then rename the ERROR file.

I found this but don't know how I can put it to my use.
Code:
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
FExists = fs.fileexists("ERROR__*.*")
How can I get each file name and parse each one of them?

Thanks
 
This should get you started:

Code:
Private Sub Command1_Click()
Dim MyFileName As String
Dim MyFilePath As String
Dim WildCardSearch As String
MyFilePath = "C:\"
WildCardSearch = "ERROR*.*"
MyFileName = Dir$(MyFilePath & WildCardSearch, vbNormal)
Do While MyFileName <> ""
    MsgBox MyFileName
    ' Rename File
    Name MyFilePath & MyFileName As MyFilePath & "TEST-" & MyFileName
    MyFileName = Dir
Loop
End Sub

Swi
 
Thanks for the quick response Swi. I have taken your code and mixed it with a code i found. It works now! Merci!!

Code:
Private Sub btnFiles_Click()

Dim MyFileName As String
Dim MyFilePath As String
Dim search As String

MyFilePath = "C:\MSG\"
search = "ERR_MSG"

Set fso = CreateObject("Scripting.FileSystemObject")
'Set Root = fso.Getfolder(App.Path)
Set Root = fso.Getfolder(MyFilePath)

Text1.Text = ""
 For Each ffile In Root.Files 'Root2.Files
    If InStr(ffile, search) And InStr(ffile, "FIXED_") = 0 Then
      Text1.Text = Text1.Text & ffile & vbCrLf
      MyFileName = Replace(ffile, MyFilePath, "")
      Name ffile As MyFilePath & "FIXED__" & MyFileName
    End If
  Next
End Sub
 
I am glad I was of some help. Have a good evening.

Swi
 
will this code also allow you to rename any file just by changing the 'ERROR' to what ever the file is named?

Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top