VBScript to Delete Files from Redundant Backup folder that aren't in the Primary Backup Folder - vbscript

Ok, so I'm really new to VBScript, so go easy on me.
I'm trying to build a VBScript that will delete files from a redundant backup folder that no longer exist in the primary backup folder. I envision something that takes each file in the redundant folder and checks to see if a file with the same name exists in the primary backup folder. If it doesn't, it then deletes the file in the redundant folder. It seems easy enough, but I think my lack of VB knowledge is holding me back.
Edit: This will run on Server 2003.
Here's what I have so far:
'variables
dim fso1
dim fso2
dim redundantFolder
dim primaryFolder
dim redundantFile
dim primaryFile
dim counter
'creates file system object
Set fso1 = CreateObject("Scripting.FileSystemObject")
set fso2 = CreateObject("Scripting.FileSystemObject")
'sets the folder object to redundant backup folder
Set redundantFolder = fso1.GetFolder("C:\Users\bmcwilliams\Desktop\Dev\TestCompleted")
'sets folder object to primary folder
Set primaryFolder = fso2.getFolder("C:\Users\bmcwilliams\Desktop\Dev\TestUnCompleted")
'deletes files that aren't found in primary folder
For Each redundantFile in redundantFolder
For Each primaryFile in primaryFolder
if primaryFile.name == redundantFile.name
flag = false
Next
I'm aware that the logic in that 'For Each' block of code is incomplete and just plain wrong, but that's as far as my small brain has allowed me to go.
Thanks in advance. Any help is greatly appreciated.
Edit: Here's the final code. I hope it will be helpful for another beginner out there:
'creates file system object
Set fso1 = CreateObject("Scripting.FileSystemObject")
Set fso2 = CreateObject("Scripting.FileSystemObject")
'sets the folder object to redundant backup folder
Set redundantFolder = fso1.GetFolder("C:\temp\redundant")
'sets folder object to primary folder
Set primaryFolder = fso2.GetFolder("C:\temp\primary")
'deletes files that aren't found in primary folder
For Each redundantFile In redundantFolder.Files
If Not fso1.FileExists(primaryFolder.Path + "\" + redundantFile.Name) Then
redundantFile.Delete
End If
Next

'creates file system object
Set fso = CreateObject("Scripting.FileSystemObject")
'sets the folder object to redundant backup folder
Set redundantFolder = fso.GetFolder("C:\temp\redundant")
'sets folder object to primary folder
Set primaryFolder = fso.GetFolder("C:\temp\primary")
'deletes files that aren't found in primary folder
For Each redundantFile In redundantFolder.Files
If Not fso.FileExists(fso.BuildPath(primaryFolder.Path,redundantFile.Name)) Then
redundantFile.Delete
End If
Next

Related

Visual basic extract folder names and compare them with active directory users

im a newbie in vb script writing and I want to extract folder names and compare them with active directory users I started with the following code:
Set fs = CreateObject("Scripting.FileSystemObject")
'Log file name
Set logFile = fs.OpenTextFile("fileNameLogs.txt", 2, True)
'Directory you want listed
Set folder = fs.GetFolder("\\server\Data\Users")
Set files = folder.files
For Each file in files
logFile.writeline(file.name)
Next
logFile.close
This script only extracts the file names not the folder names.
Can any one help me continue and extract the folder names instead of the file names so I can compare them active directory.
You are on the right track just use the .SubFolders object collection to iterate through the Folder objects in the current Folder object similar to the approach you have used for the File objects.
Option Explicit
Dim fs, logFile
Dim folder, subFolders, subFolder
Dim files, file
Set fs = CreateObject("Scripting.FileSystemObject")
'Log file name
Set logFile = fs.OpenTextFile("fileNameLogs.txt", 2, True)
'Directory you want listed
Set folder = fs.GetFolder("\\server\Data\Users")
'Write out sub folders
Set subFolders = folder.SubFolders
For Each subFolder In subFolders
logFile.Writeline(subFolder.Name)
Next
Set subFolders = Nothing
'Write out files
Set files = folder.files
For Each file In files
logFile.Writeline(file.Name)
Next
Set files = Nothing
logFile.Close
'Clean-up and reclaim memory
Set logFile = Nothing
Set folder = Nothing
Set fs = Nothing

Using VBScript to examine properties of files within a zip file

I'm trying to use VBScript to examine the contents of several hundred .zip files. Essentially what I want to do is run through each .zip and find all of the files wihtin that zip file. For each one of these files within the zip, I want to record some information about it to an Oracle database. That information being: file name and file modified date.
So far, my solution has been extracting each zips folder structure to a temp folder then running through the temp folder with an fso object. However, this has been proven to be very slow.
Is there a way to accoplish this without unziping the zip files?
Ouch man. I have never heard of vbscript zip object. But it has been a long time since I have done vbscript. Is there anyway you can avoid it?
I did some googling for you. I did find this: http://www.example-code.com/vbscript/zip_List.asp Chilkat has done a lot of stuff I thought not possible. This gives me the impression - that what you are trying to do is not going to be painless.
If given the problem you have I would find a different solution than vbscript. But if you pull-it-off I would vote for you to be mayor of vb land
You can do it in place with Shell Objects. But it will be just as slow, maybe. If just name and date Explorer may get it direct from the zip directory (at the end of the file so the whole file still needs to be read).
This copies items in a folder to another folder. A zip file is a folder so it will copy in and copy out.
To Zip
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set SrcFldr=objShell.NameSpace(Ag(1))
Set DestFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"
To Unzip (note SrcFolder and DestFolder are reversed)
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set DestFldr=objShell.NameSpace(Ag(1))
Set SrcFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"
To Create a blank zip. (I should have used an ADODB binary stream rather than an FSO text stream, but it shouldn't matter)
Set Ag=Wscript.Arguments
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(Ag(0), 8, vbtrue)
BlankZip = "PK" & Chr(5) & Chr(6)
For x = 0 to 17
BlankZip = BlankZip & Chr(0)
Next
ts.Write BlankZip

How to delete files from zip with VBScript

Im very new to VBScript (this is my first time ever using it). So far I've copied and altered my way into getting as far as I am.
I have an APK file that is too big for my needs. So what I've been doing is manually changing it to a zip and then deleting a couple images from it then renaming it back to an APK. Im trying to automate that with a VBScript. So far I have
Set oShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = WORKSPACE & "\temp\" & app & "\build\" & PLATFORMSUBFOLDER & "\dist\" & app & "\bin"
oShell.CurrentDirectory = sFolder
'Make the Apk a Zip.
fso.MoveFile apkfile, zipApk
This is all working and I can see in Windows Explorer that the APK changes to a zip like I want it to. So Im wondering if there is any quick ways to just go in and delete a couple files without extracting the whole thing?
If not is there an easy way to extract the files and parse them at the same time?
Ive looked here Extract files from ZIP file with VBScript
but cant seem to get it working. I keep getting error "Object required: 'objShell.Names(...)'" Any hints to why this is happening?
Use the MoveHere method to move an item out of the zip file:
zipfile = "C:\path\to\your.zip"
fname = "some.file"
dst = "C:\some\folder"
Set app = CreateObject("Shell.Application")
For Each f In app.NameSpace(zipfile).Items
If f.Name = fname Then
app.Namespace(dst).MoveHere(f)
End If
Next
Then delete the files from the dst folder:
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile fso.BuildPath(dst, fname), True

Move Folder into different Folder with VBS

I've been trying to make a little VBS that gets all Home Directories on a Server and moves them to a different place. Little Example
C:\homefolders\test_person
C:\homefolders\test_person\old_home
Here is what I got so far, but the moving part doesn't work...
Call ListFolderContents("C:\Windows\System32\Drivers")
Sub ListFolderContents(path)
set fs = CreateObject("Scripting.FileSystemObject")
set folder = fs.GetFolder(path)
Msgbox folder.path
For each item in folder.SubFolders
ListFolderContents(item.Path)
Next
set folder = Nothing
set fs = Nothing
End Sub
Assuming there's no problem with permissions:
set fs = CreateObject("Scripting.FileSystemObject")
set folder = fs.GetFolder(path)
folder.Move newPath
Cheers

Rename and Move Folders Using VB

I have the following script which works great if I need to rename files in a folder but now I want to move and rename folders from one mapped drive to another mapped drive. Can someone help me to modify the script to do so? I am failry new to VB so pardon if I can't firgure this out but it took me a little while to figure this one out and now I am not sure how to modify this script. Thank you in advance!
The folders by default are labeled A.1234, A.5678, and so on and will always have a different number assigned to them. I will keep the numbers in the label as they are PO numbers. So my end result desired is Ack~1234, Ack~5678, and so on.
Dim fso, f, f1, fc, s Set
fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("Y:\Test")
Set fc = f.Files
For Each f1 in fc
f1.move f1.ParentFolder & "\" & replace(f1.Name, "A.", "Ack~")
Again these folders exist on the root of a mapped drive and need to move to another mapped drive with the new names. If more info is needed please do not hesitate to ask.
UPDATE
I modified the script below to give an idea of what I'm looking to do.
Dim fso, objFol
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFol = fso.GetFolder("Z:\")
Set objFolders = objFol.Folders
For each folder in objFolders
fso.Movefolder folder, "Y:\" & Replace(fso.Name, "A.", "Ack~")
Next
This give me an error stating it does not support "Folder". There will be any number of folders in the Z drive and I need to move them all to the Y drive. Sorry if I didn't explain properly in the previous post.
The move command is taking a string, for which you're providing the parent folder of the file, so the file isn't moving. I think it'll be sufficient to provide a different folder location. For example:
Dim fso, f, f1, fc, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("Y:\Test")
Set fc = f.Files
For Each f1 in fc
f1.move "Z:\TargetFolder\" & replace(f1.Name, "A.", "Ack~")
Note: I've not tested this, but if there are problems then just ask
Additional Stuff after update:
The following code will allow you to move folders, and do the replacements you need to the folder name string.
Dim fso, objFol, objMoveFol, strPathBuild
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFol = fso.GetFolder("Y:\Test")
For Each objMoveFol In objFol.SubFolders
'Replace the root folder locations in the path
strPathBuild = Replace(objMoveFol, "Y:\Test", "Z:\TargetFolder\")
'Do the required other fiddle
strPathBuild = Replace(strPathBuild, "A.", "Ack~")
fso.Movefolder objMoveFol, strPathBuild
Next
Make sure you're really careful with this sort of thing, as getting is wrong can be fairly spectacular.

Resources