I'm trying to write a .bat file to delete all files from a directory that look like "r" concatenated with a number of indeterminate length with a .sas7bdat ending.
e.g.: r2343.sas7bdat, r2309483.sas7bdat, etc.
The problem is, that I also have a file called "ranker_interface.sas7bdat", so I can't just do:
del "C:\temp\r*.sas7bdat"
I've tried Googling this up and down, but I haven't been able to figure it out. Is there any way of excluding a particular value from a wildcard?
for /L %%a in (0,1,9) do ECHO del r%%a*.sas7bdat
would possibly be easiest.
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.
If you are running this directly from the prompt rather than as a line in a batch file, change all %%a to %a.
This is the old school MSDOS way of getting around this:
attrib +h "C:\temp\ranker*.sas7bdat"
del "C:\temp\r*.sas7bdat"
attrib -h "C:\temp\ranker*.sas7bdat"
The attrib command hides the files you don't want deleted from the DEL command,
and then unhides them again.
To list down all files from folder that are like 'r*.sas7bdat', exclude the one from the list called ranker_interface.sas7bdat and delete the rest you can use this command from within batch file:
for /f "tokens=*" %%a in ('dir /b r*.sas7bdat ^| find /v "ranker_interface.sas7bdat"') do del /f /q %%a
It is written the way to be executed from the folder directly.
This will delete all 'r*.sas7bdat' but the one specified by find /v - exclusion, that can eventually be followed by another find /v exclusion, so something like:
for /f "tokens=*" %%a in ('dir /b r*.sas7bdat ^| find /v "ranker_interface.sas7bdat" ^| find /v "rxxxxx.sas7bdat"') do del /f /q %%a
Not the best way if you want to exclude more than one file. If you want to exclude more than one file, then you can put your excluded filenames into singel file and isntead of directly running del command further for followed by if statements can be done and compare the file if can be found in exclusion list file, that contain file name on each row. Batch can look like this then:
#echo off
for /f "tokens=*" %%a in ('dir /b r*.sas7bdat') do (
for /f "tokens=*" %%x in ('type exclusion.txt ^| find /c "%%a"') do (
if "%%x" EQU "0" (del /f /q %%a)
)
)
This will delete all files in your directory that are like 'r*.sas7bdat', except those listed down in file 'exclusion.txt'
Hope this helps
Related
I am trying to use a batch script to delete files that has a ( in its name. For example, the file that I would like to delete has the name "Test1 - Copy (5).txt".
I tried this command but it does not work:
for /f "delims=" %%a in ('
findstr /l /i /m /c:"(" "C:\Users\Desktop\New folder\*.*"
') do echo del "%%a"
Can you assist me in getting the correct code to delete the files that has a ( in its name? Thanks!
Usually when someone online gives you code that could be destructive (like code to delete a bunch of files), they'll preface the delete command with an echo so that you can see what commands would be run. In order to actually run the command, simply remove the echo:
for /f "delims=" %%a in ('findstr /l /i /m /c:"(" "C:\Users\Desktop\New folder\*.*"') do del "%%~a"
However, this is a lot of typing (and it's doing a case-insensitive search for an open parentheses for some reason), so you can simply use wildcards to delete any files whose name contain the string you're looking for:
del "%USERPROFILE%\Desktop\New folder\*(*"
I would like to recursively delete all files with a specific extension in a batch file.
I am aware of the following command:
del /s *.ext
However, this does on Windows also delete files with other extensions like e.g. .ext1 or .ext2 . The reason for this seems to be that the 8.3 file name of such a file ends with .ext and therefore also the files with longer extensions are matched.
I am looking for a replacement to the command above that recursively deletes all files with .ext extension but keeps files with longer extensions.
the where command works a bit differently (in regards to wildcards and short-names). Put a for /f loop around, and you're done. Your example would then translate to:
for /f "delims=" %%a in ('where /r . *.ext') do ECHO del "%%a"
Note: I disarmed the del command by just echoing it. Remove the ECHO after troubleshooting, when you are sure it does exactly what you want.
This also uses where.exe, but takes account of an issue not mentioned in another answer.
The issue is that where searches append each extension listed under %PATHEXT% to your .ext glob/spec. So whilst it will delete your target files, excluding files like .ext1 and .ext2 etc. it will now include for example, *.ext.com, *.ext.exe, *.ext.bat, *.ext.cmd, *.ext.vbs, *.ext.vbe, *.ext.js, *.ext.jse, *.ext.wsf, *.ext.wsh, and *.ext.msc etc.
The fix is to simply empty the content of %PATHEXT% before issuing the command. The following method does so within the For loop parenthsized command. As that is ran in another cmd.exe instance, it will not affect the instance in which the rest of your script resides:
#For /F "Delims=" %%G In ('"(Set PATHEXT=) & "%__APPDIR__%where.exe" /F /R "C:\SourceDir" "*.ext" 2>NUL"') Do #Del /A /F %%G
Obviously, you would modify, C:\SourceDir to contain the root location you require. The other current answers, use the current directory. If you want that, change it to ., or if you want the directory base as that of your batch file, change it to %~dp0.. Please do not remove any doublequotes.
Here are some alternative method examples, (please remember to adjust the drive/path/extension as needed)
If you wish to stick with the more traditional Dir command, then you could pipe the results through the findstr.exe utility, to exclude those matching the 8.3 names:
#For /F "Delims=" %%G In ('"Dir /B /S /A:-D "C:\SourceDir\*.ext" 2> NUL | "%__APPDIR__%findstr.exe" /I /L /E ".ext""') Do #Del /A /F "%%G"
You could also use the forfiles.exe utility for the task:
#"%__APPDIR__%forfiles.exe" /P "C:\SourceDir" /S /M "*.ext" /C "\"%__APPDIR__%cmd.exe\" /C \"If #IsDir==FALSE Del /A /F #File\""
Or this excruciatingly slow WMIC.exe utility method:
#"%__APPDIR__%wbem\WMIC.exe" DataFile Where "Drive='C:' And Path Like '\\SourceDir\\%%' And Extension='ext'" Delete 1> NUL 2>&1
Stephans answer is the shorter version, but you can use findstr's regex as well to match that the end of the name should be .ext
for /f "delims=" %%i in ('dir /b /s ^| findstr /IRC:"\.ext$"') do echo del "%%~i"
I'm new to writing batch files and I need to write one in order to delete a list of files please. I need the batch script to first locate the files (they are all based in different folders in the same drive) and then subsequently delete all of them. Is there a way that I can write a batch script to locate the files in a list and then delete them all automatically?
Apologies for how basic this question is - I am completely unsure how to go about this task!
#echo off
for /f "tokens=*" %%i in (list.txt) do (
for /f "tokens=*" %%j in ('dir /s /b /a-d "c:\%%i"') do echo del "%%j"
)
remove the echo, if the output satisfies you.
get every filename in your list (%%i)
. get every full path (%%j) for this filename (search through the disk) and delete it.
NOTE: this is not fast - searching the whole disk for every single filename needs some time.
If there are many filenames in your list, it may be better to build a complete direcotry-tree only once and write it to a file (dir /s /c /a-d >diskcontent.txt), working with that file instead of searching the disk for every file.
dir-parameters: /s search in all subdirectories, /b use a format that gives you the complete path, /a-d exclude directorynames; c:\ start in the root of drive C:
EDIT
if your list.txt contains spaces (either in the filename or in a given path), you have several options:
pushd "C:\path to\my file"
for /f "tokens=*" %%i in (list.txt) do (
or
for /f "tokens=*" %%i in ('type "C:\path to\my file\list.txt"' ) do (
or
for /f "usebackq tokens=*" %%i in ("C:\path to\my file\list.txt") do (
I am trying to create a batch file that will delete images with specific names. The images will have names such as
house-200x300.jpg
car-125x250.jpg
So what I need ideally is a regular expression to target files which end in -(Num1)x(Num2).jpg
Also, the images are in various folders and sub folders so I need to do this recursively from the parent folder.
Thanks
del /S *-???x???.jpg
Perhaps you may want to change del by dir /B command at first just to check that there is not any file that have not the specified file name format, but that will be selected by this wild-card.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*.jpg" ^|findstr /i /e /r /c:"-[0-9][0-9]*x[0-9][0-9]*\.jpg"'
) DO (
ECHO DEL "%%a"
)
GOTO :EOF
This should do the job - targeting only those filenames ending with -numXnum.jpg
You'd need to set your own sourcedir
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.
Have you considered using powershell?
Regular expressions in batch files get very bad very fast as the syntax is limited.
My experience is limited, so there may very well be a better solution than this:
#echo off
FOR /F "delims=?" %%i IN ('dir /B /S ^| findstr /R "[^\.]*[0-9][0-9][0-9]x[0-9][0-9][0-9].jpg"') DO (
del /s "%%i" >nul 2>&1
)
I'm redirecting the output as I think that FOR starts to get confused about the output of del.
I need help with a script that first finds all files in a directory with a certain string, then uses the filenames in a variable to be used in a script.
So:
Find files and filenames
Saves file?
Start some kind of loop? that changes a variable then executes the
belonging script
Repeat till all filenames have been used.
My code here..
#Echo off
For /r C:\work %%G In (*) Do #Findstr /M /S "string" > filenames.txt %%G
Set Var1=0
For %%G In (*) Do (
Var1=<filenames.txt (???)
script
script
I haven't writen "script" myself and friend help me with it, if you would like to see it do you need to wait until I can get to my other computer at home.
Thanks on beforehand!
Find files and filenames
Saves file
set "search=what I want to find"
(for /f "delims=" %%a in ('dir /a-d /b /s "C:\work" ^| findstr "%search%"') do echo (%%~fa)>filenames.txt
Start some kind of loop? that changes a variable then executes the belonging script
Repeat till all filenames have been used.
for /f "delims=" %%a in (filenames.txt) do (
REM here do something inside the loop
REM until all file names from filenames.txt were processed
)
This is designed to find files in c:\work that match a string, and echo the filenames.
#echo off
cd /d "c:\work"
for %%a in ("*string*") do (
echo "%%a"
)