1.

Solve : Delete Folders With a Specific Name or With Specific Contents?

Answer»

I am interested in adding to an EXISTING batch script some code that will look for folders with a particular name and delete them. In this case the folders are named like this:

dfd87.tmp
baols.tmp
08ds2.tmp

I have no idea how to accomplish this. So I decided to try a different approach. Each of those folders contains a .vbs file. So I thought I could use this:

Code: [Select]For /F "tokens=*" %%G IN ('dir /s /a-d *.vbs') DO RMDIR /S /Q %%G

This should do the job however the script won't be running from the %userprofile%\AppDATA\Local\Temp folder. So what I'm interested in knowing is

1. How can I SEARCH for subdirectories of %userprofile%\AppDATA\Local\Temp with names that include .tmp and delete them
2. How can I reference a particular directory in the code provided above. I tried this Code: [Select]For /F "tokens=*" %%G IN ('dir %userprofile%\AppDATA\Local\Temp /s /a-d *.vbs') DO RMDIR /S /Q %%G but am finding that a folder that does not have any .vbs files in it is being deleted . . .

Thanks for the help.

MJtry
Code: [Select]@echo off
for /f "delims=" %%A in ('dir "%userprofile%\AppDATA\Local\Temp" /b ^| find ".tmp"') do (
for /f "delims=" %%B in ('dir /b "%userprofile%\appdata\local\temp\%%A"') do (
if "%%~xB"==".vbs" echo "%userprofile%\appdata\local\tmp\%%A"
)
)

This will echo the pathways to the folders, you should double check them before changing 'echo' to 'rd'.

This was tested with no .vbs in any .tmps, so it might not work properly.This is untested - it should remove every folder in %temp% which contains a *.vbs file and has a .tmp on the end of the folder name.

Code: [Select]@echo off
for /f "delims=" %%a in ('dir "%temp%\*.vbs" /b /s /a-d ') do (
for /f "delims=" %%b in ("%%~dpa\.") do (
if /i "%%~xb"==".tmp" rd /s /q "%%~dpa" 2>nul
)
)
pause
Thank you both for your help. I took notes on what you wrote for future use and added this to my script as a result:

Code: [Select]for /f "delims=" %%A in ('dir "%userprofile%\AppDATA\Local\Temp" /b ^| find ".tmp"') do rd /s /Q "%userprofile%\AppDATA\Local\Temp\%%A" 2>NUL
I found it STRANGE that I couldn't remove the %%A directories without referencing the full path to them but it wasn't until I entered the full path that I stopped getting errors so I guess there's something to it.

Thanks again,

MJQuote from: powlaz on June 13, 2013, 12:17:05 PM

I found it strange that I couldn't remove the %%A directories without referencing the full path to them

The bare folder names will not be found by the batch unless you are running it in the folder "%userprofile%\AppDATA\Local\Temp




Quote from: powlaz on June 13, 2013, 12:17:05 PM
Code: [Select]for /f "delims=" %%A in ('dir "%userprofile%\AppDATA\Local\Temp" /b ^| find ".tmp"') do rd /s /Q "%userprofile%\AppDATA\Local\Temp\%%A" 2>NUL

You will GET errors trying to RD a filename.


Discussion

No Comment Found