Solved

Scan folder and append .xml to all files in the folder

Posted on 2006-11-27
10
294 Views
Last Modified: 2011-09-20
Using VBA code, I would like to scan a folder and append the .xml extension to the end of every file in that folder.

What would the VBA code be????

Thanks,
gdunn59
0
Comment
Question by:gdunn59
  • 6
  • 4
10 Comments
 
LVL 34

Expert Comment

by:jefftwilley
ID: 18025048
something like this might do

Function RenameStuff()
Dim strPath As String
Dim strFile As String
Dim myFullFile As String
    strPath = "c:\output\"
    strFile = Dir(strPath, vbDirectory)
    Do While strFile <> ""
        If Right(strFile, 4) = ".snp" Then
            myFullFile = strPath & strFile
            FileCopy myFullFile, myFullFile & ".xml"
            Kill myFullFile
        End If
        strFile = Dir()
    Loop
End Function
0
 
LVL 34

Expert Comment

by:jefftwilley
ID: 18025056
Sorry, I posted too quickly...do over!! lol

Function RenameStuff()
Dim strPath As String
Dim strFile As String
Dim myFullFile As String
    strPath = "c:\output\"
    strFile = Dir(strPath, vbDirectory)
    Do While strFile <> ""
        myFullFile = strPath & strFile
        FileCopy myFullFile, myFullFile & ".xml"
        Kill myFullFile
    strFile = Dir()
    Loop
End Function

If this behaves strangely after the first one, let me know. The Dir() is kind of flaky
J
0
 
LVL 34

Expert Comment

by:jefftwilley
ID: 18025077
3rd time is a charm!!

Function RenameStuff()
    Dim strPath As String
    Dim strFile As String
    Dim myFullFile As String
    strPath = "c:\ShannonSave\"
    strFile = Dir(strPath, vbDirectory)
    Do While strFile <> ""
    If strFile <> "." And strFile <> ".." Then
        myFullFile = strPath & strFile
        FileCopy myFullFile, myFullFile & ".xml"
        Kill myFullFile
    End If
    strFile = Dir()
    Loop
End Function
0
 
LVL 34

Expert Comment

by:jefftwilley
ID: 18025079
You'll have to change the strPath to yours of course...but that should work nicely.
J
0
 

Author Comment

by:gdunn59
ID: 18025545
jefftwilley,

It did change the File Type to XML, but the extensions of the files are still showing the old extensions.

Do I need to do something different????

Thanks,

gdunn59
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 34

Accepted Solution

by:
jefftwilley earned 250 total points
ID: 18027095
Function RenameStuff()
    Dim strPath As String
    Dim strFile As String
    Dim myFullFile As String
    Dim myNewFile as string
    strPath = "c:\ShannonSave\"
    strFile = Dir(strPath, vbDirectory)
    Do While strFile <> ""
    If strFile <> "." And strFile <> ".." Then
        myFullFile = strPath & strFile
        myNewFile = left(strFile, len(strFile)-4)
        myNewFile = strPath & myNewFile & ".xml"
        FileCopy myFullFile, myNewFile
        Kill myFullFile
    End If
    strFile = Dir()
    Loop
End Function
0
 

Author Comment

by:gdunn59
ID: 18030061
The last one worked like a charm.  Thank you very much!!!!!!!!

gdunn59
0
 

Author Comment

by:gdunn59
ID: 18030578
jefftwilley,

One more thing, could you just add comments to each line, explaining what each line is doing?

Thanks,
gdunn59
0
 
LVL 34

Expert Comment

by:jefftwilley
ID: 18030718
ummm...sure

Function RenameStuff()
    Dim strPath As String
    Dim strFile As String
    Dim myFullFile As String
    Dim myNewFile as string
    strPath = "c:\ShannonSave\"                     '<------Your target directory
    strFile = Dir(strPath, vbDirectory)               '<--------this defines the file name in the directory. This is a file system object
    Do While strFile <> ""                    '<--------Cycle through all the files in the directory until there aren't any more
    If strFile <> "." And strFile <> ".." Then    '<----if the file is a root dir or a parent dir...ignore it
        myFullFile = strPath & strFile                '<-----My full file name is the path and the file name
        myNewFile = left(strFile, len(strFile)-4)  '<------My new file name needs it's extension stripped off so I take the left part of the string minus 4  ie... ".doc" 4 characters
        myNewFile = strPath & myNewFile & ".xml"   '<-----Now append the required extension to the raw file name
        FileCopy myFullFile, myNewFile                   ' <----copy old file name as new file name
        Kill myFullFile                                 '<--------delete the old file
    End If
    strFile = Dir()                                   '<-------grab the next file in the directory
    Loop
End Function
0
 

Author Comment

by:gdunn59
ID: 18031398
Thanks again, you ROCK!
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sort Time by AM and PM in query 2 20
Sub Reports 8 23
addressing a subform frim a subform inside it 8 23
Acc 2010 pause execution 10 10
Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now