Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Copy and rename files in a folder

Posted on 2003-11-18
4
Medium Priority
?
1,119 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 2000 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

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!

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Suggested Courses

609 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