Question

I am using the following VBScript code snippet to enumerate all files in my c:\Scripts\ folder:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService. _
   ExecQuery("Select * from CIM_DataFile where Path = '\\Scripts\\'")

For Each objFile in colFiles
    Wscript.Echo objFile.Name 
Next

Unfortunately objFile.Name returns the path in all lower-case. It is important to me to retrieve the case of all file names, i.e. NewFileOne.txt, should not be returned as newfileone.txt.

Is there a way to enumerate files with case-sensitivity in VBScript?

Was it helpful?

Solution

If you use the FileSystemObject, you will get back names with the case preserved

Files Collection (MSDN)

dim objFSO, path, fldr, f, msg
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set fldr = objFSO.GetFolder("C:\Scripts")

For Each f in fldr.Files
    MsgBox f.name
Next

OTHER TIPS

Unlike the CIM_DataFile.Name property, the FileName and Extension properties are case sensitive. So, if it's necessary for you to use WMI, you can retrieve the file name and extension separately:

WScript.Echo objFile.FileName & "." & objFile.Extension

Mike's solution is better, but here's A VERY UGLY alternative:

Using the shell exec execute the following command:

dir c:\scripts /B>file.txt 

Now "file.txt" contains the file listed with proper casing.

Sorry, it's ugly but works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top