Home > OS >  I need to find the name of a folder and put it into a variable in a batch script so i can delete a f
I need to find the name of a folder and put it into a variable in a batch script so i can delete a f

Time:10-13

I'm trying to make a simple helper script to avoid manually navigating directories since i have to do this specific task quite regularly.

The purpose: To delete a specific folder without knowing the name of the parent directory

Example directory: C:\Users\ %USERNAME%\AppData\Local\Name With Spaces\nws-alpha-0.00.abc_0\shaders

Explain: I would like to delete that "shaders" folder without knowing the entire "nws-aplha..." folder-name because it changes with every update, and I don't feel like changing the directory in the code every time there's an update. Otherwise this minimally useful script becomes pointless lol

My Issue: After the default directory deletion prompt "Are you sure you want to delete this? (Y/N)" the script throws the error that "The system cannot find the path specified." Even if you "echo" the output of the variable %FolderPath% to the screen, it appears correctly as the entire path but wont actually delete it, Admin session or not.

My Code:

@echo off
:Helper
::Put Folder path into variable
for /d /r "C:\Users\%USERNAME%\AppData\Local\Name With Spaces\" %%a in (*) do if /i "%%~na"=="shaders" set "FolderPath=%%a" 

::Notify user and escape function if folder doesn't exist
if "%FolderPath%" EQU "" echo "Shader" Folder does not exist. Perhaps it has already been deleted && TIMEOUT /T 5 && goto :MainMenu

del "%FolderPath%"

::echo %FolderPath%
::Output of echo^: C:\Users\%USERNAME%\AppData\Local\Name With Spaces\nws-alpha-0.00.abc_0\shaders
pause
exit

I also tried the solution found here "https://stackoverflow.com/questions/44985135/i-need-a-batch-program-to-find-the-folder-name-inside-another-folder" But i still get the same error "The system cannot find the path specified."

CodePudding user response:

Consensus: I'm an idiot lmao

change del "%FolderPath%" to rmdir /s /q "%FolderPath%"

i completely forgot that del is for files and not directories

Thanks Magoo!

CodePudding user response:

To find a folder, just type some initial letters of the folder and end with * Example:

cd nws-*
  • Related