[RESOLVED / ALTERED] Windows Explorer ignores sending Backspace to it and doubles its process when started \

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

[RESOLVED / ALTERED] Windows Explorer ignores sending Backspace to it and doubles its process when started \

#1 Post by DOSadnie » 13 Jan 2023 12:51

I wrote this script [as the first step] to get access to Recycle Bin system folder

Code: Select all

cd C:\Windows
start explorer.exe
%SendKeys% {TAB}{TAB}{TAB}{ENTER}
"%userprofile%\Desktop\Recycle Bin.lnk"
It works- i.e. it opens window of Window Explorer; but with a caveat, as it also opens second window with system location This PC. Being there I can manually press Backspace and end up in the Desktop folder with focus placed on Recycle Bin folder - which is exactly what I need

But the next part of my intended macro script simulating the above does not work. And that is because

Code: Select all

%SendKeys% {BACKSPACE}
is ignored. And thus it is futile to add the pivotal line

Code: Select all

%SendKeys% ^Z
which holds the whole purpose for this shebang: I need to simulate pressing of CTRL+Z on the Recycle Bin folder in order to un-delete the last item or items sent to oblivion

And then finally I would also need to somehow close this particular Window Explorer window


I tried using variants

Code: Select all

%SendKeys% {BS}
and

Code: Select all

%SendKeys% {BKSP}
which are listed here https://learn.microsoft.com/en-us/previ ... 3(v=vs.84)] but to no avail. I also have noticed that the line

Code: Select all

%SendKeys% {TAB}{TAB}{TAB}{ENTER}
is apparently somewhat unnecessary and can be completely omitted


And thus - to sum up:

Code: Select all

cd C:\Windows
start explorer.exe
"%userprofile%\Desktop\Recycle Bin.lnk"
%SendKeys% {BACKSPACE}
%SendKeys% ^Z
#1] is unable to push Backspace to Windows Explorer showing the content of Recycle Bin
#2] opens unnecessary second window of Window Explorer showing the content of This PC
#3] lacks commands to close the recently opened Window Explorer window

Please help me
Last edited by DOSadnie on 02 Jul 2023 05:59, edited 4 times in total.

OJBakker
Expert
Posts: 88
Joined: 12 Aug 2011 13:57

Re: Windows Explorer ignores sending Backspace to it and doubles its process when started

#2 Post by OJBakker » 13 Jan 2023 15:10

Open the recycle.bin with

Code: Select all

start "" explorer.exe shell:RecycleBinFolder
And close explorer with Alt-F4

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Windows Explorer ignores sending Backspace to it and doubles its process when started

#3 Post by DOSadnie » 14 Jan 2023 03:05

This took care of the second unnecessary window

But still I am unable to successively send keys

OJBakker
Expert
Posts: 88
Joined: 12 Aug 2011 13:57

Re: Windows Explorer ignores sending Backspace to it and doubles its process when started

#4 Post by OJBakker » 14 Jan 2023 03:24

You need to wait with sending keys until the explorer is started and the recyclebin is available.
So add a timeout command before trying to send keys.
Then send Ctrl-Z to restore the files.
Then close explorer with Alt-F4.

If this does not work show us your entire script.

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Windows Explorer ignores sending Backspace to it and doubles its process when started

#5 Post by DOSadnie » 14 Jan 2023 09:54

I had already tested way back the timeout and it does nothing - i.e. the CMD window just sits behind the window of Windows Explorer counting down, which CMD window is always opened without any delay. Running the script as Administrator also does not help. I also tested running it from within Windows Explorer to rule out as culprit my 32-bit file manager [as I am running on 64-bit version of Windows 10]

The whole script looks now like this:

Code: Select all

cd C:\Windows
start "" explorer.exe shell:RecycleBinFolder
timeout 5
%SendKeys% ^Z
%SendKeys% %{F4}

OJBakker
Expert
Posts: 88
Joined: 12 Aug 2011 13:57

Re: Windows Explorer ignores sending Backspace to it and doubles its process when started

#6 Post by OJBakker » 14 Jan 2023 10:57

Your script is incomplete.
The variable %SendKeys% is not defined in your script but used in the last 2 lines.
Add a pause as the last line.
Then you can see what happens when cmd tries to process your sendkeys lines.

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Windows Explorer ignores sending Backspace to it and doubles its process when started

#7 Post by DOSadnie » 15 Jan 2023 14:09

Well among other topics and batch examples I found this "simple enough to be understand with no problems" code

Code: Select all

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


@echo off

rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"

rem Start the other program in the same Window
start "" /B cmd

%SendKeys% "echo off{ENTER}"

set /P "=Wait and send a command: " < NUL
ping -n 5 -w 1 127.0.0.1 > NUL
%SendKeys% "echo Hello, world!{ENTER}"

set /P "=Wait and send an Up Arrow key: [" < NUL
ping -n 5 -w 1 127.0.0.1 > NUL
%SendKeys% "{UP}"

set /P "=] Wait and send an Enter key:" < NUL
ping -n 5 -w 1 127.0.0.1 > NUL
%SendKeys% "{ENTER}"

%SendKeys% "exit{ENTER}"

goto :EOF


@end


// JScript section

var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
at https://stackoverflow.com/questions/170 ... 5#17050135, but because me molto stupido [https://www.youtube.com/watch?v=IwwBM8na1c4] thus I was not able to rework it to my needs


But I was able to rework this VBS script

Code: Select all

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad", 9 

Code: Select all

' Give Notepad time to load
WScript.Sleep 500
 
Dim Msg: Msg = "Hello, World!"
 
' Type in One Character at a time
For i = 1 To Len(Msg)
    WScript.Sleep 200
    WshShell.SendKeys Mid(Msg, i, 1)
Next
 
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "%{F4}"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{ENTER}"
found at https://helloacm.com/send-keystrokes-to ... dkeys-wsh/ to my needs:

Code: Select all

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "explorer.exe shell:RecycleBinFolder", 9

WScript.Sleep 1111

WshShell.SendKeys "^z"
WshShell.SendKeys "%{F4}"
This works almost A-OK - as the only caveat is to not to repeatedly click it to too fast trying to execute it multiple times, because it causes the file manager from within which I click on this script to close window of that manager instead of Windows Explorer [which I guess could be avoided if this VBS script would not send those keys to windows with an improper title]

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Windows Explorer ignores sending Backspace to it and doubles its process when started

#8 Post by DOSadnie » 28 Jan 2023 05:35

DOSadnie wrote:
15 Jan 2023 14:09
[...]
But I was able to rework this VBS script
[...]
Which, as it turns, executes Undo in Windows Explorer and not just Un-delete - thus when used without a care can results with chaos

And thus I still have the problem of
OJBakker wrote:
14 Jan 2023 10:57
[...]
variable %SendKeys%
[...]
which I failed at defining

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Windows Explorer ignores sending Backspace to it and doubles its process when started

#9 Post by DOSadnie » 01 Jul 2023 07:43

I decided to leave this script, as it is - i.e. to execute with it the broader un-do task [which includes un-delete], because very rarely I also by mistake I inadvertently drop something to some location [like when suddenly wireless mouse deciced to loose its connection for a split second]

And so with this

Code: Select all

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run "explorer.exe shell:RecycleBinFolder", 9
WshShell.Run "explorer.exe shell:RecycleBinFolder", 0, True

WScript.Sleep 1111

WshShell.SendKeys "^z"
WshShell.SendKeys "%{F4}"
I can just execute a single VBS file that will undo both kinds of mistakes [accidental deletion and movement] and also unwanted copy if needed

Post Reply