Batch or VBS copy to clipboard - batch-file

I am looking for a way to copy the text "Hello world" to the clipboard using either VBS or batch. I've done a lot of research but couldn't find anything.

As Squashman proposed you can use :
echo string|clip
thought this will set one enter at the end of the string.
To strip the enter you can use this:
mshta "javascript:Code(close(clipboardData.setData('text','string')));"

You can do it with an html object to retrieve the contents of the clipboard:
' Get clipboard text
Set objHTML = CreateObject("htmlfile")
Set Ws = CreateObject("WScript.Shell")
Clipboardtext = objHTML.ParentWindow.ClipboardData.GetData("text")
MsgBox Clipboardtext,vbInformation,"Get Clipboard"
sText = "Hello World"
'Here we set the string sText into Clipboard
Ws.Run "mshta.exe ""javascript:clipboardData.setData('text','" & Replace(Replace(sText, "\", "\\"), "'", "\'") & "');close();""", 0, True

I'm afraid this isn't easily achievable using batch or VBScript.
To access clipboard, you need to use a series of Windows APIs, which is not directly possible with either batch or VBScript. Your best bet could be writing a CLI program (helper program), then call it in yout batch / VBS.

Related

calling bat file using asp classic

Can anyone help me with this problem, i need to call/execute bat file. below are my codes for bat file and asp classic.
test.bat
echo off
start C:\inetpub\wwwroot\Texting\Notification.lnk
That test.bat will run a shortcut and call the application, that will update received file.
sample.asp this code is for classic asp
<%
dim fs,tfile,loc,locadd,loctime1,nameFile,wshell
locadd = Month(Date)&Day(Date)&Year(Date)
loctime1 = stripNonNumeric(FormatDateTime(Now,3))
nameFile = "\\85new\Accts85new\Texting\Reply\Karing\"
loc = "\\85new\Accts85new\Texting\Reply\Karing\"&locadd&"_"&loctime1&".txt"
set fs= Server.CreateObject("Scripting.FileSystemObject")
set tfile=fs.CreateTextFile(loc)
For Each Item in Request.QueryString
tfile.Write Item &";"& Request.QueryString(Item)&"$&$"
next
tfile.close
set tfile=nothing
set fs=nothing
Function stripNonNumeric(inputString)
Set regEx = New RegExp
regEx.Global = True
regEx.Pattern = "\D"
stripNonNumeric = regEx.Replace(inputString,"")
End Function
set wshell = CreateObject("WScript.Shell")
wshell.Run "cmd.exe /c C:\inetpub\wwwroot\Texting\test1.bat"
set wshell = nothing
%>
that sample.asp will get the value of parameters from querystring.
The code is working, no error received when i hit ENTER on the browser.
Thank you in advance!
Typically this should be done in another way, but assuming you understand the security implications of calling bat file from a web application, you need to try to redirect output to a file to ensure it is executed and what is the output, i.e.
wshell.Run "test1.bat>debug.txt"
CreateObject("WScript.Shell") is executed in a separate process and you will not get any error in the browser, that's why your bat file needs to output its result somewhere.

How do i prevent a VBScript from running standalone?

I'm doing a mash between VbScript and CMD, i can call the VBScript easily with
cscript.exe //NoLogo "%~dp0TASK.vbs" >>"%~dp0output.txt"
But I need to disable the feature of users clicking on the VBScript and calling all sorts of errors, rather than it being called through a batch file.
My first attempt was a mess of setting a variable into a text file before i ran cscript.exe and use error handling in VBScript to tell if that variable could be collected, but it added too much time to the script.
Does VBScript have a way to tell whether it was started by CMD, or simply by double clicking, and able to act accordingly?
Here is a simple function, detecting the parent process caption. You can check if the process was started by CMD shell (the caption is cmd.exe) or by double-click (explorer.exe):
If LCase(GetParentProcessCaption()) <> "cmd.exe" Then WScript.Quit
' the rest part of your code here
Function GetParentProcessCaption()
With GetObject("winmgmts:\\.\root\CIMV2:Win32_Process.Handle='" & CreateObject("WScript.Shell").Exec("rundll32 kernel32,Sleep").ProcessId & "'")
With GetObject("winmgmts:\\.\root\CIMV2:Win32_Process.Handle='" & .ParentProcessId & "'")
With GetObject("winmgmts:\\.\root\CIMV2:Win32_Process.Handle='" & .ParentProcessId & "'")
GetParentProcessCaption = .Caption
End With
End With
.Terminate
End With
End Function
In the context of your question another method allowing to pass parameters from CMD shell process to WSH script child process may be useful. It uses environment variable and WScript.Shell object. Consider the below example.
There is code for task.cmd file:
set myvar=myvalue
wscript "%~dp0task.vbs"
And for task.vbs file:
WScript.Echo CreateObject("WScript.Shell").Environment("process").Item("myvar")
I have got the output as follows:
Note, process environment variables are accessible for child processes only.
One way is for your VBS file to check for the presence of parameters and if they do not exist then stop the execution.
In your VBS script:
If WScript.Arguments.Count = 0 then
' No parameters provided. Can stop here.
End If
When you call your VBS file, just passing any parameter will satisfy the condition:
REM This will work.
cscript.exe //NoLogo "%~dp0TASK.vbs" "hello world"
REM So will this.
cscript.exe //NoLogo "%~dp0TASK.vbs" 1 2 3 4
REM This will not.
cscript.exe //NoLogo "%~dp0TASK.vbs"
This will not stop people from running it manually (with a parameter) or creating a shortcut which has a parameter. It would only really stop running the VBS directly (as a parameter will not be passed).
When you double click on a .vbs file, the action is determined by the following registry key:
Computer\HKEY_CLASSES_ROOT\VBSFile\Shell\Open\Command
If you were to change the key, you will be changing the double click action, but you will not be affecting your ability to launch the command explicitly via invoking cscript.exe directly.
If the bat file will keep the cmd.exe open while the vbs file runs, you can try to detect the cmd process inside the vbs file to continue execution.
Put this at the start of your vbs file:
Set shell = CreateObject("WScript.Shell")
list_str = shell.Exec("tasklist").stdOut.ReadAll 'get a list of processes by calling the windows program 'tasklist.exe'
If InStr(list_str, "cmd.exe") = 0 Then WScript.Quit 'quit if process is not found

Launch two Explorer windows side-by-side

Is there a way to launch two Explorer windows side-by-side (vertically tiled) with a Batch script?
If not, how might I do this with VBS?
I have modified the VBS script above by Hackoo to do exactly what the OP wants...
The comments in the script explain exactly what it will do.
If the two windows don't set into correct position, increase the 'Sleep' time and try again.
If you want a horizontal split, use 'objShell.TileHorizontally'.
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Launches two Explorer windows side-by-side filling the screen dimensions.
''' Minimizes all current open windows before launch; if this is not done,
''' the current open windows will also be resized along with our two windows.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim Calc,AppData,objShell
Calc = "%windir%\system32\calc.exe"
AppData = "%AppData%"
Set objShell = CreateObject("shell.application")
objShell.MinimizeAll
Call Explore(Calc)
WScript.Sleep 800
Call Explore(AppData)
WScript.Sleep 800
objShell.TileVertically
Set objShell = nothing
'*****************************************************
Function Explore(Path)
Dim ws
set ws = CreateObject("wscript.shell")
Explore = ws.run("Explorer /n,/select,"& Path &"")
End Function
'*****************************************************
This might be in the same category as your question. :)
How can a batch file run a program and set the position and size of the window?
Unfortunately it seems that its not possible without any external third part software in batch. Probably easier in VBS - if so the answer should be in the link.
Try this code :
Option Explicit
Dim Calc,AppData
Calc = "%windir%\system32\calc.exe"
AppData = "%AppData%"
Call Explore(Calc)
Call Explore(AppData)
'*****************************************************
Function Explore(Path)
Dim ws
set ws = CreateObject("wscript.shell")
Explore = ws.run("Explorer /n,/select,"& Path &"")
End Function
'*****************************************************

Creating a Message Box that runs a Specified Program (vbs)

Take the following script that I have:
x=msgbox ("Do you want to recycle the Premiere Pro Media Cache?" ,4, "Recycle Premiere Pro Media Cache")
If box =6 Then
CreateObject("wscript.shell").run "C:\Expedited\Scripts\PrMCRecycler1"
End If
My goal is to get this VBS file (which brings up a message box) to run the batch file (the same way it would run when double-clicking it) when the yes button is pressed. I'm not sure what I'm doing wrong above. When I click No, nothing needs to happen, so I didn't specify anything for it.
Basically, since it brings up a yes/no message box, I just need to make the yes button execute the specified batch file. I could really use some assistance in figuring out what's wrong. When I try the code listed above, nothing happens upon choosing yes (besides the dialogue box going away).
Try this example and change the path of your batch file.
Option Explicit
Dim ws,Question,PathProgram
Set ws = CreateObject("wscript.shell")
'change the path of your batch file
PathProgram = "C:\Program Files\Internet Explorer\iexplore.exe"
Question = Msgbox("Do you want to recycle the Premiere Pro Media Cache?",VbYesNO + VbQuestion, "Recycle Premiere Pro Media Cache")
If Question = VbYes Then
ws.run DblQuote(PathProgram)
End If
'***************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'***************************************

How to find the percentage value from file using batch file

html file with content
Coverage by assemblyPOroject A90%
generated using Partcover
Please help me find the percentage value i.e 90% using the batch file
>find "%" name.txt
---------- NAME.TXT
Coverage by assemblyPOroject A90%
>set string=Coverage by assemblyPOroject A90%
>echo %string%
Coverage by assemblyPOroject A90%
>echo %string:~30,2%
90
If you find that the batch language isn't powerful enough to do what you want - and it likely won't take you too long to get to that point - you can use the Windows PowerShell. This isn't installed by default on all versions of Windows, but you can download it free of charge.
If you don't like the PowerShell language, there's Python/Perl...
You can use vbscript (not tested)
Set objFS=CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfLine
strLine= objFile.ReadLine
s = Split(strLine," ") 'save each word into array
' display
For i=0 To Ubound(s)
If InStr(s(i),"%") > 0 Then
WScript.Echo s(i)
End If
Next
Loop
For more information on how to use vbscript, download the manual and go through it

Resources