close all open folders in file explorer using batch - windows

I want to create a bat file that can close all open folders in the file explorer. However, it should not close any programs that are open (for example: excel, outlook, web browsers etc). Any help would be appreciated!
Thanks!

You could use wmic to find all explorer.exe processes with -Embedding in their command line and terminate those. This should skip the shell since it doesn't have -Embedding in its command line.
wmic Path win32_process Where "CommandLine Like '%\\explorer.exe % -Embedding'" Call Terminate
As Compo pointed out in the comments: For this to work in a batch file, you need to double the percent signs:
wmic Path win32_process Where "CommandLine Like '%%\\explorer.exe %% -Embedding'" Call Terminate

Related

Keep cmd.exe console window with specific title open?

in Windows 7 x64 SP1 I need to create a batch file (.bat) which keeps the cmd.exe console window open and has a specific title:
#ECHO OFF
title notepadtest
#ECHO ON
start "" /WAIT notepad
But this batch file keeps opening an infinite number of cmd.exe console windows in an unstoppable loop!
How can I create a batch file (.bat) which creates only ONE cmd.exe console window and keeps it open and has a specific title?
Please ensure that your batchfile is not named notepad.cmd or notepad.bat or anything like any system or external command. Name it something like mynotepad.cmd instead., then try this one please:
#echo off
title notepadtest
start "" /w notepad.exe
cmdline and batch files typically works like this. When a command is issued, it first checks the local path, where the script was launched from for the command, if not found, it will check your environment and system environment. So if you name a batchfile notepad.bat your batch is actually starting itself over and over, instead of finding notepad.exe in the environment variables.
Always name batch files something unique and not system/external command related.
Always use full executable name in a batch, like start "" /w notepad.exe and not start "" /w notepad

WMIC open file or URL

Using cmd.exe, you can open a file or URL in the user’s preferred application.
start example.txt
start http://example.com
I see that WMIC can start a program
wmic process call create notepad.exe
However, can it open a file or URL, like start?
Start - Windows CMD
This won't open the 'favored' browser, but you can get to a browser without the need for start.
wmic process call create "C:\Program Files\Internet Explorer\iexplore.exe www.google.com"
wmic process call create "cmd /c start http://example.com"
start is an internal command of cmd.exe, so, you need to execute cmd.exe to reach start command

Kill batch file in such a way that its children are killed too, in Windows

I need to start an exe from a cmd (wrap the exe so I can supply some command-line options). The problem is that just calling the exe from the cmd does not make the wrapping completely transparent: if the .exe hangs, killing the cmd won't kill the exe. I need it to kill the exe too. Can I do that in plain Windows (from XP up), without adding any dependencies?
In Bash you have exec which replaces the shell process with the supplied command. This is handy for writing wrapper scripts, making the wrapping process completely transparent. I know Windows lacks execve() to make this possible, but I'm only interested in the parent-killing-its-children part.
CLARIFICATION: I'm not looking for ways to kill the exe, I am looking for ways to wrap (start) the exe so that killing it using standard ways (eg. Ctrl+C or from task manager) works. For instance, I could create a lnk file (Windows shortcut) and get this behavior, but I want to do it from a script (for one, lnks only work with absolute paths, I can't deploy that).
Thanks.
Taskkill can be used to match certain criteria. By entering Taskkill/? you get the manual and can read up on how to filter using common properties. I assume that all your children share a common portion in their name. You could use taskkill to math the name with wildcards and close all children that matched that name.
EDIT (taken from the comments section):
As IInspectable points out you can kill all child processes using the /T flag.
EDIT starting from a batch you could use START (reference here) to launch the exe parallel to the batch and have your abort in the batch.
Edit i wrote and tested this mini example:
#echo off
echo starting %1
start %1
echo Any key to kill execution
pause >> NUL
taskkill /IM %1 /t
taskkill /F /IM iexplore.exe
/F to force kill.
Which can kill subprocesses
e.g. https://www.windows-commandline.com/taskkill-kill-process/

Run a .cmd file through PowerShell

I am trying to run a .cmd file on a remote server with PowerShell.
In my .ps1 script I have tried this:
C:\MyDirectory\MyCommand.cmd
It results in this error:
C:\MyDirectory\MyCommand.cmd is not recognized as the name of a cmdlet,
function, script file, or operable program.
And this
Invoke-Command C:\MyDirectory\MyCommand.cmd
results in this error:
Invoke-Command : Parameter set cannot be resolved using the specified named
parameters.
I do not need to pass any parameters to the PowerShell script. What is the correct syntax that I am looking for?
Invoke-Item will look up the default handler for the file type and tell it to run it.
It's basically the same as double-clicking the file in Explorer, or using start.exe.
Go to C:\MyDirectory and try this:
.\MyCommand.cmd
Try invoking cmd /c C:\MyDirectory\MyCommand.cmd – that should work.
To run or convert batch files to PowerShell (particularly if you wish to sign all your scheduled task scripts with a certificate) I simply create a PowerShell script, for example, deletefolders.ps1.
Input the following into the script:
cmd.exe /c "rd /s /q C:\#TEMP\test1"
cmd.exe /c "rd /s /q C:\#TEMP\test2"
cmd.exe /c "rd /s /q C:\#TEMP\test3"
*Each command needs to be put on a new line, calling cmd.exe again.
This script can now be signed and run from PowerShell outputting the commands to command prompt / cmd directly.
It is a much safer way than running batch files!
First you can reach till that folder:
cd 'C:\MyDirectory'
and then use:
./MyCommand.cmd

Pausing a batch file when double-clicked but not when run from a console window?

Is there a way for a batch file (in this case, running on Windows XP) to determine whether it was launched from a command line (i.e. inside a console window) or launched via the shell (e.g. by double-clicking)?
I have a script which I'd like to have pause at certain points when run via the shell, but not when run at a command line. I've seen a similar question on SO, but am unable to use the same solution for two reasons: first, whether or not it pauses needs to be dependent on multiple factors, only one of which is whether it was double-clicked. Second, I'll be distributing this script to others on my team and I can't realistically ask all of them to make registry changes which will affect all scripts.
Is this possible?
Found one :-) – After desperately thinking of what cmd might do when run interactively but not when launching a batch file directly ... I finally found one.
The pseudo-variable %cmdcmdline% contains the command line that was used to launch cmd. In case cmd was started normally this contains something akin to the following:
"C:\Windows\System32\cmd.exe"
However, when launching a batch file it looks like this:
cmd /c ""C:\Users\Me\test.cmd" "
Small demo:
#echo off
for %%x in (%cmdcmdline%) do if /i "%%~x"=="/c" set DOUBLECLICKED=1
if defined DOUBLECLICKED pause
This way of checking might not be the most robust, though, but /c should only be present as an argument if a batch file was launched directly.
Tested here on Windows 7 x64. It may or may not work, break, do something weird, eat children (might be a good thing) or bite you in the nose.
A consolidated answer, derived from much of the information found on this page (and some other stack overflow pages with similar questions). This one does not rely on detecting /c, but actually checks for the name of the script in the command line. As a result this solution will not pause if you double-clicked on another batch and then called this one; you had to double-click on this particular batch file.
:pauseIfDoubleClicked
setlocal enabledelayedexpansion
set testl=%cmdcmdline:"=%
set testr=!testl:%~nx0=!
if not "%testl%" == "%testr%" pause
The variable "testl" gets the full line of the cmd processor call, stripping out all of the pesky double quotes.
The variable "testr" takes "testl" and further strips outs the name of the current batch file name if present (which it will be if the batch file was invoked with a double-click).
The if statement sees if "testl" and "testr" are different. If yes, batch was double-clicked, so pause; if no, batch was typed in on command line (or called from another batch file), go on.
Edit: The same can be done in a single line:
echo %cmdcmdline% | findstr /i /c:"%~nx0" && set standalone=1
In plain English, this
pipes the value of %cmdcmdline% to findstr, which then searches for the current script name
%0 contains the current script name, of course only if shift has not been called beforehand
%~nx0 extracts file name and extension from %0
>NUL 2>&1 mutes findstr by redirecting any output to NUL
findstr sets a non-zero errorlevel if it can't find the substring in question
&& only executes if the preceding command returned without error
as a consequence, standalone will not be defined if the script was started from the command line
Later in the script we can do:
if defined standalone pause
One approach might be to create an autoexec.nt file in the root of c:\ that looks something like:
#set nested=%nested%Z
In your batch file, check if %nested% is "Z" - if it is "Z" then you've been double-clicked, so pause. If it's not "Z" - its going to be "ZZ" or "ZZZ" etc as CMD inherits the environment block of the parent process.
-Oisin
A little more information...
I start with a batch-file (test.cmd) that contains:
#echo %cmdcmdline%
If I double-click the "test.cmd" batch-file from within Windows Explorer, the display of echo %cmdcmdline% is:
cmd /c ""D:\Path\test.cmd" "
When executing the "test.cmd" batch-file from within a Command Prompt window, the display of
echo %cmdcmdline% depends on how the command window was started...
If I start "cmd.exe" by clicking the "Start-Orb" and "Command Prompt" or if I click "Start-Orb" and execute "cmd.exe" from the search/run box. Then I execute the "test.cmd" batch-file, the display of echo %cmdcmdline% is:
"C:\Windows\system32\cmd.exe"
Also, for me, if I click "Command Prompt" from the desktop shortcut, then execute the "test.cmd" batch-file, the display of echo %cmdcmdline% is also:
"C:\Windows\system32\cmd.exe"
But, if I "Right-Click" inside a Windows Explorer window and select "Open Command Prompt Here", then execute the "test.cmd" batch-file, the display of echo %cmdcmdline% is:
"C:\Windows\System32\cmd.exe" /k ver
So, just be careful, if you start "cmd.exe" from a shortcut that contains a "/c" in the "Target" field (unlikely), then the test in the previous example will fail to test this case properly.

Resources