Uploading files to One Drive using Batch Script - windows

#if (#CodeSection == #Batch) #then
#echo off
rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"
START CHROME "https://onedrive.live.com/about/en-us/signin/"
rem the script only works if the application in question is the active window. Set a
timer to wait for it to load!
timeout /t 5
rem use the tab key to move the cursor to the login and password inputs. Most htmls
interact nicely with the tab key being pressed to access quick links.
rem %SendKeys% "{TAB}"
rem now you can have it send the actual username/password to input box
%SendKeys% "xx#xyz.com"
%SendKeys% "{TAB}"
.
.
timeout /t 5
%SendKeys% "{DOWN}"
%SendKeys% "{ENTER}"
timeout 30
goto :EOF
#end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
I am using this script to open OneDrive on a browser and trying to upload a file. I am using a fixed number of tabs to select the file in the file upload dialog box. However, everytime the script runs the cursor ends up in a different place than the file. Sometimes it works as it should. Is there a way to ensure it always ends up selecting the file or is it just not possible?

Related

Batch File calling Console Application - Leaves CMD window open

I am calling a C# Console Application via batch file, in order to send the application output into a text file, with the date/time etc.
The problem I have is that when the console application completes, it leaves the batch window open, because there is a PAUSE (the C# equivalent), so a key must be pressed for the window to close. This means I do not know when the job has finished.
Is there a way I can make the CMD window close when the application finished, without having to change the C# Application code?
#ECHO================================================================================
#ECHO The Application is currently running and may take some time. Please wait...
#ECHO================================================================================
#ECHO OFF
C:\Applications\Job\Job.exe > C:\Applications\Job\Job_Output\"Output_%date:/=-% %time::=-%.txt"
Try this (note the collated dot after echo):
echo.| C:\Applications\Job\Job.exe > C:\Applications\Job\Job_Output\"Output_%date:/=-% %time::=-%.txt"
I have tried with pause and it works well:
echo.| pause
echo. is not echo. It just prints a newline, just what you need to trigger the pause.
Not sure whether will it work if your console app already have a Console.ReadLine() or Console.ReadKey() method but instead of just calling the *.exe use the Start command which will run the executable in a separate window like
start "MyConsoleTask" C:\Applications\Job\Job.exe > C:\Applications\Job\Job_Output\"Output_%date:/=-% %time::=-%.txt"
If you have not access to the console app source code, you may try a workaround
#echo off
#echo================================================================================
#echo The Application is currently running and may take some time. Please wait...
#echo================================================================================
set "timeStamp=%date:/=-%_%time::=-%
set "timeStamp=%timeStamp:~0,-3%" & rem remove ,centiseconds.
set "logFile=C:\Applications\Job\Job_Output\Output_%timeStamp%.txt"
rem start the exe in the same cmd window
start /B "" """C:\Applications\Job\Job.exe" > "%logFile%"""
rem wait for process startup
ping 1.1.1.1 -n 1 -w 750 >NUL
rem wait for logFile to be closed. This may flag that job.exe has ended
:wait
ping 1.1.1.1 -n 1 -w 50 >NUL & rem this avoids processor load
2>nul (>>"%logFile%" call )||goto :wait
rem send a key to the console. This may be captured by the exe file
set "_vbs_file_=%TEMP%\sendConsole.vbs"
(
echo/ set oWS ^= CreateObject^("wScript.Shell"^)
echo/ wScript.Sleep 50
echo/ oWS.SendKeys "{ENTER}"
)>"%_vbs_file_%"
if exist "%TEMP%\sendConsole.vbs" (set "_spawn_=%TEMP%\sendConsole.vbs") else (set "_spawn_=sendConsole.vbs")
ping 1.1.1.1 -n 1 -w 50 >NUL
start /B /WAIT cmd /C "cls & "%_spawn_%" & del /F /Q "%_spawn_%" 2>NUL"
#echo================================================================================
#echo Process completed. I guess...
#echo================================================================================
exit/B
so,
start /B ...
starts the job.exe executable in the same cmd window.
:wait
ping 1.1.1.1 -n 1 -w 50 >NUL & rem this avoids processor load
2>nul (>>"%logFile%" call )||goto :wait
waits until logfile is closed, so it may indicate that the previous proccess has ended.
set "_vbs_file_=%TEMP%\sendConsole.vbs"
(
echo/ set oWS ^= CreateObject^("wScript.Shell"^)
echo/ wScript.Sleep 50
echo/ oWS.SendKeys "{ENTER}"
)>"%_vbs_file_%"
if exist "%TEMP%\sendConsole.vbs" (set "_spawn_=%TEMP%\sendConsole.vbs") else (set "_spawn_=sendConsole.vbs")
ping 1.1.1.1 -n 1 -w 50 >NUL
start /B /WAIT cmd /C "cls & "%_spawn_%" & del /F /Q "%_spawn_%" 2>NUL"
send the enter key to the console, so the process waiting a keystroke may capture it.
NOTE: the ping wait trick works fine only if the IP is unreachable.
NOTE: the call and/or goto trick is discussed here
we gotta simulate a key press here, therefore we should toy with the keyboard buffer.
I am no Batch expert and this is the answer I found searching how to press keys with a batch:
#if (#CodeSection == #Batch) #then
#echo off
set SendKeys=CScript //nologo //E:JScript "%~F0"
rem Open the command here
start "" /B Job.exe > JobOutput.txt
rem sends the keys composing the string "I PRESSED " and the enter key
%SendKeys% "I PRESSED {ENTER}"
goto :EOF
#end
// JScript section
WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));
source:
Press Keyboard keys using a batch file
GnuWin32 openssl s_client conn to WebSphere MQ server not closing at EOF, hangs

Chrome Extension - Message Passing to batch file

I used the sample project
https://developer.chrome.com/extensions/samples
I am able to run the python native app.
Is there any way to get the message within native-messaging-example-host.bat
I don't want to load python script
What I want to do here is
send message from chrome {text: "xyz.bat"}
and the batch file should run START xyz.bat
You should not approach this problem from the batch file standpoint, as in lieu of my solution, it requires the program to be run upfront, which in most applications is depreciated in favor of running it in the background. However if you still want to know how you could potentially do it in batch...
If you can pass the message to a blank html page (not currently sure how you can or want to do it this way), where the only thing on that html page is your runme.bat we can run a program that would copy the page, open a text file and paste it inside, close the text file, and run the batch with input from it. So code wise,
#if (#CodeSection == #Batch) #then
#echo off
set SendKeys=CScript //nologo //E:JScript "%~F0"
rem below copys everything on the page, and closes it
%SendKeys% "{TAB}"
%SendKeys% "^{A}"
%SendKeys% "^{C}"
%SendKeys% "^{W}"
rem open text file,wait for it load, paste clipboard, save and exit
start newreadforme.txt
timeout /nobreak /t 5
%SendKeys% "^{V}"
%SendKeys% "^{S}"
timeout /nobreak /t 2
%SendKeys% "^{W}"
start program.bat
goto :EOF
#end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
then in your batch file
#echo off
set /p x=<newreadforme.txt
start %x%
This code will run simulated keystrokes on the opened page to copy its contents and relay to a text file to be referenced from another batchfile. But you should only use this method as a last resort as this approach is a TERRIBLE way to solve your problem. My code requires you to keep the webpages open and upfront and make sure no one interferes with the program during its execution. So if a user is using the computer the time its running, then they may accidentally mess with the inputs.
On top of that, already you need to be modifying webpages to achieve your end result so you probably should use a language that supports html to filesystem operations. Nodejs can provide a nice interface between the file system and html pages that you may decide to pass. How you handle the webpage filled with the message i am not sure of but you should most certainly avoid using batch to do what you ask in favor of more html friendly languages

Automate keypress input for MATLAB program

I have an existing MATLAB program which after a keystroke displays an image on the left or right of the screen. The user then indicates via arrow key if the image is on the left or right. The program actually does a lot of other things, but that's the gist. This is for a WinXP computer using MATLAB 2008A.
I would like to do a large number (thousands) of iterations for this program in an attempt to determine why some computers running this program have dying graphics cards. I've already made a number of improvements to simplify existing code and reduce computational time, but I need a to test and show the improvement. GPU-Z records all the hardware variables I'm interested in for now, so I need a keypress script capable of supplying input to the MATLAB program automatically. Ideally, this would be a stand alone macro capable of being run completely independently.
After reading this question, I attempted to modify the batch script as follows:
#if (#CodeSection == #Batch) #then
#echo off
rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"
rem Start the other program in the same Window
start "" /B cmd
%SendKeys% "echo off{ENTER}"
set /P "=Wait and send a command: " < NUL
ping -n 100 -w 1 127.0.0.1 > NUL
%SendKeys% "2"
set /P "=Wait and send a command: " < NUL
%SendKeys% "3"
ping -n 30 -w 1 127.0.0.1 > NUL
%SendKeys% "2"
set /P "=Wait and send a command: " < NUL
%SendKeys% "1"
goto :EOF
#end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
In testing, the batch code seems to appropriately enter keystrokes into the active window (cmd, text files, browsers, etc). However, if I attempt to run my MATLAB program simultaneously, the batch execution stalls until the MATLAB program ends. Obviously this is useless to me in the current form.
I'd appreciate any suggestions on where to go from here. Is batch scripting even a viable solution for this kind of automation? If not, any suggestions? I'd prefer to stay away from 3rd party programs as much as possible, but if it's the easiest solution I'll consider it.

VBS (Visual Basic Script) Run Program hidden/invisible

I am trying to run a program (In this case Internet Explore) hidden/invisible from a VB script.
I found a simple script for making batch files hidden, and tried it. It didn't seem to work as the program just popped up as normal.
Here is my code so far:
CreateObject("Wscript.Shell").Run "iexplore.exe",0,True
This runs the program iexplore.exe, but doesn't run it hidden/invisible.
I am also running this VBS file from a batch file which is hidden.
The batch file simply does:
start Run.vbs
Codes of each script/batch file:
Batch File: Main file launching VBS file
#echo off
:start
start HideExecuteServerVBS.vbs (To Hide the ExecuteServerVBS.bat file when running)
timeout /NOBREAK /T 5
TASKKILL /IM iexplore.exe
timeout /NOBREAK /T 3
TASKKILL /IM iexplore.exe /F
timeout /NOBREAK /T 1800
goto start
HideExecuteServerVBS.vbs
CreateObject("Wscript.Shell").Run "ExecuteServerVBS.bat",0,True
ExecuteServerVBS.vbs
#echo off
C:\Windows\sysWOW64\csript.exe C:\Users\Admin\RunInternetProcess\vbscript.vbs
vbscript.vbs
Set ie = CreateObject("InternetExplorer.Application")
Is there a possible way to run a program invisible through a VB Script (Visual Basic Script)?
So here's the deal, if you are receiving an ActiveX error, you most likely are trying to run this vbscript under a server. Server with a 64bit platform with lack of support for direct execution of 32bit vbscripts? Yeah? If so, here's what you need to do.
Make a batch file:
ExecuteServerVBS.bat
C:\windows\sysWOW64\cscript.exe C:\path\to\your\vbscript.vbs
Put your vbscript code here:
vbscript.vbs
Set ie = CreateObject("InternetExplorer.Application")
'Go crazy
And BOOM. You're done.
UPDATE
update the file ExecuteServerVBS.vbs
#echo off
C:\Windows\sysWOW64\cscript.exe C:\Users\Admin\RunInternetProcess\vbscript.vbs > errorlog.log
update the file vbscript.vbs
On Error Resume Next
Dim ie
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
'Perform IE functions here......
If err.number <> 0 then wscript.echo err.number & ":" & err.description
You need to load it this way instead:
Set ie = CreateObject("InternetExplorer.Application")
' To make visible, uncomment the following line...
'ie.Visible = True
Have a look at these methods:
http://ss64.com/vb/run.html
http://ss64.com/vb/exec.html
http://ss64.com/vb/shellexecute.html
http://ss64.com/vb/syntax-elevate.html

Sendkeys from command prompt

Is there a way to use sendkeys (or something equivalent) from (not to) the command prompt?
Here is a one line solution:
This line will type "Testing 123" and then hit Enter.
echo >script.vbs set shell = CreateObject("WScript.Shell"):shell.SendKeys "Testing 123{ENTER}" & script.vbs
You can use vbscript. For example, this script will mute the speakers.
set shell = CreateObject("WScript.Shell")
shell.run"Sndvol"
WScript.Sleep 1500
shell.SendKeys"{TAB}"
shell.SendKeys" "
shell.SendKeys"%{F4}"
You launch it from the console with
cscript mute.vbs
More infos here
Without creating temporary files.
The 'loop' was shown only so that you could open a notebook for output. Remove it after.
#echo off
:loop
::------------begin main code------------
set command=new ActiveXObject('WScript.Shell').SendKeys('WoW{ENTER}{ENTER}');
for /f "delims=" %%i in ('mshta "javascript:%command%close(new ActiveXObject('Scripting.FileSystemObject'));"') do set "var=%%i"
::-------------end main code-------------
timeout /t 1 /nobreak >nul
goto :loop

Resources