Education is the most powerful weapon
which you can use to change the world.
-Nelson Mandela
 
Home   News   Courses   Tips   Templates   Forums   Help   Order   Contact   Logon  
 
NEW: Excel Expert 11 - Learn XLOOKUP    dismiss
Tips & Tricks

New Tips Added Weekly!
Click here to get on our Mailing List

   
 
VBScript

Rename and archive a File

How to rename and archive a file.

In this tip you will learn how to Rename a file and move it to another folder to archive it.

Add this script to the folder with the file you wish to Archive

Create a folder called archive

Then amend a few variables of the below code: path.

Firstly the filename to whatever your file is called.

filename = "Report.csv"

Now the filename you wish to then call the archive and the file extenstion to match your current file.

fso.MoveFile filename, path & "archive\Report " & createdate & ".csv"

	' ---------------------------------------------------------------------
	' Rename a file and move it
	' Rename it to datetime and move to archive.
	'
	' Created by Alex Hedley
	' Date Created: 28/01/2013
	' Date Updated: ##/##/####
	'
	' Required Refs: 
	' ---------------------------------------------------------------------
	Dim fso 
	Set fso = CreateObject("Scripting.FileSystemObject") 
	 
	Dim filename
	'filename = "testfile.txt"
	filename = "Report.csv"
	 
	Dim path
	path = "ADD YOUR PATH HERE"
	 
	Dim demofile
	Set demofile = fso.GetFile(path & filename)
	createdate = demofile.DateCreated
	 
	' Format DateTime from dd/mm/yyyy hh:nn:ss => yyyymmdd_hhnn
	'createdate = year(createdate) & month(createdate) & _
	'	day(createdate) & "_" & hour(createdate) & minute(createdate)
	
	'Build up the Date
	Dim yearNum, monthNum, dayNum, hourNum, minNum
	yearNum = year(createdate)
	monthNum = month(createdate)
	dayNum = day(createdate)
	hourNum = hour(createdate)
	minNum = minute(createdate)
	 
	' 0s are going, add them back in.
	If len(monthNum) < 2 Then monthNum = "0" & monthNum
	If len(dayNum) < 2 Then dayNum = "0" & dayNum
	If len(hourNum) < 2 Then hourNum = "0" & hourNum
	If len(minNum) < 2 Then minNum = "0" & minNum
	' Join it up
	createdate = yearNum & monthNum & dayNum & "_" & hourNum & minNum
	 
	' Add the CreateDate and move to the archive
	fso.MoveFile filename, path & "archive\Report " & createdate & ".csv"
	 
	Msgbox "Done"
	
Download Source

Source

 

Alex Hedley (Avatar) By: Alex Hedley


Click here to sign up for more FREE tips

 

You may want to read these articles from the 599CD News:

 

Learn
 
Accessindex
Excelindex
Wordindex
Windowsindex
PowerPointindex
Photoshopindex
Visual Basicindex
ASPindex
Seminars
More...
Customers
 
Account Login
Online Theater
Downloads
Lost Password
Free Upgrades
Insider Circle
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Articles
Search The Site
Waiting List
Production Schedule
Collapse Menus
Help
 
Live Chat
Customer Support
WalkThru Tutorials
Troubleshooting
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Order
 
Video Tutorials
Handbooks
MYOLP Memberships
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Gift CDs
Contact
 
Live Chat
General Info
Support Policy
Contact Form
Email Richard
Mailing Address
Phone Number
Fax Number
Course Survey
Facebook    Twitter

YouTube Channel    LinkedIn

Blog RSS Feed