?
Solved

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

Posted on 2006-11-27
10
Medium Priority
?
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

771 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