Folder started from batch file is not active window

333 Views Asked by At

I have the following batch file which basically copies another bat file from the shared folder, selects this bat file and sends ENTER key with sendkeys option.

It all works fine on Windows 7, however, as soon as I try to use the same feature on Windows 10 it won't work properly.

It does copy file in a specific folder, it does open this folder with the selected file but it won't start file itself. It is important in my case to start the file with sendkeys feature.

I assume that the problem is caused by not keeping the opened folder in focus, sendkeys does send ENTER button, but since the selected folder is not in the windows focus batch file is not started automatically.

Is there any possible way to focus windows on the opened folder?

@if (@CodeSection == @Batch) @then

@echo off

set SendKeys=CScript //nologo //E:JScript "%~F0"

xcopy "\\fs\FIle Share\SA Support\ZverTools\Win10UninstallUnnecessaryApps.bat" "%USERPROFILE%" /y

PING localhost -n 2 >NUL

set targetfilepath=%USERPROFILE%\Win10UninstallUnnecessaryApps.bat   
%SystemRoot%\explorer.exe /select, "%TARGETFILEPATH%"

PING localhost -n 1 >NUL

%SendKeys% "{ENTER}"

goto :EOF

@end

var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
1

There are 1 best solutions below

5
laniakea On BEST ANSWER

So, problem was associated with ping, one second was not enough, so I increased it to four seconds and now it works fine.

@if (@CodeSection == @Batch) @then

@echo off

set SendKeys=CScript //nologo //E:JScript "%~F0"

xcopy "\\fs\FIle Share\SA Support\ZverTools\Win10UninstallUnnecessaryApps.bat" "%USERPROFILE%" /y

TIMEOUT /T 2 /NOBREAK

set targetfilepath=%USERPROFILE%\Win10UninstallUnnecessaryApps.bat   
explorer.exe /select, "%TARGETFILEPATH%"

TIMEOUT /T 4 /NOBREAK

%SendKeys% "{ENTER}"

goto :EOF

@end

var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));

I also replaced ping

PING localhost -n 1 >NUL

with temeout

**TIMEOUT /T 4 /NOBREAK**

(according to Compo's suggestion)