0

I want to be able to rerun my code multiple times and I think my problems come from the variables. I can't figure where I have to put the setlocal and the endlocal. As you can see I put setlocal just before the variable but it's still not working.

setlocal
set "reply=g"
set /p "reply=Voulez vous garder ou supprimer un certain type de fichier ? [g|s]: "

if /i "%reply%" == "g" ( 
setlocal
set /p ext="Quelles extentions voulez vous garder (Ecrivez avec le point toutes les extentions) ? : "
for /f "delims=" %%F in ('dir /b /s /a-d "%_FolderName%"^| findstr /vile "%ext%"') do del "%%F" 
endlocal )

if /i "%reply%" == "s" ( 
setlocal
set /p ext="Quelles extentions voulez vous supprimer (Ecrivez avec le point toutes les extentions) ? : "
for /f "delims=" %%F in ('dir /b /s /a-d "%_FolderName%"^| findstr "%ext%"') do del "%%F" 
endlocal )

endlocal
blaztolen
  • 3
  • 3
  • The variable `ext` is set and read in the same block of code, so you'll need delayed expansion for it… – aschipfl May 23 '22 at 06:13
  • `endlocal` is rarely used other than to terminate a code section the changes the `delayedexpansion` status. Executing an `endlocal` undoes any environment changes made since the prior `setlocal`, restoring the environment to what it was when the `setlocal` was executed. It is normal to execute a `setlocal` immediately in a batch so that any changes made to the environment are undone when the batch ends, which is an implicit `endlocal`. Best approach imo is to `setlocal` at the start of a batch and then only use a `setlocal/endlocal` bracket (set of lines starting/ending...) if you have reason – Magoo May 23 '22 at 06:15

0 Answers0