Extract Sorted File Names By Alephabets - windows

As you can know, you can sort files in windows explorer by their names, But it seems VBScript is not showing them in sorted way.
Here is a sample script which show file names in a folder one by one
Set objFSO = CreateObject("Scripting.FileSystemObject")
strReg = "C:\Users\Smart\Desktop\Files"
Set objFolder = objFSO.GetFolder(strReg)
Set colFiles = objFolder.Files
For Each objFile in colFiles
WScript.Echo ObjFile.Path
Next
But it seems it is showing files in random ways. How Can I edit this script to show files just as they are sorted in windows explorer?
Just note that My Files' Names are unicode

As the FileSystemObject can't sort the files (cf. the citation here), you'll have to write (not edit) your own code. Start here for a sample using an ArrayList (and a list of alternative approaches). The first link shows how to use dir /A:-D /B /O:....

Related

Delete files from multiple folders that are 30 days old

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.

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.

windows shell - how to detect the current location where script file is placed?

I am trying the windows shell file which will be inserted in the folder where it will analyze folders content.
Now i would like to know how can i detect which is the current path ? i.e. location where the vbs file is placed using FileSystemObject?
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFS = CreateObject("Scripting.FileSystemObject")
WScript.Echo objFS.GetParentFolderName(WScript.ScriptFullName)
You can get that from WScript.ScriptFullName. Just remove the filename from the end (the bit after the last backslash). I normally use JScript for scripts, but IIRC VBScript has an InStrRev function that will help you find the last backslash. Or: Create a File object for the WScript.ScriptFullName path and then use its ParentFolder property. Something like (untested):
Dim objFSO
Dim objFile
Dim objFolder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(WScript.ScriptFullName)
Set objFolder = objFile.ParentFolder
To get the full path only without the extension I use Replace(WScript.ScriptFullName, WScript.ScriptName, "") to just result in a filepath

Resources