Delete files from multiple folders that are 30 days old - vbscript

I have VB Script which will delete files from folder which is more than 30 days old. My issue is it will delete from only one folder. I need to add multiple paths of folder in one script so that one script can delete files from multiple folders.
Const strPath = "D:\LIMS Testing\"
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Call Search (strPath)
' Comment out below line if you'd like to use this script in windows schedule task
WScript.Echo"Done."
Sub Search(str)
Dim objFolder, objSubFolder, objFile
Set objFolder = objFSO.GetFolder(str)
For Each objFile In objFolder.Files
' Use DateLastModified for modified date of a file
If objFile.DateLastModified < (Now() - 30) Then
objFile.Delete(True)
End If
Next
For Each objSubFolder In objFolder.SubFolders
Search(objSubFolder.Path)
' Files have been deleted, now see if the folder is empty.
If (objSubFolder.Files.Count = 0) Then
objSubFolder.Delete True
End If
Next
End Sub

You can simply call Search for all the folders you want to process:
Search "D:\LIMS Testing\"
Search "D:\some other folder\"
Search "D:\yet another folder\"
If all the folders you want to process are in the same folder already (if they are all under D:\LIMS Testing\ for example), your code will work because the Search subroutine is recursive.

Related

Windows 7 won't refresh folder automaticly, made a vbs script to refresh by using Sendkey

Because win7 won't refresh my folder where i store log files, so i found a script online but it doesn't seem to work.
Sub RefreshSavedFiles()
Dim oShellObject
Set oShellObject = CreateObject("Wscript.Shell")
strFolder = "C:\Users\User\Documents\PDF files saved"
oShellObject.AppActivate strFolder
oShellObject.SendKeys "{F5}"
End Sub
i have a backup program that calls this script before it backups the file to my server.
This enumerates open folders and refreshes the two specified.
Set objShell = CreateObject("Shell.Application")
Set AllWindows = objShell.Windows
For Each window in AllWindows
If window.locationname = "C:\" or window.locationname = "website2.com" then
window.refresh2 3
End If
Next

Moving all files except specific file to folder vbscript

I'm trying to move all my files to another folder using vbscript but somehow I can't seem to get it right. I've executed my code but the filename that I don't want also moves to the folder that I've created. Can you help me with this?
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Users\Users\Desktop\Other Files\Excel
Files")
If objFolder.Name <> "TestResults.xlsx" Then
objFSO.Movefile "C:\Users\Users\Desktop\Other Files\Excel Files\*",
"C:\Users\Users\Desktop\Sample Folder"
End If
I've executed my code but the filename that I don't want also moves to the folder that I've created.
Its because you are moving all the files because you used *
There are few issues with your code like If objFolder.Name <> "TestResults.xlsx" even though your object refers to folders only.
Then you moved all the files - you gotta traverse the folder and filter out the files which are not to be moved
Try below code
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Users\Users\Desktop\Other Files\Excel Files").Files
For Each objFile In objFolder
If objFile.Name <> "TestResults.xlsx" Then
objFSO.MoveFile objFile.Path, "C:\Users\Users\Desktop\Sample Folder\"
End If
Next

get list of folders from local drive using vbscript

I want to delete all the documents(.doc) files from my computer, for that I know how to get the list of sub folders from a folder, but not how to get the list of folders from the root directory(Ex C:)
subfoldersInFolder = folder.subFolder
Gives all the subfolders of a folder. but supposedly that I want all the folders from C:,
Set colDrives = objFSO.Drives
For Each objDrive in colDrives
objDrive.subFolder //doesn't work
Next
For Each objFolder In objFSO.GetFolder("C:\").SubFolders
WScript.Echo objFolder.Path
Next
' or...
For Each objFolder In objFSO.GetDrive("C:").RootFolder.SubFolders
WScript.Echo objFolder.Path
Next
Edit: sundar nataraj Сундар has asked that I go into more detail.
This is a basic For loop that iterates the SubFolders collection. The SubFolders property is only available for a Folder object. You can get a Folder object for the root in a number of ways. Here are two examples:
Use the GetFolder() function to retrieve a root folder.
Use the RootFolder property of a Drive object.
I've added a WScript.Echo statement in each example to demonstrate the use of the objFolder variable.
The drive object doesn't have a SubFolder property. What it does have is a RootFolder property using which you can navigate to the root folder and then on using the SubFolder property of the folder object, get all the containing folders just like Mr #Bond mentioned.
However, if you would like to loop through all the folders of all the drives, this will do it
On Error Resume Next
Set FSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = FSO.Drives
For Each objDrive in colDrives
For Each objFolder In FSO.GetFolder(objDrive.RootFolder).SubFolders
For Each subfolder in objFolder.SubFolders
WScript.Echo subfolder.Size
Next
Next
You might want to skip through the folders that you are not authorized to access. Hence the first statement.

How to Create Folders and Subfolders in %AppData%

First of all thank you very much for all the help I’ve found over 3 Vbscript that has save my life during this last six months.
I’m new to Vbscripting and I’m currently working on getting a Vbscript that create folders and copy a file at the same time overwrite that folder and file if they already exist
Folder and subfolders to be created (Avaya) C:\Users\My Username\AppData\Roaming\Avaya\ Avaya\one-X Agent\2.5\
File from (Myfile.txt) C:\Myfile.txt to C:\Users\My Username\AppData\Roaming\Avaya\one-X Agent\2.5\
I get “Path not found” error, but If I leave the path till (Avaya) it creates Avaya Folder but not it’s subfolders C:\Users\My Username\AppData\Roaming\Avaya\
Here’s what I have and thank you in advance
Dim fso, vFolder
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
set objWShell = wScript.createObject("WScript.Shell")
usrName = objWShell.expandEnvironmentStrings("%USERNAME%")
Set fso = CreateObject("Scripting.FileSystemObject")
Set vFolder = fso.CreateFolder("C:\Users\" & usrName & "\AppData\Roaming\Avaya\one-X Agent\2.5\")
CreateFolderDemo = vFolder.Path
The problem is that CreateFolder does not create intermediate folders. The FSO doesn't have a method that does that. It might be easier to use mkdir like this:
Option Explicit
Dim shl
Set shl = CreateObject("WScript.Shell")
Call shl.Run("%COMSPEC% /c mkdir ""%APPDATA%\Avaya\one-X Agent\2.5""",0,true)
Some errors:
You declare fso, but you use objFso
You use %USERNAME% but you should consider %APPDATA% instead
You should use OPTION EXPLICIT to detect typos and undefined variables
You should make your code easier to read by dimming one variable at a time
CreateFolder doesn't create the entire tree, so you need to use FolderExists
For example:
Option Explicit
Dim objWShell
Set objWShell = WScript.CreateObject("WScript.Shell")
Dim appData
appData = objWShell.expandEnvironmentStrings("%APPDATA%")
Dim objFso
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
If Not objFso.FolderExists(appData + "\Avaya") Then
objFso.CreateFolder appData + "\Avaya"
End If
If Not objFso.FolderExists(appData + "\Avaya\one-X Agent") Then
objFso.CreateFolder appData + "\Avaya\one-X Agent"
End If
If Not objFso.FolderExists(appData + "\Avaya\one-X Agent\2.5") Then
objFso.CreateFolder appData + "\Avaya\one-X Agent\2.5"
End If
Lastly, it's not clear why your solution needs to be in VBScript. It appears that your requirements are creating folders and copying files, which means, batch files would probably be a far more simpler.

creating generic file in generic folder using QTP

I created a generic folder by taking the name from DataTable:
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
strdata = Datatable.Value("Column name", "Sheet")
fso.CreateFolder "C:\Documents and Settings\schoudar\Desktop\" & strdata
Till this I am successful but now I want to save a file in this folder and the folder name changes when I do no. of iterations. So I tried with CaptureBitmap file:
Browser("Browser").CaptureBitmap "C:\Documents and Settings\Desktop\ & strdata\filename.bmp"
But it is not working so please help me out.
What error are you getting?
Just a guess, fix the quotes and string appends:
Browser("Browser").CaptureBitmap "C:\Documents and Settings\Desktop\" & strdata & "\filename.bmp"

Resources