Provide the source path - vbscript

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"

Related

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

How to overwrite a file in VBScript [duplicate]

i created a text file "list.txt" in commonapplicationdatafolder by using the following VBscript.i am displaying some
values from a variable(strlist) by writing in to textfile.
Const Value = &H23&
Const PATH = "\Cape\ibs"
Dim fso ' File System Object
Dim spFile ' Text File object to write
Dim objApplication ' Application object
Dim objFolder ' Folder object
Dim objFolderItem ' FolderItem object
Set objApplication = CreateObject("Shell.Application")
Set objFolder = objApplication.Namespace(Value)
Set objFolderItem = objFolder.Self
sname = objFolderItem.Path & PATH & "\list.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set spFile = fso.CreateTextFile(sname, True)
spoFile.WriteLine(strlist)
spoFile.Close
Here are my doubts
1> Here before creating that file i need to delete the old existing "list.txt" Because during installation
i always want to create the list file. So i want to include code that removes any existing file(any old list.txt),
before creating the latest one.Here i did the following code
If fso.FileExists(sname) Then
fso.DeleteFile sname, True
Else
Set spFile = fso.CreateTextFile(sname, True)
spoFile.WriteLine(strlist)
Set objFolderItem = Nothing
Set objFolder = Nothing
Set objApplication = Nothing
Set fso = Nothing
spoFile.Close
End If
Whats going on is it will create folder first time,next time it will delete it ,But i always want that file there(new fresh one with value from 'strlist' )
Can any one tell me the vbscript code to do that.Their i removed Else part also but only deletion going ,below things are not working means creation.
2>Here i was writing in to "list.txt" by using simply 'WriteLine' method(spoFile.WriteLine(strlist)),but i read somewhere that we need to use
'OpenTextFile'(Const ForWriting = 2) for writing,If that is the case what changes i need to do here,Is it mandatory?
you need to move your delete or not delete decision before your write decision.
If fso.FileExists(sname) Then
'you delete if you find it'
fso.DeleteFile sname, True
End If
'you always write it anyway.'
Set spoFile = fso.CreateTextFile(sname, True)
spoFile.WriteLine(strlist)
Set objFolderItem = Nothing
Set objFolder = Nothing
Set objApplication = Nothing
Set fso = Nothing
spoFile.Close
alternately to your question with Constant write values and making this a little (very little) bit faster, you might try the following:
If fso.FileExists(sname) Then
Set spoFile = fso.OpenTextFile(sname, 2, True)
Else
Set spoFile = fso.CreateTextFile(sname, True)
End If
' perform your write operations and close normally'
' copy in flash drive
Const Removable = 1
Set FSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = FSO.Drives
For Each Drive in colDrives
If Drive.DriveType = Removable then
fso.copyfile "filename.vbs" , Drive.DriveLetter&":\"
End if
Next

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.

Unzip files using wildcards

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

How to Copy a file that was read from a list

Hello guys I have an issue or issues with my code above
I'm trying to get "sExtension" to be search in a different folder other that the one I'm using to save my script since this script will be use as a Startup Script on many computers
(It works only if I run the script in the same folder "sExtension", "ExtAssign.txt" and sComputername are otherwise it wont find the path)
This is what it should do
Read a file called "ExtAssign.txt" (There is a full list of computer names in that file) and if it find the computer name on that file then it should copy a file with the with the extension number assigned to that computer name from a file server to "C:\" Drive
For this example I'm trying to do this locally, If I can make it then I'll try it from my File Server
Set objFSO = CreateObject("Scripting.FileSystemObject")
set oFso = CreateObject("Scripting.FileSystemObject")
Set objFS = CreateObject("Scripting.FileSystemObject")
Set fso = CreateObject("Scripting.FileSystemObject")
set oShell = WScript.CreateObject("WScript.Shell")
set oShellEnv = oShell.Environment("Process")
Set folder = Fso.GetFolder("C:\Users\XXXXX\Desktop\Test\Extensions\")
Set wshshell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set ObjEnv = WshShell.Environment("Process")
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
Scomputername = ObjEnv("COMPUTERNAME")
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objWShell = wScript.createObject("WScript.Shell")
Dim strFile
'File to scan
strFile = "C:\Users\XXXXX\Desktop\Test\Extensions\Extassign\ExtAssign.txt"
Dim strPattern
'Look for computer name in file
strPattern = scomputername
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
Dim strLine
'Read each line and store it in strLine
strLine = objFile.ReadLine
'If the line matches the computer name, save the line to ExtArray
If InStr(strLine,strPattern)>0 Then
Dim ExtArray
'Split the line and separate the extension
ExtArray = Split(strLine,"|", -1, 1)
Dim sExtension
'Save the extension to sExtension
sExtension=ExtArray(1)
End If
Loop
'If the sExtension is empty, computer was not found, send message and terminate script.
If sExtension="" Then
WScript.Echo "ERROR: Computer "& scomputername &" not found in Extension Assignment List, so no extension has been set. Avaya will not be launched. Please contact your IT department for assistance."
Else
'If the sExtension contains a number, Copy that file to C:\ and rename it to Config.xml
fso.CopyFile "C:\Users\XXXXX\Desktop\Test\Extensions\ "& sExtension &"", "C:\Config.xml", True
End If
at the end it if it finds the file sExtension it will rename it to Config.xml but it wont do it unless I run the script in the same folder sExtension and sComputername.
I get File not found error
Thank you in advance and Happy new year!
The culprit is most likely this line:
fso.CopyFile "C:\Users\XXXXX\Desktop\Test\Extensions\ "& sExtension &"", "C:\Config.xml", True
There is a trailing space after the last backslash in the path, so you're creating a path
C:\Users\XXXXX\Desktop\Test\Extensions\ 12345
^
when you actually want a path
C:\Users\XXXXX\Desktop\Test\Extensions\12345
On a more general note: why are you creating 7(!) FileSystemObject instances (replacing one of them three times on top of that)? And 3(!) WScript.Shell instances? You don't even use most of them, not to mention that you don't need the Shell object in the first place. You only use it for determining the computer name, which could be done just fine using the WScript.Network object (that you don't use at all).
Also, please don't ever use comments like this:
'Read each line and store it in strLine
strLine = objFile.ReadLine
It's quite obvious that you read each line and assign it to the variable strLine. Comments shouldn't rephrase what you're doing (the code already does that, at least when you're using speaking variable and function names), but why you're doing it, i.e. what the purpose of a particular code section is.
Your code could be reduced to something as simple as this:
Set fso = CreateObject("Scripting.FileSystemObject")
Set net = CreateObject("WScript.Network")
computername = net.ComputerName
foldername = "C:\Users\XXXXX\Desktop\Test\Extensions"
filename = fso.BuildPath(foldername, "Extassign\ExtAssign.txt")
Set f = fso.OpenTextFile(filename)
Do Until f.AtEndOfStream
line = f.ReadLine
If InStr(line, computername) > 0 Then
arr = Split(line, "|", -1, 1)
If UBound(arr) >= 1 Then extension = arr(1)
End If
Loop
f.Close
If IsEmpty(extension) Then
WScript.Echo "ERROR: Computer "& computername &" not found in ..."
Else
fso.CopyFile fso.BuildPath(foldername, extension), "C:\Config.xml", True
End If

Resources