[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Copy and rename files in a folder

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

834 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