i currently have a vbscript that creates a txt file in a directory and opens it, but id like to make it so that the file is hidden,
currtly i have this code:
Set objFSO=CreateObject("Scripting.FileSystemObject")
outFile="C:\Users\User\Desktop\New map"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write "test line 1" & vbCrLf
objFile.Write "test line 2" & vbCrLf
objFile.Close
CreateObject("WScript.Shell").Run("""C:\Users\User\Desktop\New map""")
You can set the attribute like this
Const cHidden = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
outFile = "C:\Users\User\Desktop\New map"
Set objFile = objFSO.CreateTextFile(outFile, True)
objFile.Write "test line 1" & vbCrLf
objFile.Write "test line 2" & vbCrLf
objFile.Close
Set mapFile = objFSO.GetFile(outFile)
mapFile.Attributes = cHidden
CreateObject("WScript.Shell").Run Chr(34) & outFile & Chr(34)
Quick Reference --> https://www.thevbprogrammer.com/ch06/06-09-fso.htm
https://ss64.com/vb/filesystemobject.html
Related
For some reason when I run my vbscript, I am getting an object required at Line 4 Char 1 for the InstallLog. Any idea why this might be occurring?
Dim wshShell, FSO, strDexcomFolder, strDexcom, SysRoot, intRunError, strGroup, strDomain, InstallLog
Const ForWriting = 2
Set InstallLog = FSO.OpenTextFile("Install_Log.txt", ForWriting)
Set wshShell = CreateObject("WScript.Shell")
SysRoot = WshShell.ExpandEnvironmentStrings("%SystemDrive%")
Set FSO = CreateObject("Scripting.FileSystemObject")
strDexcomFolder = "c:\Program Files (x86)\Bioex"
strDomain = "xxxxxxxx"
strGroup = "domain users"
msgbox strDexcomFolder
If FSO.FolderExists(strDexcomFolder) Then
msgbox"start"
intRunError = WshShell.Run("icacls """ & strDexcomFolder & """ /grant " & strDomain & "\" & strGroup & ":(OI)(CI)(M) ", 2, True)
msgbox intRunError
If Err.number <> 0 Then
InstallLog.WriteLine("")
InstallLog.WriteLine("Error Assigning Permissions!")
InstallLog.WriteLine("Error #: "&Err.Number&", "&Err.Description&"")
InstallLog.WriteLine("")
MsgBox"Error assigning permissions!"
InstallLog.close
End If
Else
Wscript.Echo "Error: folder " & strDexcomFolder & " does not exist"
End If
WScript.Quit
Here. This should get you going. The icacls command is now being echoed into the log so you can confirm your syntax is being passed correctly. Edit - Some command line programs do not pass arguments correctly without preceding them with "cmd.exe /C". I added that also along with full path to icacls.exe in case you are running from a location that is not in the system path.
Option Explicit
Dim wshShell, objFSO, strDexcomFolder, strDexcom, SysRoot, intRunError, strGroup, strDomain, InstallLog, strWinDir
Set wshShell = CreateObject("WScript.Shell")
SysRoot = WshShell.ExpandEnvironmentStrings("%SystemDrive%")
strWinDir = WshShell.ExpandEnvironmentStrings("%windir%")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const ReadOnly = 1
strDexcomFolder = "c:\Program Files (x86)\Bioex"
strDomain = "xxxxxxxx"
strGroup = "domain users"
Set InstallLog = objFSO.CreateTextFile("Install_Log.txt", True)
MsgBox strDexcomFolder
If objFSO.FolderExists(strDexcomFolder) Then
MsgBox "Start"
InstallLog.WriteLine("Running Command - " & strWinDir & "\System32\cmd.exe /C " & strWinDir & "\System32\icacls.exe " & Chr(34) & strDexcomFolder & Chr(34) & " /grant " & Chr(34) & strDomain & "\" & strGroup & chr(34) & ":(OI)(CI)(M)")
intRunError = WshShell.Run(strWinDir & "\System32\cmd.exe /C " & strWinDir & "\System32\icacls.exe " & Chr(34) & strDexcomFolder & Chr(34) & " /grant " & Chr(34) & strDomain & "\" & strGroup & chr(34) & ":(OI)(CI)(M)", 2, True)
MsgBox intRunError
If intRunError <> 0 Then
InstallLog.WriteLine("")
InstallLog.WriteLine("Error Assigning Permissions!")
InstallLog.WriteLine("Error #: " & Err.Number & ", " & Err.Description)
InstallLog.WriteLine("")
MsgBox "Error assigning permissions!"
End If
Else
InstallLog.WriteLine("Error: folder " & strDexcomFolder & " does not exist")
WScript.Echo "Error: folder " & strDexcomFolder & " does not exist"
End If
InstallLog.close
WScript.Quit
You will definitely need to have this line of code PRECEDES those which are using the FSO object or calling a function like FSO.OpenTextFile
Set FSO = CreateObject("Scripting.FileSystemObject")
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
I'm new to PowerShell. Now the task is to pin some programs to the Taskbar. This Tutorial is great and runs well in one of my Win7 32bit in Vmware. But same script cannot run in Win10 32bit in Vmware. So I modified the code and finally found it was because some Verbs are not found. My test code is as following:
CLS
$cmd = 'C:\Windows\System32\cmd.exe'
Test-Path $cmd
$Shell = New-Object -ComObject Shell.Application
$Desktop = $Shell.NameSpace(0X0)
$itemLnk = $Desktop.ParseName($cmd)
$itemVerbs = $itemLnk.Verbs()
Foreach($v in $itemVerbs)
{
write-host $v.Name
}
The ISE is run as admin and the result, as well as 'Verbs' found by mannually right clicking the cmd.exe, are shown as below:
So please tell me why and how to fix this, thank you!
Its probably because there is no cmd.exe located in C:\Windows\, use this instead:
$cmd = Join-Path $env:SystemRoot 'System32\cmd.exe'
Note: Im using the Join-Path cmdlet to combine the path and $env:SystemRoot to retrieve the system root (e. g. C:\Windows)
You can only use verbs above the first separator and Properties. Your program must keep running to display properties.
Here's a VBScript that list and performs the verb. ShVerb.vbs /? to read the above.
HelpMsg = vbcrlf & " ShVerb" & vbcrlf & vbcrlf & " 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
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"
I’m new to vbs, but I need to make a script that copies files from one folder to another, then reads a hole list of copied files and make a log file with those names. This is where I got it covered. Next thing I need to do is to make some sort of msgbox/IEmsg out of this list. This msgbox should have all of the file names, line under the line, that script just copied.
This is my script:
StrMonth = Month(Date)
If Len(strMonth) = 1 Then
strMonth = "0" & strMonth
End If
StrDay = Day(Date)
If Len(strDay) = 1 Then
strDay = "0" & strDay
End If
StrYear = Year(Date)
FolderName = "c:\Rafel" & strDay & "." & strMonth & "." & StrYear
Set objFSO = CreateObject("Scripting.FileSystemObject")
SET objFolder = objFSO.CreateFolder(FolderName)
objFSO.CopyFile "c:\Rafel\files\*.*", FolderName & "\"
Wscript.Sleep 1000
IF objFSO.FileExists( FolderName & "\database.txt") THEN
Wscript.Echo "Folder created, files are copied."
END IF
SET objFSO = CreateObject("Scripting.FileSystemObject")
FILEFOLDER = FolderName
SET objFolder = objFSO.GetFolder(FILEFOLDER)
SET filelist = objFolder.Files
FILELOG = FolderName & "\file log.log"
FOR EACH objFile IN filelist
FILENAME = FILENAME & "###" & Now & " File copied" & "###" & vbCrlf &_
" File name:" & objFile.Name & vbCrlf &_
" Create date:" & objFile.DateLastModified & vbCrlf &_
" Size:" & objFile.Size & vbCrlf &_
vbCrlf
NEXT
SET objFile = objFSO.CreateTextFile(FILELOG)
objFile.Write vbCrlf
objFile.Write(FILENAME)
objFile.Close
As you can see, it works as far as making copies and creating log file with a list of files. But how can I make a one message box with a list of those files? I’ve tried, for each but it just “spam” msgbox for each file.
I'm a noob so pls take that in mind. :)
Use a custom wscript.shell popup box to expand your maximum amount of characters of the msgbox and display the msgbox accordingly.
More info
Code.
StrMonth = Month(Date)
If Len(strMonth) = 1 Then
strMonth = "0" & strMonth
End If
StrDay = Day(Date)
If Len(strDay) = 1 Then
strDay = "0" & strDay
End If
StrYear = Year(Date)
FolderName = "c:\Rafel" & strDay & "." & strMonth & "." & StrYear
Set objFSO = CreateObject("Scripting.FileSystemObject")
SET objFolder = objFSO.CreateFolder(FolderName)
objFSO.CopyFile "c:\Rafel\files\*.*", FolderName & "\"
Wscript.Sleep 1000
IF objFSO.FileExists( FolderName & "\database.txt") THEN
Wscript.Echo "Folder created, files are copied."
END IF
SET objFSO = CreateObject("Scripting.FileSystemObject")
FILEFOLDER = FolderName
SET objFolder = objFSO.GetFolder(FILEFOLDER)
SET filelist = objFolder.Files
FILELOG = FolderName & "\file log.log"
FOR EACH objFile IN filelist
FILENAME = FILENAME & "###" & Now & " File copied" & "###" & vbCrlf &_
" File name:" & objFile.Name & vbCrlf &_
" Create date:" & objFile.DateLastModified & vbCrlf &_
" Size:" & objFile.Size & vbCrlf &_
vbCrlf
NEXT
SET objFile = objFSO.CreateTextFile(FILELOG)
objFile.Write vbCrlf
objFile.Write(FILENAME)
objFile.Close
Dim objShell : Set objShell = CreateObject("Wscript.Shell")
objShell.Popup FILENAME, 0, "Filenames", 0