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
Solved

Copy and rename files in a folder

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

856 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