Brackets in the folder name - vbscript

I'm trying to create a script that runs a file the problem is that the folder where is it has the character "[]" and every time I try to open it displays an error saying that the path was not found... as I would to ignore this character?
Sub Main()
If WScript.Arguments.Count >= 1 Then
MyFileSWF = WScript.Arguments.Item(0)
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "CMD /C Start /Max "" ""[ Utilities ]\Flash Player Standalone 18.exe"" " & """" & MyFileSWF & """", 0, True
End If
End Sub
On Error Resume Next
Main
If Err.Number Then
WScript.Quit 4711
End If

This:
"CMD /C Start /Max "" ""[ Utilities ]\Flash Player Standalone 18.exe"" " & """" & MyFileSWF & """"
Is being interpreted as:
CMD /C Start /Max " "[ Utilities ]\Flash Player Standalone 18.exe" "%swfFile%"
Note how the directory name (with square-brackets) is actually excluded from the first quote-enclosed string.
Change it to this:
"CMD /C Start /Max ""[ Utilities ]\Flash Player Standalone 18.exe"" """ & MyFileSWF & """"

Related

wshShell.run single line command wont set variable and pass correctly

the script is executed from the network and is supposed to bring in a config file located in the local machines C: drive with a path for mapping.
I've tried a few different ideas but every time I get "System error 67 has occurred. The network name cannot be found." Any help is appreciated as I can't resolve the issue. (I'm a newbie to command line)
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "cmd /k cd.. & cd CIEB_Group3 & set /p RootServer=<Server.txt & net use K: %RootServer% /pers:yes", 1, True
Instead of creating a VBS file that calls a single long command, it would be possible to do this using more VBScript, allowing further development at a later date if you so wished.
The below script makes the assumption that your "CIEB_Group3" stays in the same place (one cd .. up, then one cd down again) and that the text file remains as "server.txt" and only contains one entry.
Set WshShell = CreateObject("WScript.Shell")
'strpath = script execution directory
strPath = WshShell.CurrentDirectory
strPathReverse = StrReverse(strpath)
'create the parent folder filepath, equivalent to cd..
strParentFilePath = Left(strPath, Len(StrPath) - InStr(strPathReverse,"\"))
'open the text file, making the assumption it is in CIEB_Group3 and is called server.txt
Set objfso = CreateObject("Scripting.FileSystemObject")
Set textfile = objfso.OpenTextFile (strParentFilePath & "\CIEB_Group3\Server.txt", 1)
'read the text file first line into a variable
strNetworkShare = textfile.ReadLine
'map the drive, chr(32) = <space>, chr(34) = "
WshShell.Run "net use K:" & Chr(32) & Chr(34) & strNetworkShare & Chr(34) & Chr(32) & "/pers:yes", 1, 1
Alternatively, if you still would like to use the single CMD line - you may wish to try expanding the environment variable outside the CMD command, as per below.
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "cmd /k cd.. & cd CIEB_Group3 & set /p RootServer=<Server.txt & net use K: " & wshShell.ExpandEnvironmentStrings("%RootServer%") & " /pers:yes", 1, True

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.

How to skip open folders (and skip open files) when moving items using VBScript?

I made a VBscript to move Windows folders, and the script gets stuck when it reaches a folder that a user has open. Is there a way I can change my script so that it skips open folders and skips open files?
Dim fso, directory, item, group, dateStamp, NumberFilesDeleted, text, MoveFoldersErrorInfo, MoveFilesErrorInfo, objLogFile
On error resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
'Move folders in the temp folder to To_Be_Deleted folder and check for errors
fso.MoveFolder "E:\Projects\temp\*" , "E:\Projects\To_Be_Deleted"
If Err.Number <> 0 Then
MoveFoldersErrorInfo = "Error: " & Err.Number & " Error (Hex): " & Hex(Err.Number) & Err.Source & " Description: " & Err.Description
Err.Clear
End If
'Move files in the temp folder to To_Be_Deleted folder and check for errors
fso.MoveFile "E:\Projects\temp\*" , "E:\Projects\To_Be_Deleted"
If Err.Number <> 0 Then
MoveFilesErrorInfo = "Error: " & Err.Number & " Error (Hex): " & Hex(Err.Number) & Err.Source & " Description: " & Err.Description
Err.Clear
End If
Vbscript is extremely slow with fileoperations and if there was a reliable way to check if a file is open it would even further slow down the process. You could just neglect the open files, with "on error resume next" as you do in your scriptsample.
I know of only one reason why you would wanna check and that is forcing the closing of the file with a utility like psfile.exe from systernals but i suppose your users wouldn't be happy with that.
As an external program psfile can be run from your script and check and if desired close open files, so you could run it first on the handled folder with the /c parameter (force close) and then do the move from batch of script.
VBScript is not very reliable in move/delete operations with files. Use DOS commands instead, it will delete/move files even open by users.
You will have to create folders yourself, that is a minus. But it is guaranteed to work and will not give you error messages.
Your code could look similar to this:
Set wshShell = CreateObject( "WScript.Shell" )
wshShell.Run "cmd /c mkdir " & strNewDestination & "\" & strFolder, 0, True
wshShell.Run "cmd /c copy /y " & strFolder & "\*.* " & strNewDestination & "\" & strFolder, 0, True
wshShell.Run "cmd /c del /y " & strFolder & "\*.*", 0, True

Prevent VBscript app from showing Console Window

I have a VBScript app which create Symbolic Links.
Set wshell = CreateObject("WScript.Shell")
.....
linkcmd = "mklink /D """ & linkFolderPath & "\" & linkName & """ """ & libfolder & "\" & folderName & """"
cmd = "cmd /C " & linkcmd
wshell.Run cmd, 4, true
This is fine and works, but when I create a lot of links, each execution of the wshell.Run command results in a transient console window appearing and promptly vanishing.
Is there anyway to prevent the console window from being created so visibly?
You can use this VBScript to run cmd commands hidden, just incorporate it into your script:
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "cmd /c yourcommands", 0, True

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

Resources