| 1. |
Solve : Batch File - batch window auto-focus?? |
|
Answer» I've GOT a batch file I'm using to maintain my disk image backups. Make a one-line launcher batch script; add it to your scheduled tasks. It will launch the batch you want to run in a new window, and give it focus, and then exit. Thanks ST, but that script does nothing. My batch doesn't won't run even though Task Scheduler reports a status of "Running". Presumably your code is completely suppressing the window altogether? Quote from: Lemonilla on April 01, 2012, 06:34:16 PM If I understand correctly, you have a batch file running in the background that will pop up errors on your disk images and you want it to bring it to the top. Nope, you've got it wrong. My batch is waiting on my input before it's allowed to continue or quit. The batch file opens up on top of other windows just fine. My issue is that it's not in focus when I let Task Scheduler run it on a schedule. Needless to say, running it manually via task scheduler, or simply double clicking the batch file directly, will give the window focus. FWIW, Here's how things are working. Machine is on at backup time: Acronis images my drives at 8AM every Monday. Task Scheduler kicks in two hours later, plenty of time for Acronis to finish creating the file, and manipulates said images. Machine is off at backup time: BOTH Acronis and Task Scheduler want to run immediately at next system boot. Since the file exists but isn't finished yet, I have the batch sitting at a prompt waiting for a "Y" keystroke. When Acronis is done, I press "Y" and the batch continues. Here's my file it this helps: This "version" has all the network attached storage stuff removed for ease of testing from the Desktop if you're so inclined... Code: [Select]ECHO OFF C: CHDIR %~dp0 CLS ECHO WAITING FOR ACRONIS FILE DUMP PING 1.1.1.1 -n 1 -w 3000 >NUL IF NOT EXIST *.tib CALL :ABORT CLS CHOICE /M "IS ACRONIS DONE DUMPING DRIVE IMAGE" IF %ERRORLEVEL% EQU 2 CALL :EXIT CLS ECHO RENAMING BACKUP. PING 1.1.1.1 -n 1 -w 3000 >NUL IF EXIST *.tib REN *.tib Backup.tib IF NOT EXIST ".\BACKUPS\" CALL :MAKEFOLDER :MOVEFILE CLS ECHO MOVING BACKUP INTO STORAGE DIRECTORY PING 1.1.1.1 -n 1 -w 3000 >NUL MOVE Backup.tib ".\BACKUPS\" CLS ECHO RESTRUCTURING BACKUP STORAGE DIRECTORY PING 1.1.1.1 -n 1 -w 3000 >NUL CHDIR ".\BACKUPS\" IF EXIST Backup2.tib DEL Backup2.tib IF EXIST Backup1.tib REN Backup1.tib Backup2.tib IF EXIST Backup.tib REN Backup.tib Backup1.tib CLS ECHO ALL DONE! PING 1.1.1.1 -n 1 -w 3000 >NUL EXIT REM *************SUBROUTINES************** :ABORT CLS ECHO NO BACKUP FOUND, ABORTING PING 1.1.1.1 -n 1 -w 3000 >NUL EXIT :MAKEFOLDER MKDIR ".\BACKUPS\" CLS ECHO NO STORAGE DIRECTORY FOUND, CREATING IT NOW PING 1.1.1.1 -n 1 -w 3000 >NUL CALL :MOVEFILE :EXIT CLS ECHO OK, EXITING. PING 1.1.1.1 -n 1 -w 3000 >NUL EXIT Hmm, now that I'm thinking about it... I wonder if I can make the batch read the date/time the was last modified? If so, perhaps I could create an argument along the lines this for when the job is missed: If last modified within the last two hours, ask me if Acronis is done. That way, I could do away with the prompt altogether when the job runs as scheduled. Quote from: S3NTYN3L on April 03, 2012, 08:36:45 AM Thanks ST, but that script does nothing. Works fine on my system. I set it to run with highest privileges. I presume the path and file name are correct? What OS are you running this on? Quote from: Salmon Trout on April 03, 2012, 10:49:21 AM Works fine on my system. I set it to run with highest privileges. I presume the path and file name are correct? What OS are you running this on? Running with highest privileges as well on Windows 7, (as stated on the left of my posts).I have got this to run at a set time and also on demand (1) Launcher.bat Code: [Select]start "" "cmd" /c "C:\Batch\Test\After 22-01-2012\Batchfocus\CHBatchfocus.bat" (2) CHBatchfocus.bat Note: I have commented out various ping and CLS commands to make the screen capture tell a story, and added a couple of lines to make things clearer. Code: [Select]ECHO OFF echo The time is %time% set /p dummy="if we have focus, you can type SOMETHING here " C: CHDIR %~dp0 REM CLS ECHO WAITING FOR ACRONIS FILE DUMP REM PING 1.1.1.1 -n 1 -w 3000 >NUL IF NOT EXIST *.tib CALL :ABORT CLS CHOICE /M "IS ACRONIS DONE DUMPING DRIVE IMAGE" IF %ERRORLEVEL% EQU 2 CALL :EXIT CLS ECHO RENAMING BACKUP. PING 1.1.1.1 -n 1 -w 3000 >NUL IF EXIST *.tib REN *.tib Backup.tib IF NOT EXIST ".\BACKUPS\" CALL :MAKEFOLDER :MOVEFILE CLS ECHO MOVING BACKUP INTO STORAGE DIRECTORY PING 1.1.1.1 -n 1 -w 3000 >NUL MOVE Backup.tib ".\BACKUPS\" CLS ECHO RESTRUCTURING BACKUP STORAGE DIRECTORY PING 1.1.1.1 -n 1 -w 3000 >NUL CHDIR ".\BACKUPS\" IF EXIST Backup2.tib DEL Backup2.tib IF EXIST Backup1.tib REN Backup1.tib Backup2.tib IF EXIST Backup.tib REN Backup.tib Backup1.tib CLS ECHO ALL DONE! PING 1.1.1.1 -n 1 -w 3000 >NUL EXIT REM *************SUBROUTINES************** :ABORT REM CLS ECHO NO BACKUP FOUND, ABORTING pause REM PING 1.1.1.1 -n 1 -w 3000 >NUL EXIT :MAKEFOLDER MKDIR ".\BACKUPS\" CLS ECHO NO STORAGE DIRECTORY FOUND, CREATING IT NOW PING 1.1.1.1 -n 1 -w 3000 >NUL CALL :MOVEFILE :EXIT CLS ECHO OK, EXITING. PING 1.1.1.1 -n 1 -w 3000 >NUL EXIT I was trying to put the start command into the add arguments field, my bad. OK, your way works, but it doesn't do anything. It just opens and closes the launcher window, opens and closes my original batch file and blows through the original (known working) script without processing any commands contained within... I also tried your edited batch and get the same results. Everything works fine on my system. Mine is Windows 7 Professional 64-bit; my user account has Administrator privileges. Would this help? Combine the launcher and the batch file code in a single batch file: Code: [Select]echo off setlocal if [%1]==[Recurse] goto %2 start "Focused Window" "%0" Recurse Label %1 %2 %3 %4 %5 %6 %7 %8 %9 exit :Label echo Your Batch Code Here If it seems a bit dinky, well, it is! But it should keep thing simple. Quote from: Sidewinder on April 03, 2012, 04:06:24 PM Would this help? Combine the launcher and the batch file code in a single batch file: This does not address the question of why my solution works for me but not for him. I have devised a method to allow you to launch an elevated command prompt via the command line, with no clicking, and the new prompt gets focus, with no clicking. You can just keep typing. Here's how to do it: 1. Create a task using task scheduler, as detailed elsewhere on this thread. The task's command line is simply: Code: [Select]cmd and the "Run with highest privileges" box must be checked. 2. Create a batch file called "sudocmd.cmd" in your PATH with this: Code: [Select]schtasks /run /tn "myTasks\sudocmd" sudocmd.vbs 3. Create a file called "sudocmd.vbs" also in your path, most conveniently in the same folder, as: Code: [Select]set wshShell = CreateObject("WScript.Shell") WScript.Sleep 500 wshShell.AppActivate("Administrator: taskeng.exe") This last little bit is the trick that will bring the new command window into focus. 4. Then just type sudocmd at a command prompt and voila! Another possibility is this little gem I found: http://sourceforge.net/projects/sudowin/ |
|