Please, I need a BATCH to delete all folders, except specific folders and sub-folders


Decopi

New member
Local time
5:31 PM
Posts
8
OS
Windows 11
Hi,
I have a SSD with several partitions.
I need to work at J:\ and I need to delete everything inside J:\, except the following folders:

J:\System Volume Information
J:\$RECYCLE.BIN
J:\New
J:\Old Stuff

And the following sub-folders:

J:\Papers\Folder
J:\Room\Main Documents
J:\Year\Origin\Part
J:\Year\Origin\Part Two

After searching and reading a lot, I wrote the following code:

@ECHO OFF
SETLOCAL
SET "sourcedir=J:\"
SET "keepdir=System Volume Information"
SET "keepdir=$RECYCLE.BIN"
SET "keepdir=New"
SET "keepdir=Old Stuff"
SET "keepdir=Papers\Folder"
SET "keepdir=Room\Main Documents"
SET "keepdir=Year\Origin\Part"
SET "keepdir=Year\Origin\Part Two"
FOR /d %%a IN ("%sourcedir%\*") DO IF /i NOT "%%~nxa"=="%keepdir%" RD /S /Q "%%a"
GOTO :EOF

Unfortunately, the code above doesn't work (it deletes everything inside J:\).
Please, I will appreciate any help.
Thank you in advance!
 
Windows Build/Version
Latest Windows 11

My Computer

System One

  • OS
    Windows 11
Copy files/folders you wish to keep tp another drive, format drive, copy files/folders back?
 

My Computer

System One

  • OS
    Windows 10 Pro + others in VHDs
    Computer type
    Laptop
    Manufacturer/Model
    ASUS Vivobook 14
    CPU
    I7
    Motherboard
    Yep, Laptop has one.
    Memory
    16 GB
    Graphics Card(s)
    Integrated Intel Iris XE
    Sound Card
    Realtek built in
    Monitor(s) Displays
    N/A
    Screen Resolution
    1920x1080
    Hard Drives
    1 TB Optane NVME SSD, 1 TB NVME SSD
    PSU
    Yep, got one
    Case
    Yep, got one
    Cooling
    Stella Artois
    Keyboard
    Built in
    Mouse
    Bluetooth , wired
    Internet Speed
    72 Mb/s :-(
    Browser
    Edge mostly
    Antivirus
    Defender
    Other Info
    TPM 2.0
cereberus is correct. Copy those folders elsewhere then deal with J:\.

There are several faults with your script.
  • You are using For, one of the most powerful & sophisticated processing commands, and would have to restructure your script into a subroutine looping structure for your Set KeepDir commands to have the effect you want.
  • Whenever you try out a batch script, you should look at the window it runs in to gain valuable debugging info. You'd have seen, just for example, how it was interpreting & dealing with "%sourcedir%\*" and the fact that only your last Set KeepDir line was being used.

my ditty Batch file and PowerShell guides [post #7] - ElevenForum



All the best,
Denis
 

My Computer

System One

  • OS
    Windows 11 Home x64 Version 23H2 Build 22631.3447
.. and Robocopy is your friend.
 

My Computers

System One System Two

  • OS
    Windows 11 22H2 (latest update ... forever anal)
    Computer type
    PC/Desktop
    Manufacturer/Model
    HP Slim S01
    CPU
    Intel i5-9400
    Memory
    8GB
    Graphics Card(s)
    NVIDIA GeForce GT730
    Sound Card
    OOBE
    Monitor(s) Displays
    Acer 32"
    Screen Resolution
    1920x1080
    Hard Drives
    2 x 1TB SSDs
    PSU
    OOBE
    Case
    OOBE
    Cooling
    OOBE
    Keyboard
    Logitech wireless
    Mouse
    Logitech wireless
    Internet Speed
    Classic Australian w.a.p.
    Browser
    Brave
    Antivirus
    KIS
  • Operating System
    Windows 11 Pro (latest upadte ... anally always)
    Computer type
    Laptop
    Manufacturer/Model
    HP Pavillion 15
    CPU
    i7-1165G7 @ 2.80GHz
    Graphics card(s)
    Intel Iris Xe Graphics
    Hard Drives
    Samsung NVMe 512GB
    + numerous/multiple SSD Type C USB enclosures
    Internet Speed
    NBN FTTN 50
    Browser
    Brave
    Antivirus
    KIS
Copy files/folders you wish to keep tp another drive, format drive, copy files/folders back?

Thank you @cereberus for your replay.
Unfortunately I can't do that because my J: is huge in size. The copy/copy back you suggest will take time and lot of I/O.
I need a more efficient approach, something similar to my code above, a batch that directly deletes everything inside J: except folderA, folderB, folderB/folderB2 etc
Hope you can help me.
Thank you again!
 

My Computer

System One

  • OS
    Windows 11
cereberus is correct. Copy those folders elsewhere then deal with J:\.

There are several faults with your script.
  • You are using For, one of the most powerful & sophisticated processing commands, and would have to restructure your script into a subroutine looping structure for your Set KeepDir commands to have the effect you want.
  • Whenever you try out a batch script, you should look at the window it runs in to gain valuable debugging info. You'd have seen, just for example, how it was interpreting & dealing with "%sourcedir%\*" and the fact that only your last Set KeepDir line was being used.

my ditty Batch file and PowerShell guides [post #7] - ElevenForum



All the best,
Denis

Thank you Denis.
As I explained to cereberus, unfortunately I can't use his suggestion.
And with regards to your comment, I apologize, I know almost nothing about BATCH files, I tried to read here and there, I copied/pasted some stuff, but I'm blind, I don't understand what I'm doing. And sadly, I don't have time to learn about batches, I need the batch and that's all.
It'll be great if you can help me to fix the syntax of my wrong code.
Thank you for your patience!
 

My Computer

System One

  • OS
    Windows 11
Sorry but I don't have the time. Your script needs a complete re-write.

Your reply to cereberus worries me. It implies that you do not have a backup drive capable of holding all your files.
  • That's a very dangerous position to be in & I think getting an external disk for backups should be your highest priority.
  • In addition to holding your files, it should also be big enough to hold more than one system image. Do you know what I mean by system image?

Having an external backup disk also means that the solution for your current task is simplified so you won't have to learn any scripting.


Denis
 
Last edited:

My Computer

System One

  • OS
    Windows 11 Home x64 Version 23H2 Build 22631.3447
KISS!
I use batch files a lot, and have for over 40 years, but one rule I always adhere to is, "Keep it as simple as possible".
In your case, I'd just write a batch file with a line for each folder or specific file I wanted to delete, and leave the rest alone.
Anything you don't mark for deletion will be retained, by default.

I basically do that, in my own "Cleanup.bat" program, where I specify certain folders containing junk files that I want to get rid of. Or even certain file types, like:

Del /F /S /Q C:\*.tmp
Del /F /S /Q C:\*.bac
Del /F /S /Q C:\*.old


In the above text, only those specific files will be deleted, anywhere they show up in the C: drive, unless they are open in another process.

Your batch file will be successful if only you specify exactly the files and/or folders that you want to get rid of.
It may require several lines, but you only have to write it once, and it will take less time than you've already spent here.
Give it a try!

Happy Father's Day,
TM :cool:
 

My Computer

System One

  • OS
    Win-11/Pro/64, Optimum 11 V5, 23H2 22631.3374
    Computer type
    PC/Desktop
    Manufacturer/Model
    Home Made w/Gigabyte mobo/DX-10
    CPU
    AMD FX 6350 Six Core
    Motherboard
    Gigabyte, DX-10, GA-78LMT-USB3
    Memory
    Crucial, 16 GB
    Graphics Card(s)
    NVIDEA GeForce 210, 1GB DDR3 Ram.
    Sound Card
    Onboard
    Monitor(s) Displays
    24" Acer
    Screen Resolution
    1280x800
    Hard Drives
    Crucial SSD 500GB, SanDisk 126GB SSD, Toshiba 1TB HD
    PSU
    EVGA 500 W.
    Case
    Pac Man, Mid Tower
    Cooling
    AMD/OEM
    Keyboard
    101 key, Backlit/ Mechanical Switches/
    Mouse
    Logitech USB Wireless M310
    Internet Speed
    Hughes Net speed varies with the weather
    Browser
    Firefox 64x
    Antivirus
    Windows Defender, Super Anti Spyware
    Other Info
    Given to me as DEAD, and irreparable.
    Rebuilt with Gigabyte mobo, AMD cpu, 16GB ram and 500GB Crucial SSD.
To b
Sorry but I don't have the time. Your script needs a complete re-write.

Your reply to cereberus worries me. It implies that you do not have a backup drive capable of holding all your files.
  • That's a very dangerous position to be in & I think getting an external disk for backups should be your highest priority.
  • In addition to holding your files, it should also be big enough to hold more than one system image. Do you know what I mean by system image?

Having an external backup disk also means that the solution for your current task is simplified so you won't have to learn any scripting.


Denis
To be fair, Depends on what you use you're pc for, For example, i've just clean installed from scratch after about a year,, Lost the lot(Happily) everything back to normal within an hour, downloads for games etc aren't a big deal for me. Depends what you hold on you're pc./
 

My Computer

System One

  • OS
    11
    Computer type
    PC/Desktop
    Manufacturer/Model
    Custom
    CPU
    10700k@5.2
    Motherboard
    Gigabyte Gaming X Z490
    Memory
    Viper Steelseries 32gb@ 3600mhz
    Graphics Card(s)
    Gigabyte 2070 Super 8GB, +200 core + 600 memory
    Monitor(s) Displays
    ASUS 4k HDR, Two 1080p Benq and Samsung
    Screen Resolution
    3840x2160/2560x1440/1920x1080
    Hard Drives
    Adata XPG SX8200 PRO 1tb
    Samsung EVO 870 500GB
    PSU
    Corsair RX 650
    Case
    NZXT h510
    Cooling
    CM HYPER 212 RGB
    Keyboard
    Razer Ornata Chroma
    Mouse
    Steelseries Rival 710
I had a great help in other forum. I almost solved 99% of my needs. I share with you here, in case is useful to someone:

This batch will format F: (more efficient and fast than command delete). Then it will copy everything inside E: (except Folder One and Folder Two) to F:
@echo off
Format F: /FS:NTFS /V:EM2 /Q /X /y
robocopy.exe E:\ F:\ . /mir /XD "Folder One" "Folder Two" /copy:DAT /dcopy:DA /r:0 /v /xj /mt:32

And this batch will copy everything inside I: (except Folder One and Folder Two) and then it will copy to J:
This batch is amazing because only pastes (at J:) new or changed files.
@%SystemRoot%\System32\robocopy.exe I:\ J:\ /E /MIR /MT:32 /NDL /NFL /NJH /NJS /R:0 /XJ /XD "I:\Folder One" "I:\Folder Two"

Another different version:
@echo off
md J:\empty
robocopy /mir J:\empty J:\ /xd "Folder One" "Folder Two"
robocopy.exe I:\ J:\ . /mir /XD "Folder One" "Folder Two" /copy:DAT /dcopy:DA /r:0 /v /xj /mt:32

All batches are tested and working like a charm.
Hope it'll help.
 

My Computer

System One

  • OS
    Windows 11
Hello @Decopi,

Yes, RoboCopy is VERY powerful. It may be worth you getting better aquainted with it.

 RoboCopy:


NOTE:- To view RoboCopy Commands in aCMD Prompt, type robocopy /? and press Enter.

To output the Commands to a .txt file, type robocopy /? > %UserProfile%\Desktop\RoboCopy.txt. Obviously change the outputPath to your own.

I hope this helps.
 

My Computer

Back
Top Bottom