Solved

Copy and rename files in a folder

Posted on 2003-11-18
4
1,114 Views
Last Modified: 2010-05-01
I need a real simple program done in VBS or another MS based NON_Compiledable program that will copy all the files in a flder then add the system date to the current file name.  I have been out of programming VB or VBS for a long time so I am seeing if there is one here that people already use.  If it is done in VBS I can copy that code into the program I have and it should work there because it looks like VBS all over it.

Thanks


CFS
0
Comment
Question by:craigs052998
[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
  • 3
4 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9773332
dim fso
dim fld
dim ff

set fso= createobject("scripting.filesystemobject")
set fld =fso.getfolder("c:\temp")
for each ff  in fld.files
     name ff.name as mid(ff.name,1,len(ff.name)-4) & format(now,"mm-dd-yy") & right(ff.name),4)
next
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9773354
sorry:
Sub test()
Dim fso
Dim fld
Dim ff
Dim filename

Set fso = CreateObject("scripting.filesystemobject")
Set fld = fso.getfolder("C:\TEMP\temp")
For Each ff In fld.Files
     filename = "c:\temp\temp\" & ff.Name
     Name filename As Mid(filename, 1, Len(ff.Name) - 4) & Format(Now, "mm-dd-yy") & Right(filename, 4)
Next
End Sub
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9773371
sorry again
     Name filename As Mid(filename, 1, Len(filename) - 4) & Format(Now, "mm-dd-yy") & Right(filename, 4)
0
 
LVL 7

Accepted Solution

by:
wsteegmans earned 500 total points
ID: 9773950
Hi craigs,

Here a real VB-Script, that also processes your subfolders ...

Dim fso
Dim strCurrentDate

Set fso = CreateObject("scripting.filesystemobject")
Call BuildCurrentDate
Call CopyFilesWithDateStamp ("c:\temp\TempSource", "c:\temp\TempDestination")
Set fso = Nothing

Sub CopyFilesWithDateStamp(strSourceFolder, strDestinationFolder)

    Dim fsoFile
    Dim fsoFolder
    Dim fsoSubFolder
    Dim strFileNameFront
    Dim strFileExt
    Dim strNewFileName
   
    Set fsoFolder = fso.GetFolder(strSourceFolder)
    For Each fsoSubFolder In fsoFolder.SubFolders
        fso.CreateFolder strDestinationFolder & "\" & fsoSubFolder.Name
        CopyFilesWithDateStamp fsoSubFolder.Path, strDestinationFolder & "\" & fsoSubFolder.Name
    Next
   
    For Each fsoFile In fsoFolder.Files
        BreakFileName fsoFile.Name, strFileNameFront, strFileExt
        strNewFileName = strFileNameFront & "_" & strCurrentDate & strFileExt
        fsoFile.Copy strDestinationFolder & "\" & strNewFileName
    Next
   
    Set fsoFile = Nothing
    Set fsoFolder = Nothing
    Set fsoSubFolder = Nothing
   
End Sub

Sub BreakFileName(strFullName, strFront, strExtension)

    Dim intPos
   
    strFront = strFullName
    strExtension = ""
   
    intPos = InStrRev(strFullName, ".")
    If intPos > 0 Then
        strFront = Left(strFullName, intPos - 1)
        strExtension = Mid(strFullName, intPos)
    End If

End Sub

Sub BuildCurrentDate

    strCurrentDate = Year(date())
    If (Month(date()) < 10) Then
        strCurrentDate = strCurrentDate & "0" & Month(date())
    Else
        strCurrentDate = strCurrentDate & Month(date())
    End If
    If (Day(date()) < 10) Then
        strCurrentDate = strCurrentDate & "0" & Day(Date())
    Else
        strCurrentDate = strCurrentDate & Day(Date())
    End If

End Sub
0

Featured Post

Industry Leaders: 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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

691 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