Is it possible to make a Batch, or .vbs if needed, is it possible to make batch have a little icon down there with the flag, battery and volume.
I want it to be a "Shutting down in xx minutes/hours", and mayyybbe clicking it cancels' it.
Thanks in advance :)
Well, I know how to accomplish part of what you want -- making a systray balloon tip by borrowing from PowerShell. But I don't know how to make it listen for dismissal of the balloon. Maybe someone else can offer another answer building upon mine?
Anyway, I use this for a conversion script I made to convert flac to mp3 in batches. Feel free to hack it for your own evil purposes.
#echo off
setlocal
for %%I in (*.flac) do (
rem // This initiates the systray balloon.
call :systray converting from "%%~nxI" to "%%~nI.mp3"
)
goto :EOF
rem // Here's the :systray function
:systray <message>
setlocal enabledelayedexpansion
set "args=%*"
set "args=!args:'=''!"
set "code="[void] [Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms');^
$o=New-Object Windows.Forms.NotifyIcon;$o.Icon='%systemroot%\System32\PerfCenterCpl.ico';^
$o.BalloonTipIcon='Info';$o.BalloonTipText='!args!';$o.BalloonTipTitle='%~nx0';$o.Visible=1;^
$o.ShowBalloonTip(10000);Start-Sleep -M 12000;$o.Dispose();Exit""
start /b "" "powershell" %code%
endlocal & goto :EOF
The n values in $o.ShowBalloonTip(n1) and Start-Sleep -Mn2 are in milliseconds. Salt to taste.
Update: I found a bit about registering an event for $o.BalloonTipClicked as well as a lovely example in the wild. Basically, replace this:
$o.ShowBalloonTip(10000);Start-Sleep -M 12000;$o.Dispose();Exit
... with this:
$o.ShowBalloonTip(10000);register-objectevent $o BalloonTipClicked clicked;^
if (wait-event clicked -t 12) {$true} else {$false}; $o.Dispose(); Exit
You also need to execute powershell in a single threaded apartment for the event to work.
start /b "" "powershell" -STA %code%
Now, you need to figure out how to make that relevant back in the context of your batch process. For one thing, you'd probably no longer be able to use start /b to make the balloon tip non-blocking, and you'd probably use a for /F loop to capture the output of the powershell command.
Adding to your worries, I propose that "Shutting down in xx minutes" is not entirely user-friendly. What if "Shutting down in 30 minutes" appeared 29 minutes ago, but the user just now saw it? "Shutting down at 9:51 AM" might be better.
So with all this in mind, since what you want is event driven and since the batch language doesn't handle date-time math all that easily, I suggest doing the whole damn thing in PowerShell. Save this with a .ps1 extension. Right-click and run with PowerShell. Or if you want to execute it from a cmd console, do powershell ".\scriptname.ps1".
set-executionpolicy remotesigned
if ([threading.thread]::CurrentThread.GetApartmentState() -eq "MTA") {
& powershell.exe -window minimized -sta $MyInvocation.MyCommand.Definition
exit
}
[void] [Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
$minutes = 30
$launch_time = (Get-Date).AddMinutes($minutes).ToShortTimeString()
$o = New-Object Windows.Forms.NotifyIcon
$o.Icon = "$env:SystemRoot\System32\PerfCenterCpl.ico"
$o.BalloonTipIcon = "Info"
$o.BalloonTipText = "Shutting down at $launch_time"
$o.BalloonTipTitle = "Shutdown pending..."
$o.Visible = 1
function show-balloon { $o.ShowBalloonTip($minutes * 60 * 1000) }
show-balloon
$o_hover = [Windows.Forms.MouseEventHandler]{ show-balloon }
$o.add_MouseMove($o_hover)
register-objectevent $o BalloonTipClicked clicked
if (wait-event clicked -t ($minutes * 60)) {
remove-event clicked
$o.BalloonTipText = "Have a nice day!"
$o.BalloonTipTitle = "Shutdown aborted"
$o.ShowBalloonTip(10000)
if (wait-event clicked -t 10) { remove-event clicked }
} else {
# Initiate shutdown sequence on my mark. Authorization rojo alpha 3. Mark.
stop-computer
}
unregister-event clicked
$o.Dispose()
Bill_Stewart, if you're reading this, I know you're pleased. As it happens, PowerShell is indeed the correct tool for the job this time.
Related
Fair people of stack overflow! My question today is how to detect key presses, but not in the batch window itself. I have this code already:
choice /c KEY /n /t 5 /d d
if %errorlevel%==1 goto restofcode
This only works if you select the batch window itself. What I need is a way, in batch (or some other language) to make it detect if you press Ctrl+W outside of the actual batch command line itself.
There is a program called GS Auto-Clicker and it starts clicking your mouse when you press a key. I want something like this as far as detecting what key you press.
Okie doke, here's an example demonstrating GetAsyncKeyState() using a Batch + PowerShell hybrid script. Save it with a .bat extension. Hopefully the inline comments sufficiently explain how it works. See the MSDN page on GetAsyncKeyState() for full details on that function's expected arguments and return value type.
Edit: Fixed bugs. Original answer would trigger if the user pressed and released W then pressed Ctrl. It seems my PowerShell 2.0 interpreter has some buggy behavior. Setting variables to the GetAsyncKeyState() return value on each loop iteration seems to fix it. I also made the key definitions a little less neck-beardy by leveraging the System.Windows.Forms.Keys collection, and removed the possibility that the "recently pressed" bit would trigger a false positive for "currently pressed".
<# : batch portion (begins PowerShell multiline comment block)
#echo off & setlocal
set /P "=Waiting for ctrl-W... "<NUL
rem # re-launch self with PowerShell interpreter
powershell -noprofile "iex (${%~f0} | out-string)"
echo Pressed. Toodles.
goto :EOF
: end batch / begin PowerShell chimera #>
# import GetAsyncKeyState()
Add-Type user32_dll #'
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(int vKey);
'# -namespace System
# for Keys object constants
Add-Type -As System.Windows.Forms
function keyPressed($key) {
return [user32_dll]::GetAsyncKeyState([Windows.Forms.Keys]::$key) -band 32768
}
while ($true) {
$ctrl = keyPressed "ControlKey"
$W = keyPressed "W"
if ($ctrl -and $W) { break }
start-sleep -milliseconds 40
}
$Host.UI.RawUI.FlushInputBuffer()
I have an interactive PowerShell using windows forms, meaning a PowerShell script displays some controls on a form asking for input and finally runs robocopy, when the user clicks the button.
This is a simple script displaying the problem.
You can copy the code, save it to a Power Sell script and run. It needs a:\tmp\tmp folder to successfully run, see the CopyFolder function.
function createdMainForm {
$mainForm = New-Object System.Windows.Forms.Form
$btn = New-Object System.Windows.Forms.Button
$btn.Text = "RoboCopy"
#register click event
$btn.add_click({Add_click})
$mainForm.Controls.Add($btn)
$mainForm.ShowDialog()
}
#click event
function Add_click() {
CopyFolder
}
function CopyFolder() {
$sourseFolder = "C:\tmp\tmp"
$targetFolder = "C:\tmp\tmp2"
$Logfile = "c:\tmp\a.log"
robocopy $sourseFolder $targetFolder /tee /log:$Logfile
}
function ReadMode() {
Write-Host Mode [1 for GUI mode]
$mode = Read-Host
if ($mode -eq 1) {
createdMainForm
} else {
CopyFolder
}
}
ReadMode
I want to capture the robocopy progress in a log file as well as in the console.
However only the log file capture the output, while the console "hangs" until end.
I found that is works smoothly when PowerShell does not display form, but simply runs the command.
The script works in two modes. enter 1 for "form mode" anything else for console mode, where the log actually written to console and file.
How can I use the form, and display the progress in the console?
Use the proper robocopy parameters:
/log:<LogFile> Writes the status output to the log file (overwrites the existing log file).
…
/tee Writes the status output to the console window, as well as to the log file.
robocopy $s $t /tee /log:C:\path\to\your.log
As for updating the output of your embedded control, you probably need to refresh your form on a timer:
$timer = New-Object Windows.Forms.Timer
$timer.Interval = 1000
$timer.add_tick({$form.Refresh()})
Start the timer when calling robocopy and stop it after the command completes. Details will vary depending on your actual code (which you chose to omit).
Edit: I think I finally understood what you're trying to achieve and what problem you have getting there.
You're running robocopy from a scriptblock that is associated with a form element. Because of that STDOUT output doesn't go to the console. You need to take the robocopy output (the success output stream) and write it to the host console yourself:
function CopyFolder() {
...
robocopy $sourseFolder $targetFolder /tee /log:$Logfile | Write-Host
}
Backstory: I need to programmatically find the differences between two files. I want to use WinMerge to generate a report (Tools -> Generate Report) that I can parse to get the differences between two files. I need this done using either a Groovy script or a bat script.
I was hoping that WinMerge would offer command line options to generate the report and then I could just use a Groovy Process object to execute WinMergeU.exe with the arguments. No such luck according to the command options I've found for WinMerge.
Next, I was hoping to be able to start WinMerge and send keystrokes to step through the menus to generate the report(Alt+T, R, Diff.html, [Enter]). I don't see a way to do that from a Groovy Process and I haven't found a way to do this in a bat script. I'm looking for something similar to WshShell.Sendkeys in VB. Is this a wild-goose chase?
UPDATE/Answer with PowerShell in a bat file:
I was intrigued by Knuckle-Dragger's comment about using a PowerShell script in a bat file.
$folder = "C:\DevTools\WinMerge\WinMergeU.exe"
ii $folder
Start-Sleep -m 1000
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
[Microsoft.VisualBasic.Interaction]::AppActivate("WinMerge")
Start-Sleep -m 100
[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
Start-Sleep -m 100
[System.Windows.Forms.SendKeys]::SendWait("%F")
[System.Windows.Forms.SendKeys]::SendWait("o")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
[System.Windows.Forms.SendKeys]::SendWait("%T")
[System.Windows.Forms.SendKeys]::SendWait("r")
Start-Sleep -m 1000
[Microsoft.VisualBasic.Interaction]::AppActivate("Save As")
Start-Sleep -m 1000
[System.Windows.Forms.SendKeys]::SendWait("Diff.txt")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
To encapsulate this in a command window, save it to a file PowerShellScript.ps1:
start /b /wait powershell.exe -nologo -WindowStyle Hidden -sta *PowerShellScript.ps1*
Here is a powershell example to activate winmerge and send some keys.
EDIT: Reduced copy pasta with some .NET variables. $SK = Sendkeys $AA = AppActivate $LRA = Reflect .NET.
$startapp = "C:\DevTools\WinMerge\WinMergeU.exe"
ii $startapp
$SK = "[System.Windows.Forms.SendKeys]::SendWait"
$AA = "[Microsoft.VisualBasic.Interaction]::AppActivate"
$LRA = "[void][System.Reflection.Assembly]::LoadWithPartialName"
Start-Sleep 1
$LRA+'("Microsoft.VisualBasic")'
$AA+'("WinMerge")'
Start-Sleep -m 100
$LRA+'("System.Windows.Forms")'
Start-Sleep -m 100
$SK+'("%F")'
$SK+'("o")'
$SK+'("{ENTER}")'
$SK+'("%T")'
$SK+'("r")'
Start-Sleep 1
$AA+'("Save As")'
Start-Sleep 1
$SK+'("Diff.txt")'
$SK+'("{ENTER}")'
To encapsulate this in a command window, save it to a file PowerShellScript.ps1: Note, changed the command syntax a bit, should work if you use the & {.\dot\source\path}
start /b /wait powershell.exe -nologo -WindowStyle Hidden -sta -Command "& {.\PowerShellScript.ps1}"
This is something that I threw together without testing, and I don't have WinMerge so testing isn't really an option.
RunMe.bat
start "" "C:\Path\WinMerge.exe" "file1" "file2"
GenerateDiffFile.vbs
GenerateDiffFile.vbs
Set s = CreateObject("WScript.Shell")
wscript.sleep(1000) ' Sleep for 1 second to allow time for WinMerge to finish loading and merge the files
s.SendKeys("%tr")
wscript.sleep(250) ' Give time for dialog box to appear
s.SendKeys("Diff.html{Enter}")
This is completely untested, but I think it is very close to what you need... You can see that the batch-file runs WinMerge, passing the two files to merge on the command line. Then it launches the VBS script, which pauses long enough to allow the launched WinMerge to be able to accept keyboard input. I used this page from Microsoft to know what keys to send.
I've been sending keys to programs for years. It works very reliably, as long as the program you want to send keys to keeps focus.
All I do is this:
echo y | "Batchfile needing the keystroke"
I have made a very simple Powershell script with WinForms GUI.
Everything works as intended but, when I run the .ps1 script with PowerShell a black empty console window appears at first and then the GUI shows.
Anyway to make the console window dissapear?
Best regards
I wrote a small article on this subject (sorry in french) one year ago.
Here is the common solution using a small VBS script to start PowerShell hidding his window (the trick is in the last ,0).
Set Args = Wscript.Arguments
'MsgBox "Chemin LDAP: " & Args(0)
'MsgBox "Classe: " & Args(1)
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -Noninteractive -file c:\SlxRH\RhModif.ps1 " & chr(34) & Args(0) & chr(34) , 0
I also embeded PowerShell in an executable with no console called slxPShell2.EXE.
I found the above didn't work for me. I used this:
Set objShell = CreateObject("WScript.Shell")
objShell.Run "CMD /C START /B " & objShell.ExpandEnvironmentStrings("%SystemRoot%") & "\System32\WindowsPowerShell\v1.0\powershell.exe -file " & "YourScript.ps1", 0, False
Set objShell = Nothing
Hope that helps.
This solution Minimizes Powershell window after it starts. Powershell window opens, then disapears, without using any outside code. Put at beginning of your script.
$t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
add-type -name win -member $t -namespace native
[native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
This is how I got this working:
Have the Winforms GUI script in one ScriptOne.ps1 file
Create another LaunchScriptOne.ps1 file with the content:
powershell.exe -WindowStyle Hidden -File "C:\path\to\ScriptOne.ps1".
The solution was provided in another thread on the same topic: Hide or Minimize the powershell prompt after Winform Launch
I hope someone will find a way to put this into one single script as well. The answers above in this thread did not help me, but maybe I did something wrong, idk.
I'm nube so no rep so can't comment inline... though wrt #Ipse's solution which I'm a fan of, I also make sure to close the hidden window when the script is done... not sure if PS gets around to this sort of auto-garbage collection, but suspect it's good best practice.
eg. at end of your script I'd suggest doing:
stop-process -Id $PID
(which should terminate that hidden window v. just leave it lurking around and tying up those resources).
I rendered 140000 frames to create a movie of them.
However, it starts at 1.png where it would be better if it had started at 000001.png in order to keep the order good when importing it in final cut express.
I used to have a program r-name but that was based on power-pc so it doesn't work anymore.
Also the program was quite shit with even a batch of 300 files for example so i guess it would be better to use the terminal for that.
I have seen examples for renaming but most where for changing the extention for example or change a prefix.
Could someone help me with the right terminal script? I need to finish this project asap, else i would have re-rendered it but it takes 15 hours to do.
Not very efficient, but since you need to run it only once:
for i in `seq 1 140000`; do
mv $i.png `printf %06d $i`.png
done
EDIT: I assumed (maybe wrongly) that you were using Linux. This won't work on Windows.
EDIT: Yes, this should work in Mac OS X. Instead of typing these lines into the prompt, you can save it to a file. Usually, you would save such a file with a name like rename.sh. Then You can run it on the terminal like this:
sh rename.sh
If you are unsure, you can change the mv line into:
echo mv $i.png `printf %06d $i`.png
This will print out on the screen the commands that would be executed. Then if everything looks ok, you change it back to the original and run it again.
If the number of files is different, just replace 140000 with the number of the last file.
for i in *.png
do
name=${i%.png}
[[ $name =~ ^[0-9]+$ ]] && mv $i "$(printf '%06d' $name).png"
done
If you are using Windows
#echo off
setlocal enableDelayedExpansion
for %%F in (*.png) do (
set "name=00000%%~nF"
ren "%%F" "!name:~-6!.png"
)