×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

VBA code to re-name a text file, execute a macro, then repeat (for all files in a folder)

VBA code to re-name a text file, execute a macro, then repeat (for all files in a folder)

VBA code to re-name a text file, execute a macro, then repeat (for all files in a folder)

(OP)
I am very new to VBA, so please be patient. I need to to re-name .txt files in a folder to a file name that never changes (it is linked as a table in Access). Run a macro that scrapes data into another table, then repeat the process for all the files in the folder (loop). The code below renames the 1st file and runs the macro, but that is as far as I get. It does not loop correctly. Any help is greatly appreciated!

Function process()

Dim tmp As String
tmp = Dir("C:\Users\Calhoun\Documents\REPORTING\Correspondence\*.txt")
Do While tmp > ""
If Len(Dir("C:\Users\Calhoun\Documents\REPORTING\Correspondence\STATIC_FILE_NAME.txt")) <> 0 Then
Kill "C:\Users\Calhoun\Documents\REPORTING\Correspondence\STATIC_FILE_NAME.txt"
End If
Name "C:\Users\Calhoun\Documents\REPORTING\Correspondence\" & tmp As "C:\Users\Calhoun\Documents\REPORTING\Correspondence\STATIC_FILE_NAME.txt"

DoCmd.RunMacro "RunQueries"

tmp = Dir
Loop

End Function

RE: VBA code to re-name a text file, execute a macro, then repeat (for all files in a folder)

(OP)
This was resolved by removing the If Len(Dir(...)) condition. Using Dir inside the loop breaks the Dir outside the loop.
Corrected code below.

Function process()
'
Dim tmp As String
tmp = Dir("C:\Users\Calhoun\Documents\REPORTING\Correspondence\*.txt")
Do While tmp > ""
on error resume next
Kill "C:\Users\Calhoun\Documents\REPORTING\Correspondence\STATIC_FILE_NAME.txt"
On Error Goto 0
Name "C:\Users\Calhoun\Documents\REPORTING\Correspondence\" & tmp As "C:\Users\Calhoun\Documents\REPORTING\Correspondence\STATIC_FILE_NAME.txt"

DoCmd.RunMacro "RunQueries"

tmp = Dir
Loop
End Function

RE: VBA code to re-name a text file, execute a macro, then repeat (for all files in a folder)

So now it'll rename just the first .txt file it finds ...

RE: VBA code to re-name a text file, execute a macro, then repeat (for all files in a folder)

(OP)
the tmp = Dir will repeat for that specific file, and return an empty string when all files are renamed, which is your exit condition - the code loops correctly. For all file in the specified folder. smile

RE: VBA code to re-name a text file, execute a macro, then repeat (for all files in a folder)

Oops, sorry, missed the 2nd tmp=dir

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close