Solved

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

Posted on 2006-11-27
10
295 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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
 
LVL 1

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
 
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
 
LVL 1

Author Comment

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

gdunn59
0
 
LVL 1

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
 
LVL 1

Author Comment

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

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

809 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