Home > front end >  "set/p" doesn't work, I can't make out how "set" works
"set/p" doesn't work, I can't make out how "set" works

Time:11-03

@echo off
REM turning off the output of commands to the screen
Set /P "%~1=Extension: "
if "%~1" == "" echo Extension not introduced
REM if you do not enter anything, the extension is not entered
if not exist "*.%~1" echo No files found
REM if you entered an extension that does not exist, it will give you no files found.
DEL /Q "*.%~1"
REM delete files with the specified extension without confirmation

I can't figure out how "set" works. Help to make it so that: after launching batch, you had to enter the extension and then it would be deleted or output that it was not found.

CodePudding user response:

(not really worth an answer, but comments are not good at displaying readable code)

%~1 is the first parameter to the script. When I understand you correctly, you don't want any parameters, but you want the script to ask for input.

You have to use a variable (%variable%) instead of a parameter then:

@echo off
:label
set /p "ext=" & REM clear variable because `set /p` leaves it unchanged when you enter nothing
Set /P "ext=please enter Extension: "
if "%ext%" == "" echo Extension not introduced & goto :label
if not exist "*.%ext%" echo No files found & goto :label
DEL /Q "*.%ext%"

(removed the comments and added some basic "code flow")

CodePudding user response:

Why not just try to simplify your flow by using If's, labels, and GoTo's?

Example

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

For /F "Delims==" %%G In ('"(Set Ext) 2>NUL"') Do Set "%%G="
Set "Ext=%~1"
If Defined Ext GoTo ChkExt

:GetExt
ClS
Set /P "Ext=Enter extension [without leading period]>"
If ErrorLevel 1 GoTo GetExt

:ChkExt
Set Ext | %SystemRoot%\System32\find.exe "." 1>NUL
If Not ErrorLevel 1 GoTo GetExt

:DelExt
%SystemRoot%\System32\choice.exe /M "Delete all %Ext% files in            
  • Related