Solved

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

Posted on 2006-11-27
10
300 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

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.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

687 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