Avatar of gdunn59
gdunn59

asked on 

Renaming Files using Wildcards with VBA Code

I need to find some files in a directory and rename them.  I would like to use a wildcard.  How can this be accomplished using VBA Code?

Here is an example of the filename that needs to be renamed:

  ALL_RC_20070226064133_Query 1 with ELGDISHP.txt

Here is an example of how I would like the file renamed:

  ALL_RC.txt

Thanks,
gdunn59
Microsoft Access

Avatar of undefined
Last Comment
MNelson831
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

'I would like to use a wildcard'  
...to indicate what?
Avatar of PapaLorax
PapaLorax

IF LEFT(filename,6)="ALL_RC" then
Avatar of Leigh Purvis
Leigh Purvis
Flag of United Kingdom of Great Britain and Northern Ireland image

If I may expend - I think what Pete is asking you is:
In what way do you want to use a wildcard?  To return what file types/names?
And by what criteria do you want to rename the file?  To the first 6 characters? To the second occurance of an underscore?  You need to provide hard and fast axioms to create routines in an application.

(Alas the VBA routine DoWhateverYouThinkMakesSense isn't implemented yet :-)
Avatar of GRayL
GRayL
Flag of Canada image

What do the second, third, ... , etc. files that need to be renamed look like?  
Avatar of gdunn59
gdunn59

ASKER

Would like to use the wildcard as follows:

   original file:  ALL_RC_20070226064133_Query 1 with ELGDISHP.txt

  original file with wildcard:  ALL_RC*..txt

  and rename as follows:  ALL_RC.txt

The second and third files are the same with the exception of the beginning part of the filename (ALL_RC_).  The 2007.... represents the date and time (yyyymmddhhmmss), so this part of the filename will be different also, depending on the date/time file was processed.

Thanks,
gdunn59

Avatar of MNelson831
MNelson831
Flag of United States of America image

You may need to clarify exactly what it is that you are wanting to accomplish.  There are many ways to interact with files using file scripting objects, but I would need to clearly understand the ultimate goal in order to best provide assistance.
Avatar of GRayL
GRayL
Flag of Canada image

Given you want to replace the filename with the first six letter of the filename, is there some common substring in the part you are deleting that you can use for a wild card, ie may the end of the name - ELGDISHP?  Perhaps you could post 5 or 10 names and show us what you want them reduced to?
Avatar of gdunn59
gdunn59

ASKER

GRayL,

I already posted the examples in my last post. I show what the original filename is, how I want to use the file with a wildcard, and how I want the file to be renamed.
Avatar of PapaLorax
PapaLorax

Why does this not work?

IF LEFT(filename,6)="ALL_RC" then

It doesn't seem like you need a wildcard. What seems wierd is how are you going to handle multiple files of the same thing? Or do you just want to truncate everything to the first 6 letters?

then just use filename = left(filename,6)
Avatar of gdunn59
gdunn59

ASKER

I just want to use the first few characters.  

How would I go about incorporating this into my docmd statement below:

DoCmd.TransferText acImportDelim, "ALL_RC_Import_Spec", "All_Reason_Code", "S:\Systems Support\Systems Support\Mindi's Daily Report\Daily Reports\Log Source Files\ALL_RC_20070226064133_Query 1 with ELGDISHP.txt", True, "", 20127

thanks,
gdunn59
Avatar of MNelson831
MNelson831
Flag of United States of America image

So then you are attempting to import multiple files right?

Have a look at this:

Dim stFileName as string
Dim stFilePath as string
Dim stNewName as string

stFilePath = "S:\Systems Support\Systems Support\Mindi's Daily Report\Daily Reports\Log Source Files"

dirStart =  "\\MyServer\D$\MyFolder\InBox\"
dirEnd = "\\MyServer\D$\MyFolderArchives"

Set fs = CreateObject("Scripting.FileSystemObject")
Set oFolder = fs.GetFolder(dirStart)
Set oFiles = oFolder.Files

For each oFile in oFiles
      stFileName =  stFilePath & "\" & oFile.name
                 stNewName = left(oFile.name,6)

DoCmd.TransferText acImportDelim, "ALL_RC_Import_Spec", "All_Reason_Code", stFileName, True, "", 20127

If Not fs.FolderExists(dirEnd) then fs.CreateFolder(dirEnd)

If fs.FileExists(dirEnd & "\" & stFileName & "_" & stStamp) then fs.DeleteFile(dirEnd & "\" & stFileName  & "_" & stStamp)

If fs.FileExists(oFile.Path) then
      fs.MoveFile oFile.Path, dirEnd & "\" & stNewName & right(stFileName,4)
End If


Next
set fs = nothing
Avatar of gdunn59
gdunn59

ASKER

The answer to your question regarding importing multiple files is "yes", but each file is being imported into a different table.  So there are 3 files to be imported into 3 different tables.

Here are the 3 files:
        FILES                                                                                                  TABLE
   1.  ALL_RC_20070226064133_Query 1 with ELGDISHP.txt                  All_Reason_Code
   2.  RESOLVED_20070226064212_Query 1 with ELGRESHP.txt            Productivity___Resolved
   3.  ACTIVE_20070226064149_Query 1 with ELGDISHP.txt                   Productivity___Active



ASKER CERTIFIED SOLUTION
Avatar of GRayL
GRayL
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of MNelson831
MNelson831
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo