Using VBA for manipulation of files

B

Bob

I wrote a .bat file to change a filename from .xls to .txt
For example 'move C:\data1.3.1.xls c:\data1310.txt' The
problem is the periods in the file name do not allow the
file to be imported into access. The period has to be
stripped away and the extension changed. Right now I have
to go in and manually and remove all the necessary
periods. My question is that is there a way to use VBA to
parse through a folder and have it rename files for me
without having to remove all periods by hand.

Thank You
 
B

Bruce C.

I'm not sure it's the periods so much as the file format...

Changing a file's name extension does not change the
file's format. Changing a file's name from .xls to .txt
does not make it a text formatted file. Could it be the
reason Access is having trouble is that the file is not
formatted as text?

Why are you changing the file name for importing from
Excel into Access, anyway? These two are designed to work
together. You can create a macro using the
TransferSpreadsheet method (or do the VBA thing with the
DoCmd).
 
B

Becky

This is a quick little thing that imports several files at
once. It could be easily modified to export them with a
new name, etc.

http://support.microsoft.com/default.aspx?scid=kb;en-
us;88764&Product=acc

But previous posters are right. Just changing the
extension isn't going to change the file type. Might be
some online DOS sources if you're changing the extension
to fix some kind of issue.

Becky
 
D

Dirk Goldgar

Bob said:
I wrote a .bat file to change a filename from .xls to .txt
For example 'move C:\data1.3.1.xls c:\data1310.txt' The
problem is the periods in the file name do not allow the
file to be imported into access. The period has to be
stripped away and the extension changed. Right now I have
to go in and manually and remove all the necessary
periods. My question is that is there a way to use VBA to
parse through a folder and have it rename files for me
without having to remove all periods by hand.

Setting aside the questions raised by others, you can certainly use VBA
to loop through the files in a folder and rename all or some of them.
For example,

' *** WARNING: AIR CODE ***

Dim strFolder As String
Dim strFileName As String
Dim strNewName As String

strFolder = "C:\Temp"

strFileName = Dir(strFolder & "\*.xls")

Do Until Len(strFileName) = 0

' Only rename the ones with embedded periods.
If strFileName Like "*.*.*" Then

' strip off the ".xls" to get new name
strNewName = Left(strFileName, Len(strFileName) - 4)

' remove any embedded periods (.)
strNewName = Replace(strNewName, ".", "")

' replace the ".xls" suffix.
strNewName = strNewName & ".xls"

' Rename the file.
Name strFileName As strNewName

End If

' Get next file name.
strFileName = Dir()

Loop

' --- end of code ---
 
G

Geoff

Bob - If you want code that works in Access 97
or later, does what you want, using VBA or Microsoft
Scripting Runtime, ask in this thread again.
Regards. Geoff
 
B

Bob

Let me try to clarify my issue. I need to import an
unknown number of files from an unknown location into an
access database. The code parser that generates the data
files saves them as a flat-text file, which has an .xls
extension. The Jet database engine would not allow me to
import the files using a TransferSpreadsheet command
because they are flat text file and contain no inherent
excel formatting. I could not import them as is using the
TransferText command because Jet Direct does not
consider .xls a text file. I wrote a batch file
to "move" 'c:\data\outputv3.2.xls'
to 'c:\data\outputv32.txt' then the below code goes
through the folder location and imports all of the .txt
files into the database. I have automate the below
portion and would like to automate the remaining portion
of the job. If possible I would like to know how to again
set the folder location as a variable, then the code would
go through the specified folder and take all of the .xls
files in the folder and remove any periods within the file
name and then change the extension from .xls to .txt. My
knowledge of VBA is minimal, with my only experience
coming from figuring out the below code.

Thank You

'This code is for the import button'

Private Sub Command11_Click()

Dim myfile
Dim mypath
Dim InputMsg As String
Dim InputTblName As String
Dim mytable

'The below code allows you to chose the folder where the
text files are located'
InputMsg = "Type the path of the folder that contains the
files you want to import."
mypath = InputBox(InputMsg)

'The below code allows you to set the table name for the
output'
InputTblName = "Type the name of the table you want to
create."
mytable = InputBox(InputTblName)


myfile = Dir(mypath & "*.txt")
Do While myfile <> ""
'This will import ALL the excel files (one at a time,
but automatically) in this folder.'
DoCmd.TransferText acImportDelim, "Tab_Spec", mytable,
mypath & myfile
myfile = Dir
Loop

End Sub
 
G

Geoff

Bob
This code will do it.
Set the reference to Microsoft Scripting Runtime
(as described in the function "ChangeFileNames").

Incidentally, you could open a dialog to allow the
user to select a folder, but you'd need some tricky
code.
Good luck
Geoff


*** CODE START ***

' In general declarations at top of module:
' Change constant to FALSE if using Access 97:
Const UsingAccess2000_OrLater As Boolean = True


Private Sub Command11_Click()

Dim myfile
Dim mypath
Dim InputMsg As String
Dim InputTblName As String
Dim mytable

On Error GoTo ErrorHandler

' Get folder from user:
InputMsg = "Type the path of the folder that " _
& "contains the files you want " _
& "to import."
mypath = InputBox(InputMsg)

' Get table name from user:
InputTblName = "Type the name of the table " _
& "you want to create."
mytable = InputBox(InputTblName)

' Change all files ending in ".XLS" in the
' folder "mypath" to filenames ending in ".TXT":
If Not ChangeFileNames(mypath) Then GoTo Bye

myfile = Dir(mypath & "*.txt")
Do While myfile <> ""
' This will import ALL the excel files (one at
' a time, but automatically) in this folder.'
DoCmd.TransferText acImportDelim, _
"Tab_Spec", mytable, mypath & myfile
myfile = Dir
Loop

Bye:

Exit Sub

ErrorHandler:

MsgBox Err.Description, vbOKOnly, _
"Error Number: " & Err.Number
Resume Bye

End Sub

Function ChangeFileNames(ByVal strPath As String) As Boolean

' For files in the folder defined by the variable strPath,
' remove periods from all filenames ending in ".XLS"
' and rename them with a ".TXT" filename extension.

' Uses Microsoft Scripting Runtime.

' To set a reference to Microsoft Scripting Runtime:
' In the VBA editor, open the TOOLS menu and
' click References. In the References dialog,
' check Microsoft Scripting Runtime.

' Out:
' Returns FALSE if path does not exist.

Dim objFSO As Scripting.FileSystemObject
Dim objFLDR As Scripting.Folder
Dim objFILES As Scripting.Files
Dim objFILE As Scripting.File

Dim strNewFileName As String
Dim strNewPathName As String
Dim strMessage As String
Dim intOptions As Integer
Dim strHeading As String

Const conDblLine As String = vbNewLine & vbNewLine

On Error GoTo ErrorHandler

' Set FALSE return:
ChangeFileNames = False

' Initialise:
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check folder exists:
If Not objFSO.FolderExists(strPath) Then GoTo FolderDoesNotExist

' Initialise:
Set objFLDR = objFSO.GetFolder(strPath)
Set objFILES = objFLDR.Files

' Loop through all files in folder and rename XLS files:
For Each objFILE In objFILES
GoSub PrepareToRenameFile
Next

' Set TRUE return:
ChangeFileNames = True

Bye:

Set objFILE = Nothing
Set objFILES = Nothing
Set objFLDR = Nothing
Set objFSO = Nothing
Exit Function

FolderDoesNotExist:

MsgBox strPath & vbNewLine _
& "The above path does not exist.", vbOKOnly, "Invalid Path"
GoTo Bye

PrepareToRenameFile:

If Not UCase(Right(objFILE.Name, 4)) = ".XLS" Then Return
strNewFileName = GetNewFileName(objFILE.Name)
strNewPathName = strPath & "\" & strNewFileName
If objFSO.FileExists(strNewPathName) Then GoTo Q_ReplaceFile
GoTo RenameOldFile

Q_ReplaceFile:

strMessage = strNewPathName & vbNewLine _
& "The above file already exists." & conDblLine _
& "Overwrite old file with new?"
intOptions = vbExclamation + vbYesNoCancel
strHeading = "File Exists"
Select Case MsgBox(strMessage, intOptions, strHeading)
Case vbYes
objFSO.DeleteFile strNewPathName
GoTo RenameOldFile
Case vbNo
Return
Case Else
GoTo Bye
End Select

RenameOldFile:

objFILE.Name = strNewFileName
Return

ErrorHandler:

MsgBox Err.Description, , "Error Number: " & Err.Number
Resume Bye

End Function

Function GetNewFileName(ByVal strOldFileName As String) As String

' Out:
' Return new filename without periods and with ".txt" filename
extension.

Dim strNewName As String
Dim strTemp() As String
Dim I As Integer
Dim lngStart As Long
Dim lngEnd As Long

#If UsingAccess2000_OrLater Then

' Use the Split function in Access 2000 or later.
strTemp = Split(strOldFileName, ".")

#Else

' Use the InStr function in Access 97.

' Initialise:
lngStart = 1
I = -1

' Get first period position:
lngEnd = InStr(lngStart, strOldFileName, ".")

' Loop through filename until no more periods:
Do Until lngEnd = 0
I = I + 1
ReDim Preserve strTemp(I)
strTemp(I) = Mid(strOldFileName, lngStart, lngEnd - lngStart)
lngStart = lngEnd + 1
lngEnd = InStr(lngStart, strOldFileName, ".")
Loop

' Put characters after last period into array:
I = I + 1
ReDim Preserve strTemp(I)
strTemp(I) = Mid(strOldFileName, lngStart)

#End If

' Put name back together again without periods:
For I = LBound(strTemp) To UBound(strTemp)
strNewName = strNewName & strTemp(I)
Next

' Remove "xls" and add ".txt"
strNewName = Left(strNewName, Len(strNewName) - 3) & ".txt"

' Return new file name:
GetNewFileName = strNewName

End Function

*** CODE END ***
 
G

Guest

Thank You,

I set the reference, but when I pasted the code in and
tried to run it. I got the following error message.


'The expression On Click you entered as the event property
setting produced the following error'

Ambigious name detected: ChangeFileNames

*The expression may not result in the name of a macro, the
name of a user defined function, or [Event Procedure].
*There may have been an error evaluating the fuction,
event or macro

Thank You
 
G

Geoff Gray

Bob,

Use Find (Ctrl-F) in the VBA editor and search for
"Function ChangeFileNames". This phrase should
only appear once in the form's module. (It might
have been pasted in twice by accident?)

If it appears more than once, you need to delete the
duplicate copies of the function. That should fix the
problem.

If the phrase does only appear once, the easiest
solution would be to rename the function and replace
all occurrences of the word "ChangeFileNames" in
the form module. To do this:

1. Open the form module in design view.
2. In the VBA editor, use Replace (Ctrl-H).
3. In the Replace dialog, in the Find What box, enter
ChangeFileNames
4. In the Replace With box, enter (say) RenameFiles
(must be one word with no spaces)
5. In the Search options, click Current Module
6. Click the Replace All button.

This will only work if the function does indeed only
appear once in the form module. If it appears twice
and you replace all occurrences, you'd be back to
square one!

Good luck.
Geoff
 
C

corey lawson

Yes, but the FileSystemObject does have nice methods to extract the path,
file name and extension, which is not hard, but kind of a pain, to do in
VBA. It also allows you to easily rename files. For one, you need to search
from the right of the file name to get the extension (for most cases), and
VBA doesn't have a RInstr function (but it's easy enough to find source for
it or do it your self).

On the plus side, you can use the RegExp engine to match file names with
much more generality than just using "*" and "?" wildcards in the Scripting
engine.
 
D

Dirk Goldgar

corey lawson said:
Yes, but the FileSystemObject does have nice methods to extract the path,
file name and extension, which is not hard, but kind of a pain, to do in
VBA. It also allows you to easily rename files. For one, you need to search
from the right of the file name to get the extension (for most cases), and
VBA doesn't have a RInstr function (but it's easy enough to find source for
it or do it your self).

Sure it does, since VB6: InStrRev()

The Split() function can also come in handy for parsing paths.
On the plus side, you can use the RegExp engine to match file names with
much more generality than just using "*" and "?" wildcards in the Scripting
engine.

I've never had occasion to do that, but I'm sure you're right.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top