vbscript: how to print multiple files with printer selection dialog once - vbscript

I have to write vbscript that prints all files in a folder. Printer dialog should popup at the very beginning to allow user to select printer to use. Than all files should be send to printer silently
How can I do that?
Thank you in advance!

Finally I produced something like this (simplified):
set objPrnDlg = CreateObject( "MSComDlg.CommonDialog" )
objPrnDlg.PrinterDefault = True
objPrnDlg.ShowPrinter
set objShell = CreateObject("Shell.Application")
set objFolder = objShell.Namespace(path-to-files)
set colItems = objFolder.Items
for each objItem In colItems
objItem.InvokeVerbEx ("Print")
next

Related

print all files in folder and then remove them

I am creating a web-based Point of Sale, and right now I am getting stuck with the VBScript.
The script should be printing all files in the selected folder, and after that I want to remove the files printed.
The selected folder will only contain PDF files that are going to be printed.
This is my current code:
TargetFolder = "C:\Users\pos\Downloads\"
If TargetFolder > "" Then
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(TargetFolder)
Set colItems = objFolder.Items
For Each objItem in colItems
objItem.InvokeVerbEx("Print")
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.GetFile(TargetFolder+objItem)
MyFile.Delete
Next
Else
Wscript.Quit
End If
All files are printed if I remove the remove file part, With the remove files it will delete all files before they get printed.
So what I want is to print the files, and remove them once they are printed.
You can detect whether printer jobs queue is empty before deleting files.
Do
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select Document from Win32_PrintJob", , 48)
WScript.Sleep(1000)
Loop While colItems.Count > 0
And then delete files in another loop.

How to list the target path of all My Network Places in vbs?

I want a script to list a users My Network Places in Windows XP. There are lots of examples on the web that show how to get the Name of each location, but I want the target path (i.e. the server name / folder that the link is pointing at).
To get the name I can do:
Const MY_NETWORK_PLACES = &H12&
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(MY_NETWORK_PLACES)
Set objFolderItem = objFolder.Self
Wscript.Echo objFolderItem.Path
Set colItems = objFolder.Items
For Each objItem in colItems
Wscript.Echo objItem.Name
Next
How do I get the target path? Looking at .Path gives me the local path of the shortcut.
Use .GetLink.Path:
If objItem.IsFileSystem Then
Wscript.Echo objItem.Name, " =>", objItem.GetLink.Path
End If

Create New File From Custom Explorer Bar Button

I want to create a custom button on my Windows Explorer toolbar to create a new blank text document, similar to the "New Folder" button that is already there.
Following these steps, I was able to create my button and get it running a custom VBScript:
Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFile = fso.CreateTextFile(WshShell.CurrentDirectory & "\NewTextDocument.txt", True)
objFile.Close
Wscript.Quit
However, the value of WshShell.CurrentDirectory is C:\Windows\system32. (I think this is because the command being called is wscript.exe which is in that directory.).
How can I get the directory where the Explorer window is opened up to?
--
Somewhat related: I have been getting a "Permission denied" error when I run this script. I was assuming this was because the system32 directory is protected. Are there any other precautions to ensure the script will be allowed to create a file?
Thanks.
You need a different approach.
Use the shell not file system to do what you want.
Here's two sample scripts using the type of objects you need.
'Const NETHOOD = &H14& 'fonts
'Const NETHOOD = &H12& 'Network
Const NETHOOD = &H11& 'My Comp
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(NETHOOD)
Set objFolderItem = objFolder.Self
Wscript.Echo objFolderItem.Path
Set colItems = objFolder.Items
For Each objItem in colItems
For x = 1 to 79
Properties = Properties & vbtab & objFolder.GetDetailsOf(ObjItem, x)
Next
Wscript.Echo objItem.Name" & Properties
Properties=""
Next
and to find right window
Set objShell = CreateObject("Shell.Application")
Set AllWindows = objShell.Windows
For Each window in AllWindows
msgbox window.locationname
Next

How to Copy a file that was read from a list

Hello guys I have an issue or issues with my code above
I'm trying to get "sExtension" to be search in a different folder other that the one I'm using to save my script since this script will be use as a Startup Script on many computers
(It works only if I run the script in the same folder "sExtension", "ExtAssign.txt" and sComputername are otherwise it wont find the path)
This is what it should do
Read a file called "ExtAssign.txt" (There is a full list of computer names in that file) and if it find the computer name on that file then it should copy a file with the with the extension number assigned to that computer name from a file server to "C:\" Drive
For this example I'm trying to do this locally, If I can make it then I'll try it from my File Server
Set objFSO = CreateObject("Scripting.FileSystemObject")
set oFso = CreateObject("Scripting.FileSystemObject")
Set objFS = CreateObject("Scripting.FileSystemObject")
Set fso = CreateObject("Scripting.FileSystemObject")
set oShell = WScript.CreateObject("WScript.Shell")
set oShellEnv = oShell.Environment("Process")
Set folder = Fso.GetFolder("C:\Users\XXXXX\Desktop\Test\Extensions\")
Set wshshell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set ObjEnv = WshShell.Environment("Process")
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
Scomputername = ObjEnv("COMPUTERNAME")
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objWShell = wScript.createObject("WScript.Shell")
Dim strFile
'File to scan
strFile = "C:\Users\XXXXX\Desktop\Test\Extensions\Extassign\ExtAssign.txt"
Dim strPattern
'Look for computer name in file
strPattern = scomputername
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
Dim strLine
'Read each line and store it in strLine
strLine = objFile.ReadLine
'If the line matches the computer name, save the line to ExtArray
If InStr(strLine,strPattern)>0 Then
Dim ExtArray
'Split the line and separate the extension
ExtArray = Split(strLine,"|", -1, 1)
Dim sExtension
'Save the extension to sExtension
sExtension=ExtArray(1)
End If
Loop
'If the sExtension is empty, computer was not found, send message and terminate script.
If sExtension="" Then
WScript.Echo "ERROR: Computer "& scomputername &" not found in Extension Assignment List, so no extension has been set. Avaya will not be launched. Please contact your IT department for assistance."
Else
'If the sExtension contains a number, Copy that file to C:\ and rename it to Config.xml
fso.CopyFile "C:\Users\XXXXX\Desktop\Test\Extensions\ "& sExtension &"", "C:\Config.xml", True
End If
at the end it if it finds the file sExtension it will rename it to Config.xml but it wont do it unless I run the script in the same folder sExtension and sComputername.
I get File not found error
Thank you in advance and Happy new year!
The culprit is most likely this line:
fso.CopyFile "C:\Users\XXXXX\Desktop\Test\Extensions\ "& sExtension &"", "C:\Config.xml", True
There is a trailing space after the last backslash in the path, so you're creating a path
C:\Users\XXXXX\Desktop\Test\Extensions\ 12345
^
when you actually want a path
C:\Users\XXXXX\Desktop\Test\Extensions\12345
On a more general note: why are you creating 7(!) FileSystemObject instances (replacing one of them three times on top of that)? And 3(!) WScript.Shell instances? You don't even use most of them, not to mention that you don't need the Shell object in the first place. You only use it for determining the computer name, which could be done just fine using the WScript.Network object (that you don't use at all).
Also, please don't ever use comments like this:
'Read each line and store it in strLine
strLine = objFile.ReadLine
It's quite obvious that you read each line and assign it to the variable strLine. Comments shouldn't rephrase what you're doing (the code already does that, at least when you're using speaking variable and function names), but why you're doing it, i.e. what the purpose of a particular code section is.
Your code could be reduced to something as simple as this:
Set fso = CreateObject("Scripting.FileSystemObject")
Set net = CreateObject("WScript.Network")
computername = net.ComputerName
foldername = "C:\Users\XXXXX\Desktop\Test\Extensions"
filename = fso.BuildPath(foldername, "Extassign\ExtAssign.txt")
Set f = fso.OpenTextFile(filename)
Do Until f.AtEndOfStream
line = f.ReadLine
If InStr(line, computername) > 0 Then
arr = Split(line, "|", -1, 1)
If UBound(arr) >= 1 Then extension = arr(1)
End If
Loop
f.Close
If IsEmpty(extension) Then
WScript.Echo "ERROR: Computer "& computername &" not found in ..."
Else
fso.CopyFile fso.BuildPath(foldername, extension), "C:\Config.xml", True
End If

VBScript that Opens an ini file and a Config file in notepad

I work in a hospital environment and right now im doing PC deployments. Part of the deployment requires us to view 2 files on a network drive looking for information regarding the old systems. They use specific ports and or TTY's to view information in each department.
I am trying to create a VBS file that can open 2 files in 2 different notepad windows. The first one opens up but the pcview.cfg keeps giving me an error. Im trying to link to the same location that the HBOWEM32 is pointed to. Can anyone solve? For security reasons I have taken out the exact location of the network drive. The code below prompts for a specific folder name which is the old pc name. After entering that data it opens the HBOWEM32 files fine but says it cannot find the other part. I Have manually looked inside the folder and the pcview.cfg file DOES exist. I just want a faster way of opening these rather than brute forcing through the run prompt.
Here is the code.
CONST strDir = "<Netowrk Location)"
Dim WshShell
set objShell = CreateObject("WScript.Shell")
set objFSO = CreateObject("Scripting.FileSystemObject")
function findFolder(strDir, strFlag)
set objFolder = objFSO.GetFolder(strDir)
for each objSubFolder in objFolder.SubFolders
if (inStr(objSubFolder.Name, strFlag)) then
findFolder = objSubFolder.Path
exit function
else
findFolder = findFolder (objSubFolder.Path, strFlag)
end if
next
end function
strFlag = inputBox("Enter Computer Name:")
strWeb = findFolder(strDir, strFlag) & "\HBOWEM32.ini"
objShell.Run strWeb
Set WshShell = CreateObject ("WScript.Shell")
WshShell.Run ("notepad.exe """ + "\\<same location as above>\Pcview.cfg""")
Use Option Explicit
Don't create variables you don't use (WshShell, objShell)
Improve your variable names (strFlag seems to be a computer name, strWeb seems to be the full specification of a file)
Don't lump different info into one variable (strWeb contains the folder path to re-use and the specific file name)
Use diagnostics output (at least while developing)
In code:
Option Explicit
...
Dim strComputer : strComputer = InputBox("Enter Computer Name:")
Dim strFolder : strFolder = findFolder(strDir, strComputer)
Dim strIniFSpec : strIniFSpec = objFSO.BuildPath(strFolder, "HBOWEM32.ini")
WScript.Echo "will run '" & strIniFSpec & "'"
objShell.Run strIniFSpec
Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
Dim strCfgFSpec : strCfgFSpec = objFSO.BuildPath(strFolder, "Pcview.cfg")
Dim strCmd : strCmd = "notepad.exe """ & strCfgFSpec & """"
WScript.Echo "will run '" & strCmd & "'"
WshShell.Run strCmd
(not tested, please be carefull)

Resources