script to pull info from windows Drive - windows

I need a script that could ran and pull information from any drive on a windows operating system (Server 2003), listing all files and folders which contain the following fields:
Full file path (e.g. C:\Documents and Settings\user\My Documents\testPage.doc)
File type (e.g. word document, spreadsheet, database etc)
Size
When Created
When last modified
When last accessed
Any help is appreciated.
Thanks

try this vbscript
Set objFS=CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strFolder = objArgs(0)
Set objFolder = objFS.GetFolder(strFolder)
Go (objFolder)
Sub Go(objDIR)
If objDIR <> "\System Volume Information" Then
For Each eFolder in objDIR.SubFolders
Go eFolder
Next
End If
For Each strFile In objDIR.Files
WScript.Echo "Full Path: " & strFile.Path
WScript.Echo "File Size(bytes): " & strFile.Size
WScript.Echo "File Date modified: " & strFile.DateLastModified
WScript.Echo "File Date Created: " & strFile.DateCreated
WScript.Echo "File Date accessed: " & strFile.DateLastAccessed
Next
End Sub
on command line
c:\test> cscript //nologo myscript.vbs c:\

To save the results to a csv file modify ghostdog74's code as follows.
Set objFS=CreateObject("Scripting.FileSystemObject")
WScript.Echo Chr(34) & "Full Path" &_
Chr(34) & "," & Chr(34) & "File Size" &_
Chr(34) & "," & Chr(34) & "File Date modified" &_
Chr(34) & "," & Chr(34) & "File Date Created" &_
Chr(34) & "," & Chr(34) & "File Date Accessed" & Chr(34)
Set objArgs = WScript.Arguments
strFolder = objArgs(0)
Set objFolder = objFS.GetFolder(strFolder)
Go (objFolder)
Sub Go(objDIR)
If objDIR <> "\System Volume Information" Then
For Each eFolder in objDIR.SubFolders
Go eFolder
Next
End If
For Each strFile In objDIR.Files
WScript.Echo Chr(34) & strFile.Path & Chr(34) & "," &_
Chr(34) & strFile.Size & Chr(34) & "," &_
Chr(34) & strFile.DateLastModified & Chr(34) & "," &_
Chr(34) & strFile.DateCreated & Chr(34) & "," &_
Chr(34) & strFile.DateLastAccessed & Chr(34)
Next
End Sub
Then call it from the command line like this.
c:\test> cscript //nologo myscript.vbs "c:\" > "C:\test\Output.csv"

Related

Specified File Can't be Found

I have been creating this to do auto-attend of setting up of computers with minimum attention needed. Been working on this and made some breakthroughs.
I want to be able to run this as admin from the start of the script. The problem is is down at the wshShell.Run(Command_3A) 'Lightspeed. The program runs great to that point but then gives me a Specified file can't be found.
The entire thing worked fine until I inserted the Runas admin command. So I am figuring it is somewhere in there. Here is the portion to Runas admin.
Set wshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.Length = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe" _
, Chr(34) & WScript.ScriptFullName & Chr(34) & " RunAsAdministrator", , "runas", 1
WScript.Quit
End If
Here is all the code.
Set wshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.Length = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe" _
, Chr(34) & WScript.ScriptFullName & Chr(34) & " RunAsAdministrator", , "runas", 1
WScript.Quit
End If
spath = "C:\ThumbDrive"
' Step 1 - Set Power Settings
Command_1A = "powercfg /change standby-timeout-ac 0"
Command_1B = "powercfg /change standby-timeout-dc 15"
Command_1C = "powercfg /change monitor-timeout-ac 0"
Command_1D = "powercfg /change monitor-timeout-dc 15"
Command_1E = "powercfg /change hibernate-timeout-ac 0"
Command_1F = "powercfg /change hibernate-timeout-dc 15"
wshShell.Run "cmd /k " & Command_1A & "&" & Command_1B & "&" & Command_1C & "&" & Command_1D & "&" & Command_1E & "&" & Command_1F & "& exit"
WScript.Sleep 3000
' Step 2 - Remove Bloatware (Win10Apps)
' Step 3 - Install wanted programs
Command_3A = Chr(34) & spath & "\Programs\Lightspeed\UserAgentx64 V2.1.14.msi" & Chr(34)
Command_3B = Chr(34) & spath & "\Programs\AcroRdrDC1801120058_en_US.exe" & Chr(34) & "& /sAll "
Command_3C = Chr(34) & spath & "\Programs\AzMERITSecureBrowser10.4-2018-08-02.msi" & Chr(34) & "& /passive "
Command_3D = Chr(34) & spath & "\Programs\jre-8u201-windows-x64.exe" & Chr(34) & "& /s "
Command_3E = Chr(34) & spath & "\Programs\ChromeStandaloneSetup64.exe" & Chr(34) & "& /silent /install "
Command_3F = Chr(34) & spath & "\Programs\ESet Rip and Replace.exe" & Chr(34)
wshShell.Run(Command_3A) 'LightSpeed
WScript.Sleep 4000
wshShell.Run(Command_3B) 'Adobe Reader
WScript.Sleep 30000
wshShell.Run(Command_3C) 'AzMerit
WScript.Sleep 4000
wshShell.Run(Command_3D) 'Java
WScript.Sleep 30000
wshShell.Run(Command_3E) 'Chrome
WScript.Sleep 30000
wshShell.Run(Command_3F) 'Eset
So I know the UAC works and all the code works without the UAC Control. Can anyone help me figure out why the UAC control breaks everything.
The CurrentDirectory property returns a string that contains the fully qualified path of the current working directory of the active process.
It points to C:\WINDOWS\system32 for an elevated script. You can either
supply the current working directory from unelevated process to the elevated one, or
elicit it from the Wscript.ScriptFullName property.
Both methods are illustrated in the following code snippet:
Set wshShell = WScript.CreateObject("WScript.Shell")
spath = wshShell.CurrentDirectory
If WScript.Arguments.Length = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe" _
, Chr(34) & WScript.ScriptFullName & Chr(34) _
& " RunAsAdministrator " & Chr(34) & spath & Chr(34), , "runas", 1
WScript.Quit
End If
'' take out the supplied value:
spath = WScript.Arguments(1)
'' find out where the script resides:
shtap = Left( WScript.ScriptFullName, _
Len( WScript.ScriptFullName) - Len( WScript.ScriptName) -1 )
'' debugging output
Wscript.Echo CStr( WScript.Arguments.Length) & _
vbNewLine & Chr(34) & WScript.Arguments(0) & Chr(34) & _
vbNewLine & Chr(34) & spath & Chr(34) & _
vbNewLine & Chr(34) & shtap & Chr(34) & _
vbNewLine & Chr(34) & wshShell.CurrentDirectory & Chr(34)
' Step 1 - Set Power Settings
Output (using cscript.exe Windows Script Host):
2
"RunAsAdministrator"
"D:\bat\SO"
"D:\bat\SO"
"C:\WINDOWS\system32"
I had to create a input box to specify what the sPath was, and left the UAC reading from C:\WINDOWS\system32. It works wonderfully, just wish I could get it to run without having the input box.

How do i automate the simulation of right clicking a file and selecting the first option using vbscript

Currently i am doing this.But this is not working,just creating a shortcut of the vb script.
Dim objShell
Set objShell = WScript.CreateObject ("WScript.shell")
objShell.Run "E:\folder\xyz.cmd"
objShell.Sendkeys ("+{F10}")
objShell.Sendkeys "s"
Set objShell = Nothing
Can some one please help me on this.Thanks
This uses the shell's COM objects. We choose the verb we want.
Run the script without arguments for help.
To list verbs
shverb.vbs "E:\folder\xyz.cmd"
To Open
shverb.vbs "E:\folder\xyz.cmd" edit
The documentation for the shell is here https://msdn.microsoft.com/en-us/library/windows/desktop/bb787868(v=vs.85).aspx
HelpMsg = vbcrlf & " ShVerb" & vbcrlf & vbcrlf & " David Candy 2014" & vbcrlf & vbcrlf & " Lists or runs an explorer verb (right click menu) on a file or folder" & vbcrlf & vbcrlf & " ShVerb <filename> [verb]" & vbcrlf & vbcrlf & " Used without a verb it lists the verbs available for the file or folder" & vbcrlf & vbcrlf
HelpMsg = HelpMsg & " The program lists most verbs but only ones above the first separator" & vbcrlf & " of the menu work when used this way" & vbcrlf & vbcrlf
HelpMsg = HelpMsg & " The Properties verb can be used. However the program has to keep running" & vbcrlf & " to hold the properties dialog open. It keeps running by displaying" & vbcrlf & " a message box."
Set objShell = CreateObject("Shell.Application")
Set Ag = WScript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
If Ag.count = 0 then
wscript.echo " ShVerb - No file specified"
wscript.echo HelpMsg
wscript.quit
Else If Ag.count = 1 then
If LCase(Replace(Ag(0),"-", "/")) = "/h" or Replace(Ag(0),"-", "/") = "/?" then
wscript.echo HelpMsg
wscript.quit
End If
ElseIf Ag.count > 2 then
wscript.echo vbcrlf & " ShVerb - To many parameters" & vbcrlf & " Use quotes around filenames and verbs containing spaces" & vbcrlf
wscript.echo HelpMsg
wscript.quit
End If
If fso.DriveExists(Ag(0)) = True then
Set objFolder = objShell.Namespace(fso.GetFileName(Ag(0)))
' Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
Set objFolderItem = objFolder.self
msgbox ag(0)
ElseIf fso.FolderExists(Ag(0)) = True then
Set objFolder = objShell.Namespace(fso.GetParentFolderName(Ag(0)))
Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
ElseIf fso.fileExists(Ag(0)) = True then
Set objFolder = objShell.Namespace(fso.GetParentFolderName(Ag(0)))
Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
Else
wscript.echo " ShVerb - " & Ag(0) & " not found"
wscript.echo HelpMsg
wscript.quit
End If
Set objVerbs = objFolderItem.Verbs
'If only one argument list verbs for that item
If Ag.count = 1 then
For Each cmd in objFolderItem.Verbs
If len(cmd) <> 0 then CmdList = CmdList & vbcrlf & replace(cmd.name, "&", "")
Next
wscript.echo mid(CmdList, 2)
'If two arguments do verbs for that item
ElseIf Ag.count = 2 then
For Each cmd in objFolderItem.Verbs
If lcase(replace(cmd, "&", "")) = LCase(Ag(1)) then
wscript.echo(Cmd.doit)
Exit For
End If
Next
'Properties is special cased. Script has to stay running for Properties dialog to show.
If Lcase(Ag(1)) = "properties" then
WSHShell.AppActivate(ObjFolderItem.Name & " Properties")
msgbox "This message box has to stay open to keep the " & ObjFolderItem.Name & " Properties dialog open."
End If
End If
End If

Moving Windows 7 user library (desktop, my docs...etc) to another location

In Windows 7, I cut the user library folders and then I paste them to another location (including the content of them). When I do it that way, Windows 7 makes the changes needed (registry, path ... etc.) in order to get those special folders working again.
Is there a way to script it (in VBS if possible)?
I've given a look at WshShell.SpecialFolders commands and the fso.MoveFolder but I don't find the way...
Does anyone have an idea or a link in order to help me? Thanks by advance.
You need to use shell objects from Shell32. This lists or executes a command on a file/folder object (eg Cut). You can use Shell.namespace to get a special folder, see https://msdn.microsoft.com/en-us/library/windows/desktop/bb774094(v=vs.85).aspx
HelpMsg = vbcrlf & " ShVerb" & vbcrlf & vbcrlf & " David Candy 2014" & vbcrlf & vbcrlf & " Lists or runs an explorer verb (right click menu) on a file or folder" & vbcrlf & vbcrlf & " ShVerb <filename> [verb]" & vbcrlf & vbcrlf & " Used without a verb it lists the verbs available for the file or folder" & vbcrlf & vbcrlf
HelpMsg = HelpMsg & " The program lists most verbs but only ones above the first separator" & vbcrlf & " of the menu work when used this way" & vbcrlf & vbcrlf
HelpMsg = HelpMsg & " The Properties verb can be used. However the program has to keep running" & vbcrlf & " to hold the properties dialog open. It keeps running by displaying" & vbcrlf & " a message box."
Set objShell = CreateObject("Shell.Application")
Set Ag = WScript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
If Ag.count = 0 then
wscript.echo " ShVerb - No file specified"
wscript.echo HelpMsg
wscript.quit
Else If Ag.count = 1 then
If LCase(Replace(Ag(0),"-", "/")) = "/h" or Replace(Ag(0),"-", "/") = "/?" then
wscript.echo HelpMsg
wscript.quit
End If
ElseIf Ag.count > 2 then
wscript.echo vbcrlf & " ShVerb - To many parameters" & vbcrlf & " Use quotes around filenames and verbs containing spaces" & vbcrlf
wscript.echo HelpMsg
wscript.quit
End If
If fso.DriveExists(Ag(0)) = True then
Set objFolder = objShell.Namespace(fso.GetFileName(Ag(0)))
' Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
Set objFolderItem = objFolder.self
msgbox ag(0)
ElseIf fso.FolderExists(Ag(0)) = True then
Set objFolder = objShell.Namespace(fso.GetParentFolderName(Ag(0)))
Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
ElseIf fso.fileExists(Ag(0)) = True then
Set objFolder = objShell.Namespace(fso.GetParentFolderName(Ag(0)))
Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
Else
wscript.echo " ShVerb - " & Ag(0) & " not found"
wscript.echo HelpMsg
wscript.quit
End If
Set objVerbs = objFolderItem.Verbs
'If only one argument list verbs for that item
If Ag.count = 1 then
For Each cmd in objFolderItem.Verbs
If len(cmd) <> 0 then CmdList = CmdList & vbcrlf & replace(cmd.name, "&", "")
Next
wscript.echo mid(CmdList, 2)
'If two arguments do verbs for that item
ElseIf Ag.count = 2 then
For Each cmd in objFolderItem.Verbs
If lcase(replace(cmd, "&", "")) = LCase(Ag(1)) then
wscript.echo(Cmd.doit)
Exit For
End If
Next
'Properties is special cased. Script has to stay running for Properties dialog to show.
If Lcase(Ag(1)) = "properties" then
WSHShell.AppActivate(ObjFolderItem.Name & " Properties")
msgbox "This message box has to stay open to keep the " & ObjFolderItem.Name & " Properties dialog open."
End If
End If
End If
Copying files in a folder to a blank or existing zip file. Cause zip files are folders in the shell.
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set SrcFldr=objShell.NameSpace(Ag(1))
Set DestFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"

Passing Multiple variables to an application using VBScript

I am trying to path multiples variables to an application using vbscript but it is not working for me and I do not know how to fix:
Set SH = WScript.CreateObject("WScript.Shell")
Set colFiles = objFolder.Files
For Each objFile in colFiles
SH.Run ".\Resizer.exe /resize /overwrite /width: " & strResize & objFile.Path & objFile.Path,,True
Next
Resizer.exe will resize objFile.path (example: D:\pic.jpg) with strReszie width and will save it again as objFile.path
Where is the problem?
Two things:
You're not putting any spaces between your params:
strResize & objFile.Path & objFile.Path
should be:
strResize & " " & objFile.Path & " " & objFile.Path
Make sure you surround any file paths with quotes, in case they contains spaces:
strResize & " " & Chr(34) & objFile.Path & Chr(34) & " " & Chr(34) & objFile.Path & Chr(34)

VBScript Issue Help Required

I need a script that can run and pull information from any drive on a Windows operating system (Windows Server 2003), listing all files and folders which contain the following fields: The server is quite big and is within our domain.
The required information is:
Full file path (e.g. C:\Documents and Settings\user\My Documents\testPage.doc)
File type (e.g. word document, spreadsheet, database etc)
Size
When Created
When last modified
When last accessed
Also the script will need to convert that data to a CSV file, which later on I can modify and process in Excel. I can imagine that this data will be huge but I still need it. I am logged in as an administrator on the server and the script will need to also process protected files. As in previous posts I have read that the script will stop if such files are processed. I need to make sure that not a single file is skipped.
Please note I have asked this question before but still have not got a working script.
This is the script I got so far, file Test.vbs:
Set objFS=CreateObject("Scripting.FileSystemObject")
WScript.Echo Chr(34) & "Full Path" &_
Chr(34) & "," & Chr(34) & "File Size" &_
Chr(34) & "," & Chr(34) & "File Date modified" &_
Chr(34) & "," & Chr(34) & "File Date Created" &_
Chr(34) & "," & Chr(34) & "File Date Accessed" & Chr(34)
Set objArgs = WScript.Arguments
strFolder = objArgs(0)
Set objFolder = objFS.GetFolder(strFolder)
Go (objFolder)
Sub Go(objDIR)
If objDIR <> "\System Volume Information" Then
For Each eFolder in objDIR.SubFolders
Go eFolder
Next
End If
For Each strFile In objDIR.Files
WScript.Echo Chr(34) & strFile.Path & Chr(34) & "," &_
Chr(34) & strFile.Size & Chr(34) & "," &_
Chr(34) & strFile.DateLastModified & Chr(34) & "," &_
Chr(34) & strFile.DateCreated & Chr(34) & "," &_
Chr(34) & strFile.DateLastAccessed & Chr(34)
Next
End Sub
I am currently using the command-line to run it:
c:\test> cscript //nologo Test.vbs "c:\" > "C:\test\Output.csv"
The script is not working. I don't know why.
Properly passing cmd-line args to vbs when using Cscript
-Modify the cmd-line statement as follows (drop the ">")
c:\test> cscript //nologo Test.vbs "c:\" "C:\test\Output.csv"
-Next inside the script you can access the cmd-line parameters as follows
strFolder = WScript.Arguments.Item(0)
strFile = WScript.Arguments.Item(1)
I hope now you can access the files and list them properly in the csv-file. :-)
GoodLUCK!!
- CVS

Resources