Use Sendkeys to write variables into file - vbscript

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

Related

Multi line vbs auto typer from .txt file [duplicate]

This question already has answers here:
Single Line Input VBS Autotyper
(2 answers)
Closed 2 years ago.
So i have been working on a troll terminal and now i want to type out the entire bee movie script or whatever i put in a text file.
What i need help with is taking the text(with enters and spaces) and fully typing it out.
Another way to say it is i want to take a text file and copy it threw typing it out.
I couldnt find any decent code to do this wich is why i'm asking here.
Also if possible make it type anything that you put in the text file.
(I have no clue why it doesnt let me post this)
Idea: I might know another way around but i dont know how to get it done.
Maby there is a way to infinetly generate variables for each line and type them out like that.
Other idea: I might be able to make a tool to let me quickly hard code it.
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\yourfirstfile", 1)
content = file.ReadAll
Const fsoForAppend = 8
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Open the text file
Dim objTextStream
Set objTextStream = objFSO.OpenTextFile("C:\yoursecondfile", fsoForAppend)
'Display the contents of the text file
objTextStream.WriteLine content
'Close the file and clean up
objTextStream.Close
Set objTextStream = Nothing
Set objFSO = Nothing
Set wshShell = CreateObject("Wscript.Shell")
wshShell.run "C:\yoursecondfile"
Tell me if it works
Yes i m self anwsering.
This script works but it double taps enter i will update it if i figure out how to stop that from happening.
Set wsh=WScript.CreateObject("WScript.Shell")
sub custom_text_typer_confirm
Set objFileToRead =WScript.CreateObject("Scripting.FileSystemObject").OpenTextFile("Custom_Text_Typer.txt",1)
strFileText = objFileToRead.ReadAll()
objFileToRead.Close
custom_typer_wait = inputbox("How long should the program wait untill it will start typing?", "Eclips-Terminal Custom Text Typer")
custom_typer_wait1=custom_typer_wait * 1000
Wscript.sleep custom_typer_wait1
wsh.sendkeys (strFileText & "{enter}")
Wscript.sleep 1000
call home
end sub

open and write 2 files

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.

File wont move to folder vbs

So im writing a script that drops a file folder then moves that file to the folder it dropped it self. Well the folder drops fine but the file wont move. Can some see whats wrong with my code? Or give me a better way to move the file. I also get no error message about trying to move the file.
Dim folder,fso,filsys,C
Set fso = CreateObject("Scripting.filesystemObject")
Set folder = fso.GetSpecialFolder(1)
Set wshshell = CreateObject("wscript.shell")
Set filesys = CreateObject("scripting.filesystemobject")
Set objfso = CreateObject("Scripting.filesystemObject")
Set c = fso.GetFile(Wscript.scriptFullname)
On Error Resume NEXT
Set objFolder = objFSO.CreateFolder("C:\55egr932ntn7mk23n124kv1053bmoss5")
If Err.Number<>0 Then
End If
WScript.Sleep 3000
C.Move ("C:\552ntn7mk23n124kv1053bmoss5\File.exe") (folder&"\File.exe")
And I have a program I use that turns the VBS into and EXE so you see the "file.exe" which really is the .VBS itself
I'm not familiar with this syntax, but the line below looks like it's expecting the folder variable to be a string.
C.Move ("C:\552ntn7mk23n124kv1053bmoss5\File.exe") (folder&"\File.exe")
Earlier in code it looks as though you're setting folder as an object.
Set folder = fso.GetSpecialFolder(1)
You might not get the error you mentioned in your comment if you convert folder to a string.
~~
Another thing to try is the following code:
Set fso = CreateObject("Scripting.filesystemObject")
Set folder = fso.GetSpecialFolder(1)
Alert (folder&"\File.exe")
(I'm not sure if it's "Alert" or "Msgbox" or something else.) That test will show you whether the file path makes sense. If you get an error on line 3 of that test, try converting folder to a string before your Alert (or Msgbox).

Reading From Text File breaks midway

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.

Using Command Line Switches to Save a PDF as Text - Can it be done?

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

Resources