-5

I have a folder named G:\Project\Projects

This folder has some(about 20) unknown subfolders. Some of the subfolders have a folder named SSS and a file called deleteme.doc

I want to delete all these SSS folders(with contents) and deleteme.doc files using a batch file or powershell file. I do not want to delete these files or folder deeply nested into the subfolders, just those who are directly under subfolder.

For example I want to delete this folder

G:\Project\Projects\proj 1\sss

but not these 3-

G:\Project\Projects\proj 1\other\sss

G:\Project\Projects\sss

G:\Project\Projects\proj 1\sss (it is a file)

The thing is I want to simply double click-run the batch/powershell file from anywhere, possibly from another partition or drive (or even network computer).

I tried RMDIR G:\Project\Projects\*\SSS it didn't seem to be working.

Please explain your answer if possible, also show me commands for both recycle bin delete and parma delete. Though I think sending to recycle bin is not possible without powershell.

Administrator privilege is not necessary to delete folders in non-system partition right?

Windows 10. powershell version 5

Maroun
  • 94,125
  • 30
  • 188
  • 241
Najeeb Taher
  • 45
  • 2
  • 2
  • 7

3 Answers3

3
@ECHO Off
SETLOCAL
:: remove variables starting $
FOR  /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="

:: set defaults
SET "$dirname=sss"
SET "$filename=deleteme.doc"
SET "$from=c:\wherever\you\like"

:: analyse command line
:: syntax file="filename" " dir=dirname" from="dirname" (/switches)
:: each is optional. default is as established above
:: /d  - delete directory only if it is completely empty
:: /dn - delete directory only if it is not completely empty
:: /ds - delete directory only if it is completely empty or if no files in its subdirectory tree
:: /fe - delete file if empty
:: /fn - delete file if not empty
:: /b  - both file and directory must exist to make deletions
::
FOR %%a IN (%*) DO IF DEFINED $twopart (CALL :parttwo %%a) ELSE (
 SET "$processed="
 IF /i "%%a"=="dir"     SET "$processed=Y"&SET "$twopart=$dirname"
 IF /i "%%a"=="file"    SET "$processed=Y"&SET "$twopart=$filename"
 IF /i "%%a"=="from"    SET "$processed=Y"&SET "$twopart=$from"
 IF /i "%%a"=="/b"      CALL :setswitch b
 IF /i "%%a"=="/d"      CALL :setswitch d
 IF /i "%%a"=="/dn"     CALL :setswitch dn
 IF /i "%%a"=="/ds"     CALL :setswitch ds
 IF /i "%%a"=="/fe"     CALL :setswitch fe
 IF /i "%%a"=="/fn"     CALL :setswitch fn
 IF NOT DEFINED $processed CALL :extras %%a
)

IF DEFINED $extras ECHO(%$extras% NOT understood)&pause&GOTO :EOF 

:: resolve switch compatibility

IF DEFINED $d  IF DEFINED $dn SET "$incompatible=/d /dn"
IF DEFINED $fe IF DEFINED $fn SET "$incompatible=%$incompatible% /f /fn"

IF DEFINED $incompatible ECHO(%$incompatible% incompatible)&pause&GOTO :EOF 

:: if $from is set, make it quoted.
IF DEFINED $from SET "$from="%$from:"=%""

:: Now search for the directory
FOR /d /r %$from% %%a IN ("%$dirname%") DO IF EXIST "%%a\." (
 SET "$victim=%%a"
 CALL :deldir
)
IF NOT DEFINED $b FOR /r %$from% %%a IN ("%$filename%") DO IF EXIST "%%a" (
 SET "$victim=%%~dpa%$filename%"
 CALL :delfile
)

GOTO :eof

:: delete the victim directory if all other conditions are met
:: take care of file as well if required.
:deldir
SET "$victim=%$victim:"=%"
IF DEFINED $b IF NOT EXIST "%$victim%\..\%$filename%" GOTO :eof
:: if the directory has any contents and "/d" was specified, skip deletion
IF DEFINED $d (
 FOR /f %%z IN ('dir /b "%$victim%" 2^>nul') DO GOTO :eof
)
:: if the directory has files in its subtree and "/ds" was specified, skip deletion
IF DEFINED $ds (
 FOR /f %%z IN ('dir /s /b /a-d "%$victim%"  2^>nul') DO GOTO :eof
)
:: if the directory has no files and none in its subtree and "/dn" was specified, don't skip deletion
IF DEFINED $dn (
 FOR /f %%z IN ('dir /b "%$victim%"  2^>nul') DO GOTO deldir1
 GOTO :eof
)
:: delete the directory
:deldir1

ECHO(DEL /s /f /q "%$victim%"
SET "$victim=%$victim%\..\%$filename%"
IF EXIST "%$victim%" GOTO delfile 
GOTO :eof

:delfile
FOR %%z IN ("%$victim%") DO (
 IF DEFINED $fn IF "%%~zz"=="0" GOTO :EOF 
 IF DEFINED $fe IF "%%~zz" neq "0" GOTO :EOF 
)
ECHO(DEL /f /q "%$victim%"
GOTO :eof



:parttwo
SET "%$twopart%=%~1"
SET "$twopart="
GOTO :eof

:extras
SET "$extras=%$extras% %~1"
GOTO :eof

:setswitch
SET "$processed=Y"
SET "$%1=Y"
SHIFT
IF "%1" neq "" GOTO setswitch
GOTO :EOF

Here's a general solution which you'd need to test before unleashing.

The required RD commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(RD to RD to actually delete the directories.

The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(DEL to DEL to actually delete the files.

The first section analyses the parameters ans switches supplied to the program.

The defaults are shown and need to be adjusted to suit your circumstances.

The command may be run as

*thisname* from=c:\new\location dir=xxx file=hello.txt /d

where from specifies where the tree-walk begins, fie the filename to be located and dir the directoryname to delete.

The switches are also listed.

The program simply examines the command line for elements of interest and sets values as required.

We then traverse the tree, looking for the directoryname and then make decisions depending on the switch-settings.

Then traverse the tree again, looking for the filename.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Wow. You really put forth some effort to make a flexible and easy to use utility. It is a trade off with making it easy to understand how the various criteria are satisfied - the basic mechanics are somewhat obscured by the slick option processing that you have packed into the one script. – dbenham Mar 20 '16 at 15:20
1

Something like

for /f %A in ('dir "c:\somewhere\deleteme.doc"') Do echo rd "%~dpA"

Administrator privilege is not necessary to delete folders in non-system partition right?

How would we know what permissions you have. Type icacls c:\somewhere to see.

You need to know your problem.

0

I created a short powershell solution using this-

$targetName='ss s';
foreach ($file in Get-Childitem "G:\Project\Projects\" )
{
   $fname=$file.name;
   if (Test-Path "G:\Project\Projects\$fname\$targetName\")
        { 
            $shell = new-object -comobject "Shell.Application"
            $item = $shell.Namespace(0).ParseName("G:\Project\Projects\$fname\$targetName")
            $item.InvokeVerb("delete")

        }
}

This powershell script sends that folder to recycle bin after confirmation popup. (this won't send any file named 'ss s')

this seems to be working for batch file script-

set "b=ss s"
for /d %%a in (*) do  IF EXIST "%%a\%b%\*" (rmdir /s "%%a\%b%")

if the targetfolder was named "$ss s" then you have to set variables as "b=/$ss s"

Community
  • 1
  • 1
Najeeb Taher
  • 45
  • 2
  • 2
  • 7