Solved

Copy and rename files in a folder

Posted on 2003-11-18
4
1,109 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
  • 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now