简体   繁体   中英

How to delete all files in a directory and subdirectories except for certain folders?

Each month, I print a number of reports. After printing, I ftp them to a website. To avoid ftping the same files each time, I want to delete all the files in the directory. Not all the folders need to have files deleted, however. Is it possible to delete all the files in a directory and subdirectories excluding 3 or 4 folders?

I know you can do something like:

del /s /q "C:\Users\Owner\OneDrive\test\*.*"

to delete all the files. I am very unfamiliar with cmd, and I know it's possible to use if statements, but I'm not sure how they are syntaxed. Ideally, I would have something that works like:

if(folderName != "Batchsheets" || folderName != "Statements" || folderName != "Histories")
{
   folder.delete();
}

Any help would be appreciated, thanks!

Edit: The folder structure looks like this: The main directory I'll be deleting from looks like this:

C:\\Users\\Owner\\SharePoint\\EDSIReports - Reports\\Reports

Inside that folder

I have something like 50 folders. All of the folders I want to delete are just numbers (examples: 1007, 1008, 1087, 12100, 20100). The ones I want to leave have names like Batchsheets, Histories, Statements, BlankStatements, Invoices, and InterestPaid). So I want to delete all of the pdfs stored in the folders with digit names like 1008 without deleting the files in Batchsheets.

One easy way of doing it is to use robocopy . The idea is to create an empty folder and sync this empty folder over your folder structure. This will remove anything (there is nothing in the source, so, syncthing it makes the target empty). Now the trick is to exclude the required directories from the operation.

cd /d "C:\Users\Owner\SharePoint\EDSIReports - Reports\Reports"
md _empty_ & robocopy _empty_ . /nocopy /purge /xd Batchsheets Statements Histories BlankStatements Invoices InterestPaid /L

If you prefer to remove the all digit folders, then you can use

for /d %a in (*) do @(for /f "delims=0123456789" %b in ("%a") do @break) || echo rmdir /s /q "%a"

For each folder, if removing all digits (we will use them as delimiters) leave us with an empty string the inner for /f will fail (it has nothing to process) and the conditional operator || (execute next command if the previous failed) will execute the rmdir

Or, we can enumerate the all digit folders and remove them

for /f "delims=" %a in('dir /ad /b ^| findstr /x "[0-9]*"') do @echo rmdir /s /q "%a"

note:

  • all commands have been written to be used in the command line. To use inside a batch file, the for replaceable arguments ( %a and %b ) need to be escaped, doubling the percent signs ( %%a , %%b )

  • The rmdir commands have beed preceded by a echo to avoid problems while debugging. If the output is correct, remove the echo

  • The robocopy command includes a /L swith. It will make the robocopy command list what will be processed. If the output is correct, remove the /L switch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM