In Windows 7, ultimately I want to bind a shortcut key (Ctrl+Alt+D) to spitting out a date-time stamp of the form 20120913 1232.
The step I'm getting hung up on is having a batch file write anything to my text cursor. Currently I'm piping it to clip.exe, then I paste, but I'd like to eliminate the middle step of the clipboard. If this isn't possible, is there another way around my problem?
echo %date:~-4%%date:~4,2%%date:~7,2% %time:~0,2%%time:~3,2% | clip
You can't do it from cmd. You need to use VBS, Js or PowerShell. Here's a simple VBS script that does what you want. Just create a shortcut on the desktop that runs this VBS file and assign a shortcut key to it
Set WshShell = WScript.CreateObject("WScript.Shell")
' Switch back to the previous window. Use Alt+Esc because Alt+Tab sometimes doesn't work
' WshShell.SendKeys "%{TAB}", True
WshShell.SendKeys "%{ESC}", True
' Get the time string
Dim dt, datetime_str
dt = now
datetime_str = Year(dt) & Right("0" & Month(dt), 2) & Right("0" & Day(dt), 2)
datetime_str = datetime_str & " " & Right("0" & Hour(dt), 2) & Right("0" & Minute(dt), 2)
' Send the date string to the target window
WshShell.SendKeys datetime_str
However Autohotkey (AHK) would be a much better solution and works much faster. Here are 2 example functions that will type the datetime string to the current app
; Map Ctrl+Alt+T to send the date string
^!t::
FormatTime, currentdate,, yyyyMMdd HHmm
SendInput %currentdate%
Return
; Map Ctrl+Alt+D to send the date string
^!D::
SendInput %A_YYYY%%A_MM%%A_DD% %A_Hour%%A_Min%
Return
Save the above script as *.ahk and run it. AHK will run in the background and listen to the key sequence to do the desired action
For OP/anyone interested: http://ahkscript.org
Auto Hotkey might be what you were after. Either a vanilla complementary cmd .bat or a standalone AHK script should be able to pull data from STDOUT and SET your date syntax up, AND set the hotkey all from 1 script.
The "hotkey" section of this question might be why it hasn't been answered before, not something that can be done in Windows CLI afaik, you need "Shortcut key remapping software" which is exactly what Auto Hotkey is designed for :)
It has been a long time since I researched Shortcut key remapping in Windows, but I do remember AHK being only a handful of programs able to remap hard-coded shortcut combinations like WinKey + R, unless you code one yourself (in C++ or similar)
I also recommend a thorough dig into Rob van der Woude's site, specifically the 2 articles about redirection:
http://www.robvanderwoude.com/battech_redirection.php
and down the bottom is a link to the "Redirection overview page" which is a particularly good "cheat sheet" for redirection commands/syntax.
Related
I'm trying to write a vbs script that has to send the shift key once. I dont need any other key pressed after it but I can't get it to work (I want to use only vbs not any other progarm nor another language). I've tried things like:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "+{}"
or
WshShell.SendKeys "+"
or
WshShell.SendKeys {+}
or
WshShell.SendKeys +
and many more but nothing seems to work.
Thank you in advance!
-Sandro Cutri
You can send a shift key (without anything else) with the Duck toolkit. It's a free webside. It "translates" your code from vbs to the ducky language (it turns it into a inject.bin file). I assume that that's what you want to do. From there you can write
SHIFT
That's all you need to write. It's important to write in CAPITAL letters.
I'm not even sure where to start with my question, I tried a hundred things and googled for hours but didn't find anything useful. (I'm open to every dirty trick.)
Here's my problem:
I have a .hta-file with a listbox that looks like this:
It lists all sessions/modi of my SAP Gui running.
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
If application.Connections.Count > 0 Then
Set connection = application.Children(0)
If connection.Sessions.Count > 0 Then
Set session = connection.Children(0)
End If
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
Set optGroup = Document.createElement("OPTGROUP")
optGroup.label = "Server"
'count all connected servers
ConnectionCount = application.Connections.Count
If ConnectionCount > 0 Then
Sessionlist.appendChild(optGroup)
Else
optGroup.label = "No connection here."
End If
'count all sessions per server
If ConnectionCount > 0 Then
For Each conn in application.Connections
'Text output connections and sessions
SessionCount = conn.Sessions.Count
whatIsIt = conn.Description
ConnectionFeld.innerhtml = ConnectionFeld.innerhtml & " <br> " & SessionCount & " Sessions auf " & whatIsIt
'fill listbox with all connections
Set objOption = nothing
Set optGroup = Document.createElement("OPTGROUP")
optGroup.label = conn.Description
Sessionlist.appendChild(optGroup)
i = 0
'fill listbox with all sessions
For Each sess In conn.Sessions
i = i + 1
Set objOption = Document.createElement("OPTION")
objOption.Text = "Session " & i & ": " & sess.ID
objOption.Value = sess.ID
SessionList.options.add(objOption)
Next
Next
Else
Exit Sub
End If
My goal: When I doubleclick on one of the entries in that list, the selected instance of my SAP Gui should come to the foreground/get activated.
Unfortunately my taskmanager only lists one task and that is "SAP Logon". One of my opened windows also has the name "SAP Logon", all others have the same name: "SAP Easy Access".
The only way I can see the IDs of the connection (servername) and the IDs of the session is via extracting them with vbscript. (see above)
Is there any way to do that? The only workarounds I could think of after trying a thousand solutions are these two:
extremely ugly workaround:
If sessionID = sess.ID Then
Set objShell = CreateObject("shell.application")
objShell.MinimizeAll
sess.findById("wnd[0]").maximize
End If
It minimizes all windows an then maximizes the selected SAP window. Unfortunately My HTA-GUI also gets minimized which kinda sucks.
Second idea:
Somehow get to these clickable thingies by shortcut and put that in my script or some other ugly way.
By hand you have to do this:
Click on that little arrow, rightclick on the icon and then leftclick on the name.
Is there any way to automate this? It's driving me crazy.
Hope someone can help me, it would be GREATLY appreciated.
PS: I'm sitting on a machine with restricted rights and so I may not be able to tackle this with Windows API-ish solutions.
EDIT concerning comments:
It is not possible:
to change registry entries
create COM objects
work with anything else than VBScript
Similarly, it also works with the following commands:
session.findById("wnd[0]").iconify
session.findById("wnd[0]").maximize
I found it...
The resizeWorkingPane method - for changing the size of a window - also works on windows in the background. If you change the parameters, the window will come to the foreground.
session.findById("wnd[0]").resizeWorkingPane 300,200,false
I have to partially revoke this, because it doesnt work on all windows. I'm still not sure why, but it keeps failing sometimes. Still, it seems to me, that this is the closest you can get.
From Help.
Activates an application window.
object.AppActivate title
object
WshShell object.
title
Specifies which application to activate. This can be a string containing the title of the application (as it appears in the title bar) or the application's Process ID.
I don't know what access to info you have about the window. Some COM objects have a HWnd property. This post gets you how to convert a hwnd to a ProcessID to be used above.
How to find the window Title of Active(foreground) window using Window Script Host
This shows how to convert a process command-line to a ProcessID. To see what properties and methods are available use the command-line tool wmic (wmic process get /? and wmic process call /?)
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")
For Each objItem in colItems
msgbox objItem.ProcessID & " " & objItem.CommandLine
Next
This is a 100% of the time solution. It's ugly but it works. You can swap out the IQS3 t code for any other one you can confirm the user won't be in and will have access to. Also part of my reasoning for selection of this code is it loads fast.
Set objShell = CreateObject("wscript.shell")
session.findById("wnd[0]/tbar[0]/okcd").text = "/nIQS3"
session.findById("wnd[0]").sendVKey 0
objShell.AppActivate(cstr(session.ActiveWindow.Text))
session.findById("wnd[0]/tbar[0]/btn[3]").press
I'm using the function choose.dir() in a script that is run with rscript.exe under Windows XP. The problem is that the directory choosing dialog does not pop up as a top-level window. How can I bring the dialogue to the foreground?
In the meantime, I solved my problem by using visual basic script. Of course, this only works with windows:
tf <- tempfile(fileext = '.vbs')
cat('Set folder = CreateObject("Shell.Application") _
.BrowseForFolder(0, "Please choose a folder" _
, &H0001, 17)
Wscript.Echo folder.Self.Path
', file = tf)
tail(shell(paste('Cscript', tf), intern = T), 1)
After searching the rhelp archives it appears the answer is that you can't use choose.dir and file.choose in a non-interactive session. You might be able to do something similar, since list.files, file.info, file.access and files can be used to gather information, you can display this by writing to a graphics device and executing a system() call to get it displayed, and readLines can be used to get user input.
I have a Setup and Deployment project in Visual Studio 2010.
I would like the installer to create two shortcuts to the executable of another project in my solution. One normal shortcut that simply runs the application using current credentials and another which has the Run as administrator flag set, thereby ensuring that the user is asked for credentials with administrative rights when clicking the shortcut.
Running the application with administrative rights enables certain features that are otherwise not available.
Setting this flag doesn't seem to be possible at first glance. Can this be done directly in Visual Studio? If not, are there any other options?
Edit: If not, is it possible to modify the shortcut programmatically using a custom installer class?
I know this is quite an old question, but I needed to find an answer and I thought I could help other searchers. I wrote a small function to perform this task in VBScript (pasted below). It is easily adapted to VB.net / VB6.
Return codes from function:
0 - success, changed the shortcut.
99 - shortcut flag already set to run as administrator.
114017 - file not found
114038 - Data file format not valid (specifically the file is way too small)
All other non-zero = unexpected errors.
As mentioned by Chada in a later post, this script will not work on msi Advertised shortcuts. If you use this method to manipulate the bits in the shortcut, it must be a standard, non-advertised shortcut.
References:
MS Shortcut LNK format: http://msdn.microsoft.com/en-us/library/dd871305
Some inspiration: Read and write binary file in VBscript
Please note that the function does not check for a valid LNK shortcut. In fact you can feed it ANY file and it will alter Hex byte 15h in the file to set bit 32 to on.
If copies the original shortcut to %TEMP% before amending it.
Daz.
'# D.Collins - 12:58 03/09/2012
'# Sets a shortcut to have the RunAs flag set. Drag an LNK file onto this script to test
Option Explicit
Dim oArgs, ret
Set oArgs = WScript.Arguments
If oArgs.Count > 0 Then
ret = fSetRunAsOnLNK(oArgs(0))
MsgBox "Done, return = " & ret
Else
MsgBox "No Args"
End If
Function fSetRunAsOnLNK(sInputLNK)
Dim fso, wshShell, oFile, iSize, aInput(), ts, i
Set fso = CreateObject("Scripting.FileSystemObject")
Set wshShell = CreateObject("WScript.Shell")
If Not fso.FileExists(sInputLNK) Then fSetRunAsOnLNK = 114017 : Exit Function
Set oFile = fso.GetFile(sInputLNK)
iSize = oFile.Size
ReDim aInput(iSize)
Set ts = oFile.OpenAsTextStream()
i = 0
Do While Not ts.AtEndOfStream
aInput(i) = ts.Read(1)
i = i + 1
Loop
ts.Close
If UBound(aInput) < 50 Then fSetRunAsOnLNK = 114038 : Exit Function
If (Asc(aInput(21)) And 32) = 0 Then
aInput(21) = Chr(Asc(aInput(21)) + 32)
Else
fSetRunAsOnLNK = 99 : Exit Function
End If
fso.CopyFile sInputLNK, wshShell.ExpandEnvironmentStrings("%temp%\" & oFile.Name & "." & Hour(Now()) & "-" & Minute(Now()) & "-" & Second(Now()))
On Error Resume Next
Set ts = fso.CreateTextFile(sInputLNK, True)
If Err.Number <> 0 Then fSetRunAsOnLNK = Err.number : Exit Function
ts.Write(Join(aInput, ""))
If Err.Number <> 0 Then fSetRunAsOnLNK = Err.number : Exit Function
ts.Close
fSetRunAsOnLNK = 0
End Function
This is largely due to the fact that Windows Installer uses 'Advertised shortcuts' for the Windows Installer packages.
There is no way inherently to disable this in Visual Studio, but it is possible to modify the MSI that is produced to make sure that it does not use advertised shortcuts (or uses only one). There are 2 ways of going about this:
If your application uses a single exe or two - Use ORCA to edit the MSI. Under the shortcuts table, change the Target Entry to "[TARGETDIR]\MyExeName.exe" - where MyExeName is the name of your exe - this ensures that that particular shortcut is not advertised.
Add DISABLEADVTSHORTCUTS=1 to the the property Table of the MSI using ORCA or a post build event (using the WiRunSQL.vbs script). If you need more info on this let me know. This disables all advertised shortcuts.
it may be better to use the first approach, create 2 shortcuts and modify only one in ORCA so that you can right click and run as admin.
Hope this helps
This is not supported by Windows Installer. Elevation is usually handled by the application through its manifest.
A solution is to create a wrapper (VBScript or EXE) which uses ShellExecute with runas verb to launch your application as an Administrator. Your shortcut can then point to this wrapper instead of the actual application.
Sorry for the confusion - I now understand what you are after.
There are indeed ways to set the shortcut flag but none that I know of straight in Visual Studio. I have found a number of functions written in C++ that set the SLDF_RUNAS_USER flag on a shortcut.
Some links to such functions include:
http://blogs.msdn.com/b/oldnewthing/archive/2007/12/19/6801084.aspx
http://social.msdn.microsoft.com/Forums/en-US/windowssecurity/thread/a55aa70e-ae4d-4bf6-b179-2e3df3668989/
Another interesting discussion on the same topic was carried out at NSIS forums, the thread may be of help. There is a function listed that can be built as well as mention of a registry location which stores such shortcut settings (this seems to be the easiest way to go, if it works) - I am unable to test the registry method at the moment, but can do a bit later to see if it works.
This thread can be found here: http://forums.winamp.com/showthread.php?t=278764
If you are quite keen to do this programatically, then maybe you could adapt one of the functions above to be run as a post-install task? This would set the flag of the shortcut after your install but this once again needs to be done on Non-Advertised shortcuts so the MSI would have to be fixed as I mentioned earlier.
I'll keep looking and test out the registry setting method to see if it works and report back.
Chada
I needed to make my application to be prompted for Administator's Rights when running from Start Menu or Program Files.
I achieved this behavior after setting in \bin\Debug\my_app.exe 'Run this program as administator' checkbox to true. ( located in Properties\Compatibility section ).
While installing project, this file was copied to the Program Files (and therefore the shortcut in the Start Menu) with needed behavior.
Thanks,
Pavlo
firts excuse me for my English it`s super Freak. Sorry
I have a big problem , i need finish my applicatión in VB6.0 for a test in my High Schooll and i can`t find the solution, My app open a FFmpeg.EXE file which open a cmd window Prompt and start a trascoding process, i need link the last line generated into the Prompt of the CMD window (Or top Bottom) , in this line exists Values what change , in this trascoding process the result are bit Rates , which fluctuates acording to others var.
The idea it´s what into the form of my app i can read this line in real time to bulid a progress bar (File Size/Bitrate average)=time to process.
Can you help me. Thanks for the answer....
Put a reference to Windows Scripting Host Object Model and try this snippet
Option Explicit
Private Sub Command1_Click()
Dim oExec As WshExec
Dim sRow As String
With New WshShell
Set oExec = .Exec("tasklist.exe")
End With
Do While oExec.Status = WshRunning
sRow = oExec.StdOut.ReadLine
If InStr(1, sRow, "vb6.exe", vbTextCompare) > 0 Then
MsgBox sRow, vbExclamation
End If
Loop
End Sub
Basicly try executing FFmpeg.EXE and ReadLine until you find some key text.
Send the output to a textfile then read this textfile.
Should look something like this:
ping >e:\test.txt
Where ping is the FFmpge.EXE and e:\test.txt the output textfile
rdkleine
I read your answer and this is great work very good , only that it shows in the log a death value "text", and i need the value of fluctuates bitrates of conversion , which changes in real time in the prompt of the cmd window. i'm trying now with the source code of wqw , i'm working in there.
Thak's for your answer..