Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Backup using a vbscript 1

Status
Not open for further replies.

kilander

Systems Engineer
Nov 6, 2019
3
SE
Hello Forum,

I work as a system engineer on the pharma-company astrazeneca. I am currently working on a solution for a backup script since the script in the software that moves the files has stopped working and the vendor does not have a solution. I have worked very little in Vbscript and i do not have the skills to make this script so i am asking for the forums help.

Once everyday at 16:00 (4 pm) the script needs to copy 9 files from the local computer to a network disk. The files are named:
DG(date(year,month,day ex. 191103)(.MB file,.PX file and data base file)
EG(date(year,month,day ex. 191103)(.MB file,.PX file and data base file)
SG(date(year,month,day ex. 191103)(.MB file,.PX file and data base file)

An example of the nine files names from the third of november 2019: DG191103.DB, DG191103.MB, DG191103.PX, EG191103.DB, EG191103.MB, EG191103.PX,SG191103.DB, SG191103.MB, SG191103.PX

The files to be copied everyday has to be yesterdays files, since the files that are named today's date are not completed until new files are created. So the names of the files to be moved should be like DG(date-1).MB and so on.

I am grateful for any help.

Best regards
Hugo Kilander
 
Hi Hugo,

I normally don't simply provide code as this is not the purpose of this forum. Also, such a script stopping to work might have various reasons that may not have to do with code at all, e.g. file server change (new name?), new security policies that prohibit VBScript execution, ...

That said, this should get you going:
Code:
Dim fromPath, destPath
Dim fso, foldr, allFiles, fil
dim rex
dim yesterday, datetext

fromPath="C:\myDailyFiles" 'Change to correct folder path with db, mb, and px files
destPath="X:\myBackupFolder\" 'Change to correct backup folder path, make sure to finish with backslash

set rex = CreateObject("VBScript.RegExp")
yesterday=DateAdd("d",-1,Now())
datetext=right(year(yesterday), 2) & month(yesterday) & day(yesterday)

rex.Pattern="(D|E|S)G" & datetext & "\.(DB|MB|PX)"

set fso= CreateObject("Scripting.FileSystemObject")
set foldr=fso.GetFolder(fromPath)
set allFiles=foldr.Files

for each fil in allFiles
	if rex.Test(fil.Name) then
		fso.CopyFile fil.Path, destPath & fil.Name
	end if
next

"rex" is a so-called "Regular Expression". With it you can match strings against certain patterns. Since the "yesterday" part is different everyday, it must be calculated before implementing it in the pattern.
I did a quick test locally and it did work just fine.

Hope this helps.
MakeItSo

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Hello and Thank you so much!!!!

I just placed the script on the computer and it work perfectly. You just saved one guy in my team to walk to the bottom floor in a building 500 meters away and copying 9 files everyday. I am so greatfull for your help! [thanks2]

Best regards
Hugo
 
Glad to know it worked right away. Removing unnecessary physical action is what a script is for. [bigcheeks]

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top