Run a command and capture the output in vbscript - vbscript

I am trying to run the following command and return the output of it using VBscript:
dir /A-d "C:\Windows\Minidump" | find /c "/"
And I have the following script but it does not work (probably because of " charachters:
Wscript.Echo runCMD("dir /A-d "C:\Windows\Minidump" | find /c "/"")
Function runCMD(strRunCmd)
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExec = objShell.Exec(strRunCmd)
strOut = ""
Do While Not objExec.StdOut.AtEndOfStream
strOut = strOut & objExec.StdOut.ReadLine()
Loop
Set objShell = Nothing
Set objExec = Nothing
runCMD = strOut
End Function
Any suggestions on how to achieve this?

dir is intrinsic; you need %comspec%.
Double quotes need to be escaped by double double quotes in VBScript:
Wscript.Echo runCMD("%comspec% /c dir /A-d ""C:\Windows\Minidump"" | find /c ""/""")

Related

What's the difference between FSO.DeleteFolder() method and oWS.Run "%comspec% /c rmdir ...", 1, True?

I got a vbs that I wrote.
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oWS = CreateObject("WScript.Shell")
Set objFolderUsers = FSO.GetFolder("\\"& strComputer &"\C$\Users\").Subfolders
...
Later, I do something like :
For Each objFlder In objFolderUsers
user = Right(objFlder, Len(objFlder) - InStrRev(objFlder, "\"))
temp = objFlder & "\AppData\Local\Temp\"
'That's the line !...
If FSO.FolderExists(temp) Then FSO.DeleteFolder(temp)
If Not IsExcludeProfile(user) Then
If Left(objFlder.DateLastModified, 4) <= minYear Then
oWS.Run "%comspec% /c rmdir " & objFlder & " /s /q", 0, True
oWS.Run "%comspec% /c net user " & user & " /delete", 0, True
End If
End If
Next
my question is : Is there a difference between FSO.DeleteFolder(temp) and oWS.Run "%comspec% /c rmdir " & temp, 1, True because when I do the first all things are OK but when I do the second (the oWS.Run) AND objFlder = "Default" it is all deleted, not just the Temp as I want ...
Try to put enclosing quotes in the file name passed to the rmdir, because blank spaces may terminate the string that it receives before its end:
oWS.Run "%comspec% /c rmdir """ & objFlder & """ /s /q", 0, True
Just reminding that "" inside a string literal in vbs means a single " in the contents of the string.

corresponding commands from batch file to vbsript

I trying to go from cmd script to vbscript in MS window xp
cmd code yes works
set home_=%~dp0
set part001=part001
set part002=part002
set part003=part003
set part004=part004
::get the dir in part001
for /f "delims=" %%A in ('dir /s/b/o:n/a:d ^"%home%%part001%\^"') do (
echo show have dir path
echo %%A
pause
)
echo to the end
pause
goto :eof
to vbscript
the part I do not know to convert are those that are foramtted as cmd{cmd codeing}
dim strHome as strimg =cmd{[%~dp0]}
dim strPart001 as sting = part001
dim strPart002 as sting = part002
dim strPart003 as sting = part003
dim strPart004 as sting = part004
'get the dir in part001
Dim objFSo, objFile
Set objFSo = CreateObject("Scripting.FileSystemObject")
set objDirPart001list = objFS.getfolder(strHome&strPart001\)
set subDirPart001list = objDirPart001list.SubFolders
for each subDirPart001Name in subDirPart001list
WScript.Echo show dir path
WScript.Echo part
cmd{pause}
)
cmd{pause}
what are the corresponding vbscript commands for:
%~dp0
pause
The %~dp0 is a so-called magic command but more technically this method is known as variable substition. The %n variables are used to reference the command line parameters of the script. The most common, %0 will return the full path to the script that is executing. The d and p are special modifiers that will return the drive and path portion of that path, respectively. There is also n which returns the filename portion as well as others. These modifiers can be combined as necessary. So the %~dp0 command will return the full drive and path to the directory where the executing script resides. To do this in VBScript, you can use any of the following that rely on the WScript Object's ScriptFullName method:
Set objShell = CreateObject("WScript.Shell")
Set objFso = CreateObject("Scripting.FileSystemObject")
strPath = objFso.GetParentFolderName(WScript.ScriptFullName)
Or
strPath = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName))
Or my favorite:
Replace(WScript.ScriptFullName, WScript.ScriptName, "")
The pause command is used to stop the command interpreter and prompt the user to press any key to continue. This is typically done so that the user has time to read the information in the command window before it closes. Here's a subroutine to do that in CScript. (For WScript, you would just use a simple MessageBox.)
Sub Pause
WScript.StdOut.Write "Press any key to continue . . . "
WScript.StdIn.Read(1)
End Sub

set permissions with a vbs script

I have a VBS script that downloads a file on login and places it in a given folder, It works brilliantly in some places but in others it falls over because the file was created by user1 and user2 can't overwrite it.
How would i give the group "Everyone" full control of a given file using a VBS script?
Something like this:
Set WshShell = CreateObject("WScript.Shell")
strFile = "c:\test_folder\test_file.txt"
setPerms = "%COMSPEC% /c echo Y| C:\windows\system32\cacls.exe " & Chr(34) & strFile & Chr(34) & " /G domain\everyone:F"
wscript.echo setPerms
WshShell.run setPerms
Partially gleaned from here:
http://social.technet.microsoft.com/forums/en-us/ITCG/thread/6CDA091A-6B3D-4F58-8374-9A46F59F389A
One way of doing it would be to use the CACLS command line tool. Just run it from your script using Shell.Run.
Here's another link to information about how to use CACLS that has some samples.
Function giveFullPermissionToFolder(strFolder)
Dim objShell, strCmd, intRunError
Set objShell = CreateObject("Wscript.Shell")
strCmd = "%comspec% /c echo Y| cacls " & strFolder & " /T /E /C /G Users:F"
intRunError = objShell.Run(strCmd, 2, True)
If intRunError<>0 Then
Reporter.ReportEvent micFail, "giveFullPermissionToFolder" , "Unable to give full permission to " & strFolder
End If
Set objShell=Nothing
End Function

VBS Script - Run series of .batch jobs

Help me run a series of .bat script
they are located like so:
p:\Co-Brand\export.bat
p:\Generic\export.bat
p:\Tri-Brand\export.bat
Thanks in advance,
Best regards,
Joe
Would a simple shell command do? You can call this from a command prompt:
for /R %F in (*.bat) do "%F"
or the following from a .bat file:
for /R %%F in (*.bat) do call "%%F"
found a way that works, should have tried this first of all.
I am a bit embarrassed that it was this easy actually:
cd P:\Co-Brand\
CALL Export.bat
cd P:\Generic\
CALL Export.bat
cd P:\TriBrand\
CALL Export.bat
cd P:\UBA\
CALL Export.bat
As originally asked, here is a VBScript solution...
The problem described is probably related to the "Script-Working-Directory".
Try this ...
Dim objShell
Dim blnWaitOnReturn
Dim strOriginalCD
Dim strCmd
Dim intWindowStyle
Dim intExitCode
Set objShell = WScript.CreateObject("Wscript.Shell")
'' if necessary, save the original "Script-Working-Directory"
strOriginalCD = objShell.CurrentDirectory
intWindowStyle = 1
blnWaitOnReturn = True
objShell.CurrentDirectory = "p:\Co-Brand\"
strCmd = "%comspec% /K export.bat"
intExitCode = objShell.Run(strCmd, intWindowStyle, blnWaitOnReturn)
objShell.CurrentDirectory = "p:\Generic\"
strCmd = "%comspec% /K export.bat"
intExitCode = objShell.Run(strCmd, intWindowStyle, blnWaitOnReturn)
objShell.CurrentDirectory = "p:\Tri-Brand\"
strCmd = "%comspec% /K export.bat"
intExitCode = objShell.Run(strCmd, intWindowStyle, blnWaitOnReturn)
'' if necessary, restore the original "Script-Working-Directory"
objShell.CurrentDirectory = strOriginalCD
Notes:
'' If filename contains spaces make sure to add double-quotes around filename
strCmd = "%comspec% /K " & Chr(34) & "File name with spaces.bat" & Chr(34)
'' To run the commands in a "Hidden" window, use:
intWindowStyle = 0
'' To run the commands "Minimized", use:
intWindowStyle = 7
More info on "objShell.Run" can be found here: http://ss64.com/vb/run.html
The above examples will cause VBScript to wait for each called ".bat" to complete and return an "ExitCode" before proceeding.
If you don't want VBScript to wait for one ".bat" to complete before proceeding to the next then set blnWaitOnReturn = False, and remove intExitCode like:
...
blnWaitOnReturn = False
objShell.CurrentDirectory = "p:\Co-Brand\"
strCmd = "%comspec% /K export.bat"
objShell.Run strCmd, intWindowStyle, blnWaitOnReturn
objShell.CurrentDirectory = "p:\Generic\"
strCmd = "%comspec% /K export.bat"
objShell.Run strCmd, intWindowStyle, blnWaitOnReturn
objShell.CurrentDirectory = "p:\Tri-Brand\"
strCmd = "%comspec% /K export.bat"
objShell.Run strCmd, intWindowStyle, blnWaitOnReturn
...
If you want the ability to get the "Status" and "ProcessID", and access the standard streams of the executable to read/write to the process's stdout/stderr in real-time while the process executes, then use "objShell.Exec".
More info on "objShell.Exec" can be found here: http://ss64.com/vb/exec.html

bat read a file line by line

In my bat script, is it possible to access a txt file and read it line by line. The idea I'm having is to check if the line starts with an identifier word (in my case 1 or 2 stars * or **) but to do this I need to read the file line by line.
you can use vbscript
strToFind= WScript.Arguments(0)
strToFind = Replace(strToFind,"*","\*")
strFileName = WScript.Arguments(1)
Set objFS = CreateObject( "Scripting.FileSystemObject" )
Set objFile = objFS.OpenTextFile(strFileName)
Set objRE = New RegExp
objRE.IgnoreCase = False
objRE.Pattern = "^"&strToFind&".*"
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
Set Matches = objRE.Execute(strLine)
'WScript.Echo Matches.Count
For Each Match in Matches ' Iterate Matches collection.
WScript.Echo Match.Value
Next
Loop
objFile.Close
Usage:
C:\test>cscript //nologo myscript.vbs "**" file
Here's what I found: http://www.computing.net/howtos/show/batch-file-tip-reading-writing-every-line-of-a-file/61.html
Hope that helps..
CODE:
#echo off
for /f "delims=] tokens=1*" %%a in ('find /v /n "" ^<%1') do (
echo.%%b
)

Resources