Windows batch: del & exclude file

Hello experts,
I have the following command
Del C:\folder \*.csv* C:\folder2\*log.* /q /s 

Open in new window

I would like to exclude of the delete operation a specific file with the following name mapping.csv
If you have questions, please contact me.
Thank you.
LVL 1
LD16Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
You do this only by using a for loop over dir /b and an additional if.. In a batch:

@Echo Off

Set FILE_TO_SKIP=test.cs

For /F %%F In ( 'Dir /B *.*' ) Do (
    If Not %%F Equ %FILE_TO_SKIP% (
        Echo Processing %%F
        Rem TODO - The actual delete.
        Rem Del C:\folder \*.csv* C:\folder2\*log.* /q /s
    )
)

Open in new window

LD16Author Commented:
Thank you.
Not possible to do it in a single command through cmd?
Thank you.
Bill PrewIT / Software Engineering ConsultantCommented:
This seems to work:

for /f "tokens=*" %F in ('dir /s /b /a-d "B:\EE\EE29148036\dir1\*.csv" "B:\EE\EE29148036\dir2\*log.*"') do if /i "%~nxF" NEQ "mapping.csv" del "%~F"

Open in new window


»bp
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

slightwv (䄆 Netminder) Commented:
Have you considered PowerShell?

Something like:
Remove-Item C:\folder\*.csv*,C:\folder2\*log.* -Exclude mapping.csv
LD16Author Commented:
Thank you. What is the option for remove-item with drill down?
slightwv (䄆 Netminder) Commented:
I find the documentation is the best place to locate available options:
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-item?view=powershell-6

-Recurse

Indicates that this cmdlet deletes the items in the specified locations and in all child items of the locations.

When it is used with the Include parameter, the Recurse parameter might not delete all subfolders or all child items. This is a known issue. As a workaround, try piping results of the Get-ChildItem -Recurse command to Remove-Item, as described in "Example 4" in this topic.
Bill PrewIT / Software Engineering ConsultantCommented:
And just to follow on to a comment I made on another question of yours recently, here is the simple one line command you could use if you had Swiss File Knife available on your machine.


sfk del -dir "B:\EE\EE29148036\dir1" -file *.csv !mapping.csv -dir "B:\EE\EE29148036\dir2" -file *.log !mapping.csv

Open in new window


»bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LD16Author Commented:
Thank you very much for those proposal.
@slightwv (䄆 Netminder): I tested your proposal but I modified:
C:\folder2\*log.*

Open in new window

by:
C:\folder2\*.log

Open in new window

Could you please modify your proposal so I can add your proposal as solution.
@Bill: your proposals works: Thank you again for your advice concerning sfk. I already installed and placed in C:\System32 to use it as cmd.
The tricky part is to select the most useful command and memorize them. :-)
Thank you again.
slightwv (䄆 Netminder) Commented:
>>Could you please modify your proposal so I can add your proposal as solution.

Modify in what way?  You have the basic command.  You should just need to tweak it to meet your final requirements.  If you can tell me the requirements it is missing, I can try to post a copy/paste solution.
LD16Author Commented:
Modify your proposal as extension should be like this *.extension*.
But know it is fine I understood how to proceed.
Thank you again.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.