• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

What's up with this Windows batch file?

Ichinisan

Lifer
I know perfectly well how to restart explorer.exe via the command line. I've actually done it a lot over the past few weeks (since Systernals Process Monitor somehow trashed itself *and* the Windows Task Manager). It's long past time to reinstall Windows on my work computer and jump from XP to 7 🙂

I don't remember what I was looking for when I came across instructions to create this batch file:

Code:
@echo off
color 0B
echo JumpStart! v0.1 by Scotch
echo.
echo Your desktop is being restored, Please wait. . .
ping -n 5 127.0.0.1 > NUL 2>&1
echo Killing process Explorer.exe. . .
taskkill /f /im explorer.exe
cls
echo Success!
echo.
echo Your desktop is now loading. . .
ping -n 5 127.0.0.1 > NUL 2>&1
echo.
ping -n 5 127.0.0.1 > NUL 2>&1
start explorer.exe
exit

Ignoring all the dumb / irritating things about this, I want to find out more about this part:
" > NUL 2>&1"
I know ">NUL" suppresses output from the screen without sending it to a file. What's going on with the part I underlined? The "&1" seems similar to "%0", "%1", "%2", etc (used for accepting input parameters).

...and here's what I find irritating:
  • It uses the PING command only to show a message with mandatory delays. It does it before killing explorer.exe and before it launches explorer.exe (why add delays for no damn reason?!)
  • There are two 5-ping delays added before starting explorer.exe (if you think the delay is needed, use a single 10-ping delay)
  • "color 0B" is completely unnecessary.

[edit]
I guess the ampersand combines two commands on a single line. I still don't know what's going on with "2>&1"
 
Last edited:
The 2>&1 redirects all streams to the same output. This basically also means "any errors also go to echo / fstream (whichever you're specifying)".

I'm not using technical terms and probably am not describing it correctly - but basically any errors will also not be printed.

EDIT:

If I had to guess, that entire ping line is a lazy way to count to 5.

EDIT EDIT:

And to see more clearly what the 2>&1 does, intentionally write a command line that doesn't work into a file. Here is your exercise:

Write the following commands:

heythiswontwork > c:\test.txt

You'll see that the command line prompt informs you that it isn't a recognized command, and the text file is empty. Now try:

heythiswontwork > C:\test.txt 2>&1

You won't see a thing. It is now stored in the file (the error message received from the first test).
 
Last edited:
The ping is a pause. There is a period shown to the user between the two 5 second pauses. Other than that period, a 10 second pause would do the same thing.
 
There is a period shown to the user between the two 5 second pauses.
Actually, "echo." prints a blank line. It's a special case.

I didn't know 2>&1 worked in Windows. I've known about it for years in Linux, though.

I didn't see the other '&' usage in this batch file, but you mentioned it. A single & between commands ensures that both commands run, always. A double, &&, runs the second command only if the first worked without returning an error code. E.g.:
Code:
copy file1 file2 && del file1
 
Back
Top