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

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

Related

Compare two files in two different folders and replace it by the newer

With this VBScript code I was able to copy files. If the file exists it does nothing, if not it will copy the files needed.
Dim Photo
SourceFolder = "C:\Photo1"
DistinationFolder = "C:\Photo2"
Set ObjPhoto = CreateObject("Scripting.FileSystemObject")
For Each Photo In ObjPhoto.GetFolder( SourceFolder).Files
If Not ObjPhoto.FileExists(ObjPhoto.BuildPath(DistinationFolder, Replace(Photo.Name, ".jpg", ".bmp"))) Then
photo.Copy ObjPhoto.BuildPath(DistinationFolder, Photo.Name), True
End If
Next
I want to compare the files if the source files also exists in the destination folder and replace it by the newer.
If you want to overwrite based on the last modified date, then the File object has the property you want: DateLastModified. (You can check all properties of the File object here.)
You already have access to the source file objects (your code's Photo variable) so you just need to get the target's file object.
Something like this should work:
Dim Photo
Dim targetFile, bmpTargetFilename, jpgTargetFilename
SourceFolder = "C:\Photo1"
DistinationFolder = "C:\Photo2"
Set ObjPhoto = CreateObject("Scripting.FileSystemObject")
For Each Photo In ObjPhoto.GetFolder(SourceFolder).Files
bmpTargetFilename = ObjPhoto.BuildPath(DistinationFolder, Replace(Photo.Name, ".jpg", ".bmp"))
jpgTargetFilename = ObjPhoto.BuildPath(DistinationFolder, Photo.Name)
If ObjPhoto.FileExists(bmpTargetFilename) Then
' Get the target file object
Set targetFile = ObjPhoto.GetFile(jpgTargetFilename)
' Now compare the last modified dates of both files
If Photo.DateLastModified > targetFile.DateLastModified Then
Photo.Copy jpgTargetFilename, True
End If
Else
Photo.Copy jpgTargetFilename, True
End If
Next
A couple of notes:
It seems you are checking for the existence of a .BMP file yet copying a .JPG file, so I made it explicit by using two variables.
I am also assuming you want to compare the JPG files, since those are the ones being copied.

Need to write filenames to a text file

I am very new to VB Scripting and I am looking to see if there is a way to look at a directory get the file names and then write those file names to a text file. I would think that the Path.GetFileName Method would work, but I can't seem to get it to work. Maybe I am using it the wrong way.
Here is a simple script that will echo the filenames in the "C:\Windows\" directory.
Set fs = CreateObject("Scripting.FileSystemObject")
'Log file name
Set logFile = fs.OpenTextFile("fileNameLogs.txt", 2, True)
'Directory you want listed
Set folder = fs.GetFolder("c:\windows\")
Set files = folder.Files
For Each file in files
wscript.echo file.name
logFile.writeline(file.name)
Next
logFile.close

Copy folder contents to a created .zip file: 'file not found or no read permissions'

I'm trying to create a .zip file from an existing folder using JScript and it seems that my copyHere function is not copying to the .zip folder. Instead I get a popup box titled 'Compressed (zipped) Folder Error' with the message 'file not found or no read permissions' even though I have read/write privileges on the file according to the value of my file.attributes property (32).
Here is the script I'm using:
//Get commman line arguments
var objArgs = WScript.Arguments;
var zipPath = objArgs(0);
var sourcePath = objArgs(1);
//Create empty ZIP file and open for adding
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.CreateTextFile(zipPath, true);
// Create twenty-two byte "fingerprint" for .zip
file.write("PK");
file.write(String.fromCharCode(5));
file.write(String.fromCharCode(6));
file.write('\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0');
var objShell = new ActiveXObject("shell.application");
var zipFolder = new Object;
zipFolder = objShell.NameSpace(zipPath);
sourceItems = objShell.NameSpace(sourcePath).items();
if (zipFolder != null)
{
zipFolder.CopyHere(sourceItems);
WScript.Sleep(1000);
}
Now the CopyHere function works for copying the contents of the sourcePath to a normal folder but when I try to create a .zip file and copy the contents to that, nothing happens. Any ideas on why copyHere is not copying the contents of the sourcePath to the .zip?
An Example for calling this script would be:
cscript win-zip.js C:\desired\zip\file.zip C:\path\to\source\folder
And the desired outcome would be that file.zip was created and now contains the contents of the source folder. Could this be a problem with permissions? What might cause this behavior?
Side Note, using a vbScript and the same commands I can successfully create and populate a .zip, so why doesn't it work using jscript!
Set objArgs = WScript.Arguments
ZipFile = objArgs(0)
SourceFolder = objArgs(1)
' Create empty ZIP file and open for adding
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set zip = CreateObject("Shell.Application").NameSpace(ZipFile)
' Get items in source folder
Set sourceItems = CreateObject("Shell.Application").NameSpace(SourceFolder).Items
' Add all files/directories to the .zip file
zip.CopyHere(sourceItems)
WScript.Sleep 1000 'Wait for items to be copied
Any helpful comments are greatly appreciated, thanks!
I encountered the same problem (file not found or no read permissions' even though I have read/write privileges on the file according to the value of my file.attributes property).
The problem disapeared as soon as I found and suppress a 0 length file somewhere in the directory to be copied with the copyhere method.
As Raymond said, the problem was that I had an open reference to the .zip folder in the file variable that I created (which had a lock on the folder so I could not copy any contents to it). The solution is to call
file.Close();
after writing to the file, that way we can access the file to copy contents to it :)

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

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

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

Resources