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.
Related
This question already has an answer here:
How to get a path with the variable user in VBscript
(1 answer)
Closed 3 years ago.
Basically, the code im looking for is how to find username with a variable
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile "C:\Users\%username%\Desktop\test.txt"
I tried with other lines of code to find out the username. But nothing worked
when i typed out the code i wrote with other lines of code to figure out the %username% variable, it doesn't work.
Please respond with new lines of code attached to my previous code here. (I'm new to this so it would make it confusing if you responded with unattached lines of code.)
You should write it like that :
strUser = CreateObject("WScript.Network").UserName
wscript.echo strUser
And to continue with your code like that :
Dim fso,strUser
Set fso = CreateObject("Scripting.FileSystemObject")
strUser = CreateObject("WScript.Network").UserName
'wscript.echo strUser
fso.CreateTextFile "C:\Users\"& strUser &"\Desktop\test.txt"
Also,in VBScript you can get the path to the current user's desktop folder via the SpecialFolders collection:
WScript.Echo CreateObject("WScript.Shell").SpecialFolders("Desktop")
And your code can be written like that too :
Option Explicit
Dim fso,DesktopFolder
Set fso = CreateObject("Scripting.FileSystemObject")
DesktopFolder = CreateObject("WScript.Shell").SpecialFolders("Desktop")
fso.CreateTextFile DesktopFolder &"\test.txt"
I need create one script for open one file (Server.txt → content all servers names) and create logfile with ServerName (example: Server1.txt). After that I need WriteLine in this logfile result of the script retrieve registry values.
I have one script working retrieve all registry values but I need to create each FileLog with ServerName.
I think cannot use two Opentextfile before close one.
Can we help me?
This is code I'm using for test:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("Servers.txt")
Do Until objFile.AtEndOfStream
strComputer = objFile.ReadLine
Set objFile2 = objFSO.CreateTextFile(strComputer & ".txt",True)
set objHtml=objFSO.OpenTextFile(objFile2,8,true)
objHtml.WriteLine Now() & VbTab & RegResultQuery
objHtml.Close
Loop
You can call OpenTextFile twice, but you can't open the same file twice without closing it first. From what I see you don't need to open the file twice, though. CreateTextFile already returns a handle to the file, so you can simply use objFile2 throughout the loops.
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
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