View Full Version : VBS Scripts

03-03-2008, 02:15 PM

Trying to setup some VBS scripts to copy modified file (only one file created) within the last hour, copied to another folder.

What I have is a POS software that dumps a new backup with a new name every night (332008.bak, 432008.bak etc) into a backup folder. Every week I want to take the latest backup, copy to another folder where another program will backup to a secure website.

I have a script to backup the latest backup file and put it in a new folder say c:\temp. What I am having trouble with is script that can delete the file in c:\temp using wildcards as the file name is always different.

What I have so far.

This works fine.

VBS to move latest backup to new folder.

Dim fso, d, targetDir

d = Now
targetDir = "C:\TEMP\"

Set fso = CreateObject("Scripting.FileSystemObject")
Set workFolder = fso.GetFolder("Z:\My Documents")

For Each fileEntry in workFolder.Files
If DateDiff("n", fileEntry.DateLastModified, d) <= 60 Then
End If

VBS to delete file using wildcard. Neither work!

dim filesys, demofile
set filesys = CreateObject ("Scripting.FileSystemObject")
set demofile = filesys.CreateTextFile ("C:\TEMP"), "txt"
set demofile = filesys.GetFile("C:\TEMP"), "txt"

also this

CreateObject("Scripting.FileSystemObject").GetFile ("c:\temp\*.txt").Delete

Ideas anyone? I'm thinking I haven't got the wildcards set correct?

I know the script shows .txt files. I was testing with .txt files.

03-03-2008, 04:47 PM

Is the file you are wanting to delete the same file that you are copying in the first part of your code? If so, you can get the name of the file from that part of the code, store it in a variable and then delete that very same file in the second part of your code. To get the name use this:

MyNewTempVar = targetDir & fileEntry.Name

This is assuming the two parts of the code are run at the same time.....let me know if they aren't. Then to delete the file, do it like this:
Kill (MyNewTempVar)

HTH, Andrew

P.S. There are some handy tips here for the file system object: http://www.aivosto.com/visdev/fso.html

05-03-2008, 11:42 AM
Hi andrew
The file to be deleted will not be the same name as the file being copied. For example the file 02032008.bak (2/3/2008.bak) is in the backup folder. The new file to be copied over to the backup folder is say 30032008.bak (30/2/2008.bak) and this is the only file I want in the backup folder. The file names will always be changing but always along the same format. In fact this is one of the file names "Counter Intelligence Office-071130213002.bak".

What I was thinking was a vbs script to delete the file in the backup folder before the copy of the new file. Since this seems harder than expected to achieve I will just run a .bat file with del command - del c:\temp\*.txt that does the same job.


06-03-2008, 01:59 PM

You could use another fso for loop (before the copy loop) to delete all of the text files in the target directory using the method I outline above, e.g. (this is aircode and has not been tested) :

Dim fs, f, f1

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder("C:\TEMP")
For Each f1 In f.Files
If LCase(Right(f1.Name, 3)) = "txt" Then
Kill (f1.Name)
End If

HTH, Andrew