Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

52. Imagine you have a folder full of 500 subfolders. 250 of the subfolders cont

ID: 3773563 • Letter: 5

Question

52. Imagine you have a folder full of 500 subfolders.

250 of the subfolders contain .pdf's, .txt's, and .mp3's, 150 of the subfolders contain .txt's and .mp3's, and 100 of the subfolders contain only .mp3's. Write a program so that it automatically deletes the folders NOT containing .txt files.

Do not just write a program that deletes subfolders only containing .mp3's.

Thank you in advance! The directory of which all of the folders will be in is "C:UsersUserExample1" and the code is supposed to be written in batch, but other languages are fine as long as they work.

Explanation / Answer

Write a program so that it automatically deletes the folders NOT containing .txt files. @echo off Title DeleteFilesFromSubfolder color 0A SETLOCAL ENABLEDELAYEDEXPANSION set targetFolder="C:UsersUserExample1" REM targetFolder is the location where you want to delete from .------------------------------- set Index=1 for /d %%D in (%targetFolder%*) do ( set "Subfolders[!Index!]=%%D" set /a Index+=1 ) set /a UBound=Index-1 for /l %%i in (1,1,%UBound%) do echo. %%i. !Subfolders[%%i]! :choiceloop echo.------------------------------- set /p Choice= ERRORS Search in: if "%Choice%"=="" goto chioceloop if %Choice% LSS 1 goto choiceloop if %Choice% GTR %UBound% goto choiceloop set Subfolder=!Subfolders[%Choice%]! goto start :start TITLE Delete Files which is text form - %Subfolder% IF NOT EXIST %ERRPATH% goto notExist IF EXIST %ERRPATH% echo.%ERRPATH% Exists - delete files... echo.Searching for .txt files... pushd %ERRPATH% for /r %%a in (*.txt) do ( echo "%%a" "%Subfolder%%%~nxa" ) popd echo. echo. verIFy >nul echo.Execute^? choice /C:YNX /N /M "(Y)Yes or (N)No:" IF '%ERRORLEVEL%'=='1' set question1=Y IF '%ERRORLEVEL%'=='2' set question1=N IF /I '%question1%'=='Y' goto execute IF /I '%question1%'=='N' goto end :execute echo.%ERRPATH% Exists - delete files... echo.Searching for .txt files... pushd %ERRPATH% for /r %%a in (*.txt) do ( del "%%a" "%Subfolder%%%~nxa" ) popd goto end :end echo. echo. echo. deleting files Finished from %subfolder% pause goto choiceloop ENDLOCAL exit

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote