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.
Related
Am capturing the data from the documents that are sent to the printer
I use the class "Win32_PrintJob". I only need to get the number of copies of each document that was sent to print, for this I use the property 'PagesPrinted', but when trying to get the number of copies, returns the value "0". Looking at the documentation, there is the following explanation: "This value can be 0 (zero) if the print job does not contain page delimitation information." My question is, what would this "page delimitation" be? How to get the exact number of copies?
Official Documentation: Link
My Code in VBScript:
strComputer="."
strPrintQuery="Select * from __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_PrintJob'"
Set PRINTSink=WScript.CreateObject("WBemScripting.SWbemSink","PRINTNEW_")
Set objWMI = GetObject("WinMgmts:{impersonationLevel=impersonate, (security)}!\\" & strComputer & "\")
objWMI.ExecNotificationQueryAsync PRINTSink,strPrintQuery
strPrintQuery2="Select * from __InstanceDeletionEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_PrintJob'"
Set PRINTSink2=WScript.CreateObject("WBemScripting.SWbemSink","PRINTDEL_")
Set objWMI2 = GetObject("WinMgmts:{impersonationLevel=impersonate, (security)}!\\" & strComputer & "\")
objWMI2.ExecNotificationQueryAsync PRINTSink2,strPrintQuery2
strPrintQuery3="Select * from __InstanceModificationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_PrintJob'"
Set PRINTSink3=WScript.CreateObject("WBemScripting.SWbemSink","PRINTMOD_")
Set objWMI3 = GetObject("WinMgmts:{impersonationLevel=impersonate, (security)}!\\" & strComputer & "\")
objWMI3.ExecNotificationQueryAsync PRINTSink3,strPrintQuery3
strServiceQuery="Select * from __InstanceModificationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Service'"
Set SERVICESink=WScript.CreateObject("WBemScripting.SWbemSink","SERVICEMOD_")
Set objWMI4 = GetObject("WinMgmts:{impersonationLevel=impersonate, (security)}!\\" & strComputer & "\")
objWMI4.ExecNotificationQueryAsync SERVICESink,strServiceQuery
While (True)
WScript.Sleep (500)
Wend
Sub PRINTNEW_OnObjectReady(objEvent,objContext)
WriteFile NOW & "f1 xxx " & objEvent.TargetInstance.PagesPrinted & " xxx " & objEvent.TargetInstance.DriverName & " xxx " & objEvent.TargetInstance.Owner & " xxx " & objEvent.TargetInstance.Name & " xxx " & objEvent.TargetInstance.Document
End Sub
Sub WriteFile(strText)
Dim objFSO, objFolder, objShell, objTextFile, objFile
Dim strDirectory, strFile
strDirectory = "C:
strFile = "\log.txt"
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
'WScript.Echo "Just created " & strDirectory
End If
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
'Wscript.Echo "Just created " & strDirectory & strFile
End If
set objFile = nothing
on error resume next
set objFolder = nothing
' OpenTextFile Method needs a Const value
' ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForAppending = 8
Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, True)
' Writes strText every time you run this VBScript
objTextFile.WriteLine(strText)
objTextFile.Close
End Sub
I need to find out is the path to the folder includes hidden folders or not.
Does anybody know how to do it? I have to use associators. Now my script only shows is the exact folder hidden or not and not checking the path to it.
Dim xdoc
Function CreateFolders(objFile)
Dim elem
Dim attr
Set elem = xdoc.CreateElement("Folder")
Set attr = xdoc.CreateAttribute("Description")
attr.Value = objFile.Description
elem.SetAttributeNode attr
Set CreateFolders = elem
End Function
Dim FilePath
Dim objFile
Dim root
Dim elem, elem1
Dim FilePath2
Set oFSO = CreateObject("Scripting.FileSystemObject")
If WScript.Arguments.Count > 0 Then
sFile = WScript.Arguments(0)
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.GetFile(sFile)
sModify = oFile.DateLastModified
End If
End If
FilePath2 = WScript.Arguments(0)
Set xdoc = CreateObject("MSXML2.DOMDocument.6.0")
xdoc.AppendChild xdoc.CreateProcessingInstruction("xml", "version=""1.0"" encoding=""utf-8""")
FilePath = "C:\Users\User\Downloads\My123.xml"
Set root = xdoc.CreateElement("Folders")
xdoc.AppendChild root
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("SELECT * FROM Win32_Directory WHERE Name = '" & FilePath2 & "'")
For Each objFile In colFiles
Wscript.Echo objFile.Name & " " & "Status: " & objFile.Hidden
Set elem = CreateFolders(objFile)
root.AppendChild elem
Next
xdoc.Save FilePath
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
I apologize in advance for my "newness" to vbs. I am trying to run this script to search for all pst files on my file server. At this point, I am getting this error:
searchpst.vbs(6, 26) Microsfot VBScript compilation error: Expected end of statement.
the script I am trying to run is of course named searchpst.vbs, and I know the (6, 26) is the line and charecter number of the error, but I cant seem to figure out what to do to fix it? Below is my script, and help is greatly appreciated!
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
strsql = "Select" * from CIM_DataFile Where Extension = '"pst"'"
Set fso = CreateObject("Scripting.FileSystemObject")
set wfile = fso.opentextfile("c:\test.csv",2,true)
For Each objFile in colFiles
Wfile.writeline(strComputer & " " & objFile.Drive & " " & objFile.Path & " " & objFile.FileName & "." & objFile.Extension & " " & objFile.FileSize)
I've reformatted the code for easier readability. The single apostrophe ' changes everything behind it into a comment, so it's not part of the code. So '"pst"'" isn't visible.
Actually, there are more problems than just that. That whole line is formatted incorrectly, and I think you've got a couple other lines out of order. It should look like this, I think:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
strsql = "Select * from CIM_DataFile Where Extension = 'pst'"
Set colFiles = objWMIService.ExecQuery(strsql)
Set fso = CreateObject("Scripting.FileSystemObject")
set wfile = fso.opentextfile("c:\test.csv",2,true)
For Each objFile in colFiles
Wfile.writeline(strComputer & " " & objFile.Drive & " " & objFile.Path & " " & objFile.FileName & "." & objFile.Extension & " " & objFile.FileSize)
Next
You need param list (), if you call a function to receive its return value; and _ continues a line - so change:
Set colFiles = objWMIService.ExecQuery _
strsql = "Select" * from CIM_DataFile Where Extension = '"pst"'"
to
strsql = "Select * from CIM_DataFile Where Extension = 'pst'"
Set colFiles = objWMIService.ExecQuery(strsql)
or:
Set colFiles = objWMIService.ExecQuery( _
"Select * from CIM_DataFile Where Extension = 'pst'")
After reading #Joe's (+1) answer, I tried to clean up the quoting in your SQL.
I want to copy files from one folder to another which falls between a specific date range using VBS.
for example i want to copy files from 06/11/2009 to 06/12/2010.
How can I do that in VB script.
Is WMI an option? If so, here's a sample script based on the one from the Hey, Scripting Guy! article How Can I Delete All Files Older Than a Specified Date?:
strComputer = "."
strFolder = "C:\FromFolder"
strNewFolder = "C:\ToFolder"
strDateFrom = "20090611000000.000000+00" ' 06/11/2009
strDateTo = "20100612000000.000000+00" ' 06/12/2010
Set oWMI = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colFiles = oWMI.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='" & strFolder & "'} WHERE " _
& "ResultClass = CIM_DataFile")
For Each oFile in colFiles
If oFile.CreationDate > strDateFrom And oFile.CreationDate < strDateTo Then
'WScript.Echo "Full path: " & oFile.Name
'WScript.Echo "Creation date: " & oFile.CreationDate
oFile.Copy strNewFolder & "\" & oFile.FileName & "." & oFile.Extension
oFile.Delete
End If
Next
Here's a slightly different variant where date checks are included in the WMI query:
strComputer = "."
strDateFrom = "20090611000000.000000+00" ' 06/11/2009
strDateTo = "20100612000000.000000+00" ' 06/12/2010
strNewFolder = "C:\ToFolder"
iFlags = 48
Set oWMI = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colFiles = oWMI.ExecQuery( _
"SELECT * FROM CIM_DataFile" & _
" WHERE Drive = 'C:' AND Path = '\\FromFolder\\'" & _
" AND CreationDate >= '" & strDateFrom & "'" & _
" AND CreationDate <= '" & strDateTo & "'" _
,,iFlags)
For Each oFile in colFiles
'WScript.Echo "Full path: " & oFile.Name
'WScript.Echo "Creation date: " & oFile.CreationDate
oFile.Copy strNewFolder & "\" & oFile.FileName & "." & oFile.Extension
oFile.Delete
Next
A few notes:
The script is non-recursive, that is, it only moves files from the source folder itself and not its subfolders.
Dates are specified in the UTC format. More info about this format is in the article I linked to.
WMI doesn't include methods for moving files and folders, so the script copies then deletes the files.
You can use the FileSystemObject. The following will get the date a file was created:
Dim fso, myfile, d
Set fso = CreateObject("Scripting.FileSystemObject")
Set myfile = fso.GetFile("something.dat")
d = myfile.DateCreated
MsgBox d
Read more here.
Here is an example of how to loop through the files in a given folder. For each file, you can check the date, decide whether you like it, and if so copy the file.