Solved

copy file and delete file by vbscript or bat file

Posted on 2009-07-13
4
457 Views
Last Modified: 2012-05-07
Hi ,
I want a script to do
copy file a.bkf from folder a to folder b and rename file a to a_yyyymmdd.bkf
detect the files in folder b and delete the file that old than 30days.

Please write it as a vbscript or dos bat file . so it is easy to modify.
0
Comment
Question by:GordonLiq
  • 2
4 Comments
 
LVL 19

Expert Comment

by:Rob Hutchinson
ID: 24846496
This is part of a vbs subroutine that I wrote 10 years ago and I can't even remember where I got the info from, etc.

Don't ask me to explain it as I'm not a scripting expert, but thought I'd post it anyway to give you some ideas so you can write your own script.

It does not do the detecting part of it.

I included the exe, but renamed it as RF.txt.

if there is a file in the same folder called "access_file" then it renames it to
"1023-PM-07-13-09-access_file" or whatever the current date and time are.

Attribute VB_Name = "FileScan"

Public Sub Main()
 

Dim OldName, Today, filenamedatentime

Today = Now ' Assign current system date and time.

Today = Format(Today, "hmm-AM/PM-mm-dd-yy")
 

'Rename the access_file name to today's date

OldName = "access_file"

filenamedatentime = CStr(Today + "-" + OldName)

Name OldName As filenamedatentime ' Move and rename file.
 

errorfile
 

End Sub
 

Public Sub errorfile()
 

Dim OldName, Today, filenamedatentime

Today = Now ' Assign current system date and time.

Today = Format(Today, "hmm-AM/PM-mm-dd-yy")
 

On Error Resume Next

'Rename the error_file name to today's date

OldName = "error_file"

filenamedatentime = CStr(Today + "-" + OldName)

Name OldName As filenamedatentime ' Move and rename file.
 

End Sub

Open in new window

RF.txt
0
 
LVL 10

Expert Comment

by:Alan_White
ID: 24847421
I've got a couple of scripts I can mash together that will do what you need.  
Can you tell me, is it always the same source file?  ie always a.bkf or do you need it to cope with multiple source files?
0
 
LVL 10

Accepted Solution

by:
Alan_White earned 500 total points
ID: 24847626
OK, try this for size.
Hopefully it's clear what is happening but...
The constants up the top are the only bits you should need to change.
An exception to this is the format of the date, if you have US style dates then you need to use the US format line instead of the UK line.
Let me know how you get on.
Alan

Option Explicit
 

Const sSourceFolder = "C:\A"

Const sDestinationFolder = "C:\B"

Const cFileNameWithoutExtension = "a"

Const cFileNameExtensionOnly = ".bkf"

Const cKeepDays = 30
 

Dim oShell, oFSO, i

Dim oFolderInfo, oFileCol, oFile

Dim sLogFile, sFileName

sFileName = cFileNameWithoutExtension&cFileNameExtensionOnly
 

Set oShell = CreateObject("WScript.Shell")

Set oFSO = CreateObject("Scripting.FileSystemObject")
 

Dim sNewFileName

sNewFileName = cFileNameWithoutExtension&"_"&Year(Now)&Right("0"&Month(Now),2)&Right("0"&Day(Now),2)&cFileNameExtensionOnly

oFSO.CopyFile sSourceFolder&"\"&sFileName, sDestinationFolder&"\"&sNewFileName

clearLogs
 

Sub clearLogs
 

	Dim iStartOfDate, sCurrentFilename, fileYear, fileMonth, fileDay, fileDate, diff

	Set oFolderInfo = oFSO.GetFolder(sDestinationFolder)

	Set oFileCol = oFolderInfo.Files

	For Each oFile In oFileCol 'Loop through each file within the current sub folder

		sCurrentFilename = LCase(oFile.name)

		If InStr(sCurrentFilename, cFileNameWithoutExtension&"_") = 1 AND InStr(sCurrentFilename, cFileNameExtensionOnly) >0 Then  'if not a log file, skip

		

			iStartOfDate = InStr(sCurrentFilename, "_")+1

			fileYear =  Mid(sCurrentFilename,iStartOfDate,4)

			fileMonth = Mid(sCurrentFilename,iStartOfDate+4,2)

			fileDay =   Mid(sCurrentFilename,iStartOfDate+6,2)

			

			fileDate = CDate(fileDay&"/"&fileMonth&"/"&fileYear)  'UK format

			'fileDate = CDate(fileMonth&"/"&fileDay&"/"&fileYear)  'US format	

			

			diff = DateDiff("d",fileDate,Now)

			

			If diff => cKeepDays Then

				oFSO.DeleteFile sDestinationFolder&"\"&sCurrentFilename

			End If	

		End If 

	Next
 

End Sub

Open in new window

0
 

Author Closing Comment

by:GordonLiq
ID: 31603143
That works really well , thanks for help!!!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

759 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

24 Experts available now in Live!

Get 1:1 Help Now