Is there any way to do this via VBScript?
I tried this but it mute speaker.
CreateObject("WScript.Shell").SendKeys(chr(173))
Edit:
I solve the problem via add three more shell.SendKeys"{TAB}"
set shell = CreateObject("WScript.Shell")
shell.run"Sndvol"
WScript.Sleep 1500
shell.SendKeys"{TAB}"
shell.SendKeys" "
shell.SendKeys"%{F4}"
Final code is:
set shell = CreateObject("WScript.Shell")
shell.run"Sndvol"
WScript.Sleep 1500
shell.SendKeys"{TAB}"
shell.SendKeys"{TAB}"
shell.SendKeys"{TAB}"
shell.SendKeys"{TAB}"
shell.SendKeys" "
shell.SendKeys"%{F4}"
It would be nice if there was an easier way to do this anyway.
Related
I have a .bat file which I use to back up files, which calls a .vbs and passes it two parameters, as follows:
...
ZipCMD.vbs "C:\Source" "C:\Destination\Data.zip"
...
ZipCMD.vbs contains the following code (Credit to garbb):
Set objArgs = WScript.Arguments
Set FS = CreateObject("Scripting.FileSystemObject")
InputFolder = FS.GetAbsolutePathName(objArgs(0))
ZipFile = FS.GetAbsolutePathName(objArgs(1))
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set objShell = CreateObject("Shell.Application")
Set source = objShell.NameSpace(InputFolder).Items
numfolderitems = objShell.NameSpace(InputFolder).Items.count
objShell.NameSpace(ZipFile).CopyHere(source)
' wait until number of items in zip file is the same as in the folder we are zipping up
' also sometimes gets errors when getting folder object for zip file, probably because it is in use? so ignore these
On Error Resume Next
Do while True
numitemsinzip = objShell.NameSpace(ZipFile).Items.count
If Err.Number = 0 and numitemsinzip = numfolderitems Then
Exit Do
ElseIf Err.Number <> 0 then
Err.Clear
End If
wScript.Sleep 10
Loop
On Error Goto 0
When the zipping is occurring, the usual windows 'Compressing files' interface appears, and shows the progress bar ticking along for a few minutes, before closing and disappearing.
Question: Can vbs run a compression silently (i.e. without interface)? -- I've read this article, which shows a flag, however this doesn't appear to work with copying to .zip, for some reason.
Follow-up question: If it's not possible for the .vbs which I'm using to achieve this, then is there an alternative way, which still utilises calling another file/process(?) (.vbs / .js, or other?) and feeding it the two paths from cmd?
Edit: I'm trying to achieve this without the use of third-party software (e.g. 7zip), and simply using native windows code.
Suppose I am almost 3 months late on this, but if you have powershell version 5 or later, you can simply create a powershell script:
Compress-Archive "C:\Source" "C:\Destination\Data.zip"
or from a batch file:
powershell Compress-Archive "C:\Source" "C:\Destination\Data.zip"
Also see this option
I am trying to use vbs to capture an ID inside an input box and then have that entered into the opened file. I've only learned how to write vbs script for this specific project, so what is written below is probably not kosher. I'm also not sure if using Sendkeys is the way to go, mostly because it hasn't worked yet. Thanks for any pointers.
Dim wshShell, ID
ID=inputBox("Please Enter the ID")
CreateObject("WScript.Shell").Run("file.sps")
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Sendkeys "[the ID from above should go here]"
WshShell.Sendkeys "{Enter}"
Avoid SendKeys like the plague.. its rarely the best way to do something. You just need to use the FileSystem Object:
Option Explicit
Dim fso,file
Dim id
id = inputBox("Please Enter the ID")
Set fso = CreateObject("Scripting.FilesystemObject")
'open the file for appending (8) create it if it doesn't exist
Set file = fso.OpenTextFile("file.sps",8,True)
Call file.writeLine(id)
Call file.close
Set file = Nothing
Set fso = Nothing
WScript.Quit
Resolved: All I had to do was add a sleep to the loop.
I have a text file which contains usernames and passwords which need to be logged in. I'm attempting to make a VBScript file to automate this process. The text file's syntax is
username
password
username
password
etc etc
and is a total of 738 lines.
The code I currently have is
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\Jacob\Desktop\logins.txt", ForReading)
Set WshShell = WScript.CreateObject("WScript.Shell")
Const ForReading = 1
Dim strUsername
Dim strPassword
wshshell.AppActivate "Notepad"
Do Until objFile.AtEndOfStream
strUsername = objFile.ReadLine
strPassword = objFile.ReadLine
wshshell.sendkeys strUsername
wshshell.sendkeys "~"
wshshell.sendkeys strPassword
wshshell.sendkeys "~"
Loop
which I'm using simply to make sure it works by having it write to a blank text file. (Later notepad will be replaced by the login executable, I already know that part works).
The code works fine until it hits line 221 of the text file, where it breaks. It exports a bunch of random garbage and then stops.
Any help is greatly appreciated.
Edit:
Here is the garbage text which comes at the end of the written text file. You can see that there are still remnants of what should come out.
.[redacted username].22bssddd2222ent000.2wlllkarsstt00....000h8b6b99b88uub2
.....ttttte0000mp00...gh.rrlll00225..gbu
.....0022sdg
[redacted password]
.proeflussssddd
..22le.pu6....225500s.gl.iteee.55.uuu.hmadttn..0dl
tsssssbeeee0000nsssss0000000ssssdssdbuuuz22dddd000buuu.jj2222
b4....3
.j00000000b
y.
bb.ddddssgg.2222ssgg05uuudbeeeeen......hh00222l11.....tdddd.nn
00000
ll0ssssau0s000ssssss
bbddddlll.ttttl......s
be
.o222
b.ddddddutttt
ll000buc11e000000utttt
bbb.eeeeee0000seeeee5
mmssss000000unnnttttnnn
bb000..s.dsssssss.fa000000
2
.....k000.4tnnn
.....bbbseeeu
.n.2te....s
7e9bbmv00000000tttteeee.mttseetnn2222222r0000000000.u.ddd0000000.111ddddge11111ssddddoo000.ssssssnnnn.uutnnn772
.bbg.......66teeb000000ee1114lrd00002222nsp
Two things you can try...
Try adding a call to WScript.Sleep 100 inside your loop after your call to SendKeys.
Instead of using AppActivate and SendKeys, you could try it where the script just writes the usernames and passwords to another file. This would rule out if the problem is being caused by SendKeys. Using SendKeys with this much data is fairly dangerous anyways. If anything at all goes wrong with Notepad getting focus and the keys start getting directed to another application it could have some very, very unintentional results.
Here is a modified copy of your script to write to a text file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("logins.txt", ForReading)
Set objFile2 = objFSO.OpenTextFile("logins2.txt", ForWriting, true)
Set WshShell = WScript.CreateObject("WScript.Shell")
Const ForReading = 1, ForWriting = 2
Dim strUsername
Dim strPassword
Do Until objFile.AtEndOfStream
strUsername = objFile.ReadLine
strPassword = objFile.ReadLine
objFile2.Write strUsername & vbCrLf
objFile2.Write strPassword & vbCrLf
Loop
A couple of stylistic notes as well:
Define constants before you use them, preferably at the very top of the script. This keeps you and other from having to hunt them down later or thinking they haven't already been defined. It's not a big of a problem in a small script like this, but if you scripts starts getting bigger it could cause you headaches down the road.
Be consistent with Dim's. You defined strUsername and strPassword but you didn't define objFSO, objFile, or WshShell. I always recommend using Option Explicit to force you to define variables before using them. It helps you keep from making spelling mistakes and saves you a lot of headache in the long run.
I am trying to modify all shortcuts on a computer. The script works fine but every now and then throws an error that the .Target property of an object is not available. Since my query only looks up files with a .lnk extension, this should never be the case. (For more details on this error you can see MS docs here: http://technet.microsoft.com/en-us/library/ff406382.aspx#H25)
The script in question:
strComputer = "."
Set wshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile WHERE Extension = 'lnk' AND Drive = 'C:'")
For Each objFile in colFiles
If InStr(1, ucase(objFile.Target), "METER.EXE") Then
Set objShortcut = wshShell.CreateShortcut(objFile.Name)
Wscript.Echo "FIXING: " & objShortcut.TargetPath
End If
Next
For the curious: The purpose of this script is to fix dozens of shortcuts on our lab machines which were previously modified to support a "home-grown" licensing/metering application. In all cases the original .EXE path was stripped from the target but can still be found from the shortcut's icon path.
Thanks
EDIT: The complete error message. It seems to appear more often after a restart, but not once I have run the script 2-3 times.
Microsoft VBScript runtime error: Object doesn't support this property
or method: 'objFile.Target'
CIM_DataFile doesn't have the Target property.
I believe you meant to use Win32_ShortcutFile instead.
I need to use command line switches to execute the 'Save as Text' command. Ideally, I want to:
use a command line switch to open a PDF
use a command line switch to convert the PDF to a text file by mimicking the 'Save as Text' command.
use a command line to close the PDF.
Is this possible? If so, then does anyone know how to do this?
Maybe you can try this: https://github.com/luochen1990/nodejs-easy-pdf-parser
It is a npm package and you need to install nodejs (and npm) to use it.
It can be used as a command line tool:
npm install -g easy-pdf-parser
pdf2text test.pdf > test.txt
And this tool will sort text lines by their y coordinates, so it works great at most case. And it also works well with unicode and cross platform.
Don't use CMD; use AutoIt. Very easy to do and takes a few lines
Run("file.pdf")
winwait("Adobe")
send(?);; whatever commands necessary to save as text
send("{enter}")
send("!{F4}")
I don't understand why you'd not want to use free software (not freeware), pdftotext is the ideal solution.
However, if you just want to actually open and save the PDF in an automated fashion using the Windows GUI, you could use vbscript and the sendkeys command.
Just use pdftotext though, it would be much more reliable and won't cost you a whole box.
I think the below VBscript should do the trick. It will take all .pdf files in a given folder location and save them as .txt files. One major bummer is it only works if your machine is not locked since it uses the SendKeys command. If anyone has a solution that works while a computer is locked, please send it my way!
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "PATH_OF_ALL_PDFS_YOU_WANT_TO_CONVERT_HERE"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile In colFiles
extension = Mid(objFile.Name, Len(objFile.Name) - 3, 4)
file = Mid(objFile.Name, 1, Len(objFile.Name) - 4)
fullname = objFSO.BuildPath(objStartFolder, objFile.Name)
fullname_txt = objFSO.BuildPath(objStartFolder, file + ".txt")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If extension = ".pdf" And Not objFSO.FileExists(fullname_txt) Then
WScript.Echo fullname
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run """" + fullname + """"
WScript.Sleep 1000
WshShell.SendKeys "%"
WScript.Sleep 100
WshShell.SendKeys "f"
WScript.Sleep 100
WshShell.SendKeys "h"
WScript.Sleep 100
WshShell.SendKeys "x"
WScript.Sleep 300
WshShell.SendKeys "{ENTER}"
count = 0
'this little step prevents the loop from moving on to the next .pdf before the conversion to .txt is complete
Do While i = 0 And count < 100
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.OpenTextFile(fullname_txt, 8)
If Err.Number = 0 Then
i = 1
End If
count = count + 1
WScript.Sleep 20000
Loop
End If
Next