Scripting.FileSystemObject and ..lnk file - vbscript

I'm looping in a folder to get all files inside it
Scripting.FileSystemObject doesn't seem to see that a file named ..lnk exist
is there a way to fix that?
thanks

Looping through a folder using WMI also seems to find *.lnk files. Try this example:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService. _
ExecQuery("Select * from CIM_DataFile where Drive = 'C:' And Path = '\\Temp\\'")
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
I hope this helps.
/ Frank

it seem that I made a mistake while creating my test environment, I created a folder instead of a file.
now it's working.

Related

How to create text file and write to it in vbscript

I have the following script which locates all access files on a machine:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_DataFile Where Extension = 'mdb' OR Extension = 'ldb'")
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
I'm very amateur when it comes to vbscript. Instead of Echoing to a dialog box, how do I have the script write each line out to a text file called "Results.txt"?
Also, as a bonus, how do I include the date modified of each Access file?
This is what you are looking for. In this part: ("C:\test.txt" ,8 , True), the first parameter is the path to the file. The second parameter is the iomode option. There are three options for the second parameter, 1 means for reading, 2 means for writing, and 8 means for appending. The third parameter is a boolean, true means a new file can be created if it doesn't exist. False means a new file cannot be created.
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
Set OutPutFile = FSO.OpenTextFile("C:\test.txt" ,8 , True)
OutPutFile.WriteLine("Writing text to a file")
Set FSO= Nothing
Simple Google search like "vbscript create and write to text file" will give you ocean of information on how to tackle this. Anyway here is simplest one to give you kick start.
'~ Create a FileSystemObject
Set objFSO=CreateObject("Scripting.FileSystemObject")
'~ Provide file path
outFile="YouFolderPath\Results.txt"
'~ Setting up file to write
Set objFile = objFSO.CreateTextFile(outFile,True)
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_DataFile Where Extension = 'mdb' OR Extension = 'ldb'")
For Each obj_File in colFiles
'Wscript.Echo objFile.Name 'Commented out
'~ Write to file
objFile.WriteLine obj_File.Name
Next
'~ Close the file
objFile.Close
Use the FileSystemObject's .CreateTextFile method to create a text file. Study the documentation/sample carefully.
A CIM_DataFile has a .LastAccess property.

Vbscript rename a remote folder

I'm working on a script that renames a folder on a remote pc. But it it's not working.
Iff I execute the script nothing happens. I use a modified version of the Hey Scripting Guy blog. If I use normal pathnames (c:\data) instead of remotepath names (\\"& strcomputer &"C$\data) it works. But if i use remote pathnames nothing happens.
Do you guys know whats wrong?
strComputer = "hostname"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
("Select * From Win32_Directory Where Name = '\\\\"& strComputer &"C$\\Data'")
For Each objFolder in colFolders
strNewName = objFolder.Name & ".old"
objFolder.Rename strNewName
Next
When you connect with WMI you don't use a UNC path with Win32_Directory (since it's local to that WMI repository).
So use ("Select * From Win32_Directory Where Name = 'C:\\Data'")
You should be able to accomplish your task using the FileSystemObject...
strComputer = "hostname"
strFolderName = "\\"& strComputer &"\C$\Data"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strFolderName) Then
Set objFolder = objFSO.GetFolder(strFolderName)
strNewName = objFolder.Name & ".old"
objFolder.Name = strNewName
End If

VB script Kill process and delete folder

I am a noop when it comes to vbscript but I managed to make something. But now I am kinda stuck.
Let me try to explain what I am trying to do
I need to kill a process on a computer [That part I know and made] VBpart 1
When the process is killed I need to delete a folder with all it content [ That I managed also] VB part 2
VB part 1
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim xComputer, xProcessKill
xComputer = "."
xProcessKill = "'diamant client.exe'"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& xComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & xProcessKill)
For Each objProcess in colProcess
objProcess.Terminate()
next
VB part 2
strFolder = "\\srvad01\DFS\Sandbox$\username\sandbox\DiaSoft Framework Diamant Client 1.0 NL"
set objFSO = createobject("Scripting.FileSystemObject")
objFSO.DeleteFolder strFolder
But somehow I can't merge these 2 in 1 file While they work separate fine
What am I doing wrong?
Kind regarts Arie

Query Mapped Network Drive

This query runs fine on my local machine:
strComputer = "."
drive = "C:"
path = "\\path\\to\\local\\folder\\"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("Select * From CIM_DataFile Where Path = '"&path&"' and drive='"&drive&"'")
If colFiles.Count < 1 Then
Wscript.Echo "Folder does not exist"
Else
Wscript.Echo "Folder does exist"
End If
But when I try to query a mapped network drive, the program fails with 'Folders does not exist'. Yet I am sure it is the correct path to the file.
The only parts that change are:
drive = "Z:"
path = "\\path\\to\\mapped\\drive\\folder\\"
Any clues as to why this would not work?
Trying to map drives on a remote computer via WMI will fail, though there is a workaround. Thanks to Frank White's inspirational code, a fully fleshed process now exists to map a drive on a remote computer via WMI using a command prompt and passing explicit credentials.
https://stackoverflow.com/a/11948096/1569434
So to debug this I ran the following:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_Datafile Where Drive = 'Z:'")
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
This resulted in the error 'remote procedure call failed', which I understand means that the mapped drive does not support WMI.

Enumerate files with case sensitivity in VBScript?

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?
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
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.

Resources