سؤال

Hoping i can have some help with a batch file i am making.

My batch file currently scans a directory and notes all the folders, subfolders and exports them in a list.txt file, by doing the following:

dir /a "special\program\"/s /b /-p /o:gen >list.txt

And it works fine. However what i now want to do is scan the list.txt file and delete certain filenames from the path in the .txt file, i don't want to delete filetypes but instead delete a part of the name.

Because basically theirs over 50 files in each folder and some of the files may contain the name example: enter image description here

Look at the files: file_kool.rar, fileOk.rar, somethingRANDOM.rar, i would like to delete the files with out the name "file" in them so "somethingRANDOM.rar, excluding file_kool.rar and fileOk.rar, but i am not sure how to add the exclusions because i cannot specify every filename to delete as their is way too many so i am guessing theirs a way i can use a Wildcard to exclude .rar files with "file" in them and only delete the unnoted .rar files?

I tried this:

@echo off
::Check Directories, Sub-Folders and list all files within and save to a list.txt file
dir /a "special\program\"/s /b /-p /o:gen >list.txt
::Read list.txt and delete all .rar files accept for the exception ones with "file" in them
for /F "tokens=*" %%A in (list.txt) do del /s special\program\ *file? 

Or instead of this is their a way in robocopy to exclude file: IDEA#2:

robocopy "program" "special\program" /e /xf *something.rar* *.rar /xd random random2 *file?

I tried this one which is the preferred choice i am after but i couldn't workout how in the exclusions to add the fact that i want to delete all .rar files accept for ones that contain "file"(exclusions).

Does anyone have a workaround. I am sure their is something simple here i am missing?

NOTE: I have fixed my question sorry everyone, as i typed it out i confused myself.

هل كانت مفيدة؟

المحلول

The Batch file below deletes all files with names that start with "file" placed inside "special\program" folder:

cd "special\program"
del /S file*.*

If you want to delete files with names that contain "file" at any place, just change the wild-card by: *file*.*

EDIT: Reply to the comment

In your question you said: "i would like to delete the files with the name "file" in them", so I thought you want to delete the files that have "file" in any part of the filename. Now you said in a comment that you want the reverse thing! I strongly encourage you to edit your question and make this point clear including the fact that you changed the original question; otherwise the people that may read this topic will be confused until they read your small comment at the very end of the topic... :-(

Anyway, the Batch file below do what you want (I hope so), that is: delete all .rar files in "special\program" folder excepting those that have "file" substring in any part of the file name.

@echo off
setlocal EnableDelayedExpansion

for /R "special\program" %%a in (*.rar) do (
   set "filename=%%~Na"
   if "!filename:file=!" equ "%%~Na" del "%%a"
)

نصائح أخرى

dir /a "special\program\"/s /b /-p /o:gen |find /v /i "file" >list.txt

should do what you appear to be asking.

Examples of data as you have it and as you want it go a long way....

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top