Unzip files using wildcards - vbscript

I am basically trying to loop through my working folder to pick up any files that begin with 'Health' and unzip them in the same folder. The problem I have is that the code runs but I'm not getting any unzipped files nor am I getting any errors, can someone please help?
Sub Unzip()
Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("C:\Users\Jimbo\Documents\Process\_ThisWeek").Files
If LCase(fso.GetExtensionName(f)) = "zip" And Left(f.Name, 3) = "Health" Then
Unzip f.Path, "C:\Users\Jimbo\Documents\Process\_ThisWeek"
End If
'If the extraction location does not exist create it
Set fso = CreateObject("Scripting.FileSystemObject")
If NOT fso.FolderExists(f.path) Then
fso.CreateFolder(f.path)
End If
'Extract the contants of the zip file
set objShell = CreateObject("Shell.Application")
set FilesInZip = objShell.NameSpace(f.name).items
objShell.NameSpace(f.path).CopyHere(f.name)
Set fso = Nothing
Set objShell = Nothing
Next
End Sub

Related

How to rename a file and overwrite existing in VBS?

I already an existing vbs script to take one file (titled "Running_12345.xlsx") from one location and put it in the folder titled "Folder". This is an hourly file that has a long name based on what time it was run.
Now, I want to rename the file just "Running.xlsx" to remove the constantly changing file name. Initially, this code works, but for any subsequent occurence, it fails because the "Running.xlsx" file has already been renamed once and now already exists. How do I add overwrite logic to this code:
dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
set oFldr = fso.getfolder("folder")
for each ofile in oFldr.Files
if lcase(fso.GetExtensionName(ofile.Name)) = "xlsx" then
ofile.name = "Running.xlsx"
Exit for
end if
Next
Duh, just delete the file first. The code below does what I want:
dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
set oFldr = fso.getfolder("C:\Users\...\Desktop\MosaicTransforms\")
fso.DeleteFile("C:\Users\...\Desktop\MosaicTransforms\MosaicFile.xlsx")
for each ofile in oFldr.Files
if lcase(fso.GetExtensionName(ofile.Name)) = "xlsx" then
ofile.name = "MosaicFile.xlsx"
Exit for
end if
Next

Change DateLastModified property of Folder to match file within

I have a number of folders, each with files inside the folder.
The structure looks something like this:
Folder.No.1
Folder_No_2
Folder No 3
and the files within are something like:
Folder.No.1\My.Movie.1.mp4
Folder.No.1\My.Movie.1.txt
Folder_No_2\My_Movie_2.mp4
Folder_No_2\My_Movie_2.jpg
Folder_No_2\My_Movie_2.txt
Folder No 3\My Movie 3.mp4
As you can see, some folders contain . in the name, some contain _ and some contain spaces.
The one consistent factor is that each folder will always contain an .mp4 file, regardless of anything else.
Therefore, how can I change the Date Modified date/time of the folder to match that of the .avi file contained within the folder? Can I do this by copying the DateLastModified from the file inside (the child) to the parent folder using VBScript?
So far I am working on something like this:
Dim objShell, objFolder, objFile
Set objFile = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(strDir)
Set strDir = objFile.GetFolder("C:\Temp")
For Each objFile In objFolder
If UCase(objFolder.GetExtensionName(objFile.Name)) = "MP4" Then
objFolder.Items.Item(strDir).ModifyDate = DateLastModified
WScript.Echo objFolder.Name
End If
Next
but it fails when calling from command line with: cscript CopyDateToParent.vbs
Can anyone please help to correct this to make it work?
Try my code :
StrFolder="C:\Users\admin\Desktop\" 'Your folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objFSO.GetFolder(StrFolder)
Set Folder = objShell.NameSpace(StrFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
Name=objFile.Name
strExtension = LCase(objFSO.GetExtensionName(Name))
If strExtension = "mp4" Then
Set objFolderItem = Folder.ParseName(Name)
objFolderItem.ModifyDate = DateLastModified 'Example : "01/01/2008 8:00:00 AM"
End If
Next

Provide the source path

How can I take input as folder through GUI by using VBScript?
Example: I don't want to use following Window method.
Function Browse4Folder(strPrompt, intOptions, strRoot)
Dim objFolder, objFolderItem, objShell
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, strPrompt, intOptions, strRoot)
If (objFolder Is Nothing) Then
Browse4Folder = ""
Else
Set objFolderItem = objFolder.Self
Browse4Folder = objFolderItem.Path
Set objFolderItem = Nothing
Set objFolder = Nothing
End If
Set objShell = Nothing
End Function
I want to achieve the following:
I have a folder containing .h, .c, .exe, …
I have written the operation to removed unwanted files (like I required only .h file).
The end result should be only getting .h files.
All operation I have done but for statement (1), I have written the above snippet which provide the manual selection of the folder, but I want to make it automate (if I will run the script, all the operation should be complete without doing anything manually).
If I understand your question correctly, you want to remove all files from a given folder, except those with a specific extension (.h). That could be achieved with a procedure like this:
Sub DeleteExcept(path, extension)
Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder(path).Files
If LCase(fso.GetExtensionName(f)) <> LCase(extension) Then f.Delete True
Next
End Sub
DeleteExcept "C:\your\folder" "h"

Getting list of files in current directory

I'm trying to get a script to read the contents of the directory where the script file is located, then identify a couple of specific files based on partial names and zip them. But I can't get the object.Files property to work. Can someone tell me what's wrong here?
Set FSO = CreateObject("Scripting.FileSystemObject")
objFolder = FSO.GetParentFolderName(WScript.ScriptFullName)
Set allFiles = objFolder.Files
For Each objFile in allFiles
Wscript.Echo objFile.Name
Next
Your
objFolder = FSO.GetParentFolderName(WScript.ScriptFullName)
assigns a Path (String) to objFolder (type prefix fraud detected!). Use
Set objFolder = FSO.GetFolder(FSO.GetParentFolderName(WScript.ScriptFullName))
instead.

How to delete file of source after copying in vbscript?

I am developing function that copy file from Temp Folder to Drive C.
After copying, I would like to delete file in Temp Folder.
I tried following codes but can't delete file.Please explain it to me.
sample codes:
Set objFSO = CreateObject("Scripting.FileSystemObject")
File = file of Temp Folder
objFSO.CopyFile File, "C:\"
objFSO.DeleteFile(File)
OR
Set objFSO = CreateObject("Scripting.FileSystemObject")
File = file of Temp Folder
objFSO.CopyFile File, "C:\"
Set delFileName = objFSO.GetFile(File)
delFileName.Delete delFileName
Copying a file from one location to C:\ and then deleting the version in the original location is the same as moving the file, so do that instead:
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile File, "C:\"
Set objFSO = Nothing
If you really really want to do it in the way you've described then:
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
objFSO.CopyFile File, "C:\"
If Err.Number = 0 Then objFSO.DeleteFile File
On Error Goto 0
Set objFSO = Nothing
will do the trick.

Resources