1

In order to prevent inadvertent run of batch files that could create unwanted behaviour in some folders I add these 3 lines to the beginning to all of my batch files.

@echo off
set /p choice=Are you sure to run this file, if so press 'y' anything else cancels? 
if /I %choice% neq y notepad %~f0
.
. rest of the code
.

So if the user presses y the file will do what is expected otherwise notepad will open it for editing like clicking a .ps1 file but the console window remains open until I close notepad or close the console, reading this post suggests that what I did is less than ideal.

Is there a way closing the console after notepad opens the file.

user10191234
  • 531
  • 1
  • 4
  • 24
  • 2
    using the `choice` command is definitvely better (see Compo's anser). But to explain your problem: `%choice%` is empty. You defined `%choice %` instead. Remove the spaces around the `=` with the `set` command. – Stephan Nov 14 '20 at 16:47
  • You are right, I only did it here for readability forgot that it matters. I think that it has something to do with not using `start` – user10191234 Nov 14 '20 at 18:37

1 Answers1

2

The simplest way is to use the built-in choice.exe utility, the Start command, and obviously, the Exit command. (+)

@%SystemRoot%\System32\choice.exe /M "Are you sure to run this file"
@If ErrorLevel 2 Start %SystemRoot%\System32\notepad.exe "%~f0" & Exit
@Echo Off
.
. rest of the code
.

[EDIT /]

Here's an alternative method, as mentioned in my comment below.

@%SystemRoot%\System32\choice.exe /C YN /N /D N /T 5 /M "Are you sure to run this file"
@If ErrorLevel 2 Start %SystemRoot%\System32\notepad.exe "%~f0" & Exit
@Echo Off
.
. rest of the code
.

The reason, I've advised that you use choice.exe is that the Set /P command is potentially dangerous. With it the end user could enter anything at all they wish, including deliberately malicious characters or commands. Your code would therefore need to robustly parse that input without running it, in order not to break your script, or worse.

Here's an example of a method of using Set /P to perform a similar function.

@Set "#="& Set /P "=Are you sure to run this file? If so, press 'y', anything else cancels"<Nul
@For /F Skip^=1^ Delims^=^ EOL^= %%# In ('%SystemRoot%\System32\replace.exe ? . /U /W') Do @If Not Defined # Set "#=%%#"
@(Set #) 2> NUL | %SystemRoot%\System32\findstr.exe /ILX "#=Y" 1> NUL || (
    Start %SystemRoot%\System32\notepad.exe "%~f0"& Exit)
@Echo Off
.
. rest of the code
.
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Please note, you could also use `%SystemRoot%\notepad.exe`, for a version always accessible in 32-bit `cmd.exe` sessions, and if you're sure that the ennvironment has not been modified accidentally or otherwise, use just `choice` instead of `%SystemRoot%\System32\choice.exe`, and just `notepad`, instead of `%SystemRoot%\System32\notepad.exe`, or `%SystemRoot%\notepad.exe`. – Compo Nov 14 '20 at 15:48
  • `choice` is good, but it only allows `[Y/N]`, I am really looking for the *anything else cancels* – user10191234 Nov 14 '20 at 19:10
  • That is technically incorrect, because different languages use different keys, they're not all English. Also there's no need for that because the end user can only enter two keys, _(other than `[CTRL]`+`[C]`)_. If your end user is not capable of following a simple prompt, they cannot be trusted to use your script. One possible better way of doing it is to use a default option of `N` if a `Y` isn't pressed within a defined timespan. That way only a `Y` will continue the script. I have added such an alternative as an `[EDIT /]` to my answer above. – Compo Nov 14 '20 at 19:26
  • You're right, but I am getting inspired from the `unformat` command in `MS-DOS`, it used to write that and some of the computers where the script might be used are still on archaic OS. – user10191234 Nov 14 '20 at 19:36
  • @user10191234, I've added an example to show the difference when using `Set /P`. Feel free to try it, if you really think such a thing is necessary. – Compo Nov 14 '20 at 20:24