0

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));
aschipfl
  • 33,626
  • 12
  • 54
  • 99
laniakea
  • 81
  • 1
  • 11
  • Opening an Explorer window to select a file and then simulating an `enter` key button press via JScript seems a rather convoluted way of running a batch file. Including a delay using `ping` is also a poor hack for an already existing command too so could you please explain why you've decided upon those methods. – Compo Jan 05 '20 at 09:57
  • This batch file is started from the HTA application, it changes some registry keys, this is when I run it from HTA application no changes are made to the registry, however, if I click on batch file myself it works like charm. I start to think that for some security reasons HTA files are not allowed to change registry values, this complicated method of mine did the trick, but like I have said before on Windows 10 I have encountered above mention problem. – laniakea Jan 05 '20 at 10:10
  • What can be an alternative of ping in this case? – laniakea Jan 05 '20 at 10:11
  • When you run a `.bat` file from `explorer.exe` it is run as an argument to the command `C:\Windows\system32\cmd.exe /C`, there seems no obvious reason to use such a convoluted method, _(unless it bypasses a security restriction of running a batch file from HTML Application)_, when you could simply use the same command directly within your batch file. As for the `ping` hack replacement, open up a Command Prompt window and enter`timeout /?` to read its usage information. – Compo Jan 05 '20 at 10:30

1 Answers1

0

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)

laniakea
  • 81
  • 1
  • 11
  • I still would appreciate it if you could please explain exactly why you're using Windows Explorer and SendKeys via JScript, to run a batch file. – Compo Jan 05 '20 at 11:05
  • In any other case, registry values don't change. I have an app built on the HTA platform, it does various things, for example on button click delete temp file, on button click clean printer spooler, etc. All other features work fine, so I wanted to make a button that would allow me to disable windows app updates, and I have noticed that no changes were made to registry values if I would execute it directly from the HTA application. Downloading the batch file and mimicking click even made required changes, it is weird I know, but I have double-checked everything, only above code worked. – laniakea Jan 05 '20 at 11:08
  • It may sound weird, but if you will try to execute a batch file (which has registry value changing script in it) directly via HTA application it won't work. If you will click on batch file yourself, changes are made, so I have decided to make this complicated method (download batch file, mimic human button click) and it did the trick, now with a single button click within the HTA app I can make required changes to registry values. I tried various other methods, nothing worked except the above-mentioned script. Must be something related to the security policy of WIndows I guess... – laniakea Jan 05 '20 at 11:14
  • Changes to registry values usually require that you start another `cmd.exe` instance, log out and log back in again, or use `RunDll32 User32.dll, UpdatePerUserSystemParameters` or other methods to invoke a WM_SETTINGCHANGE. This complicated methodology is still in my opinion unnecessary. – Compo Jan 05 '20 at 11:19
  • Computers which I have here are in the domain, probably there are some restrictions for network launched scripts, this method overcomes it. I tried it and it works. – laniakea Jan 06 '20 at 05:48