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
Related
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.
I'm using script which is looking for specified file in local disk. When it finds the file, it renames/removes files which are close to specified file. (I mean at the same directory, etc)
Sample code:
Sub RenameFolder( oldName, newName )
Dim filesys
Set filesys = WScript.CreateObject("Scripting.FileSystemObject")
If filesys.FolderExists( oldName ) Then
filesys.MoveFolder oldName, newName
End If
End Sub
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_DataFile Where Filename = 'myfile' and Extension = 'exe'")
For Each objFile in colFiles
RenameFolder objFile.Drive & objFile.Path & "files\test", objFile.Drive & objFile.Path & "files\test_old"
I want to add a condition, which will check if in the same directory as myfile.exe, there is another file called otherfile.exe.
If it is there - don't do anything, else - rename specified folder like in the code above.
What you are looking for is the FileExists method. Here's how I'd suggest you use it in your code:
Sub RenameFolder( oldName, newName )
Dim filesys
Set filesys = WScript.CreateObject("Scripting.FileSystemObject")
If filesys.FolderExists( oldName ) Then
filesys.MoveFolder oldName, newName
End If
End Sub
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_DataFile Where Filename = 'myfile' and Extension = 'exe'")
For Each objFile in colFiles
If Not filesys.FileExists(objFile.Drive & objFile.Path & "otherfile.exe") Then
'No else clause needed since we are checking if the file _doesn't_ exist.
RenameFolder objFile.Drive & objFile.Path & "files\test", objFile.Drive & objFile.Path & "files\test_old"
End If
Next
EDIT: Changed my example to work directly in asker's code.
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.
So I have the following scipt that I run when a user logs off using the logoff script section in Group Policy (I would like to run a check to see if it has already run before on the particular computer. If it has run before I would like the script to exit. If it hasn't then I want it to run the script and mark itself as "already been run". How can I do that?):
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer Where Network = TRUE")
For Each objPrinter in colInstalledPrinters
objPrinter.Delete_ Next
I figured it out. Here's the revised script:
Option Explicit
Dim oShell,strComputer,objWMIService,colInstalledPrinters,objPPrinter
Private Function KeyExists (keyName)
Dim bKey
On Error Resume Next
bKey = oShell.RegRead(keyName)
If TypeName (bKey) = "Empty" Then
KeyExists = False
Else
KeyExists = True
End If
End Function
Set oShell = CreateObject("Wscript.Shell")
If keyExists("HKEY_LOCAL_MACHINE\Software\CRusse\RemovePrinters") Then
wscript.quit
Else
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer Where Network = TRUE")
For Each objPrinter in colInstalledPrinters
objPrinter.Delete_
Next
oshell.RegWrite "HKEY_LOCAL_MACHINE\Software\CRusse\RemovePrinters", 1, "REG_SZ"
End If
Set oShell = Nothing
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.