1.

Solve : Delete files taht are not AVI?

Answer»

A paid service downloads video files in AVI format. Along with the videos come other files that are no longer needed after the download is finished.
How can a simple script delete just the files that are not of type nAVI in the sub folders in the download directory.?
I'd just perform the following

COPY or MOVE your *.AVI files to a different directory. * I like copying vs move, but if your tight on hdd space you can tell it to move the files instead of making a 2nd copy at the alt location.

DELETE all contents at thisoriginal directory that the AVI's were copied or moved from, you can then copy or move your AVI files back to original directory or keep them at this AVI only alternate directory.

This run from the location that the AVI's reside would create a copy of them as well as any subfolders containing them and only AVI files to be copied to an alternate location such as C:\my_AVIs\ . I would create this drop directory before running this instruction so that the location to copy files to is satisfied.

Code: [Select]xcopy *.avi c:\my_AVIs\*.* /s/d/yThen run

Code: [Select]erase *.* /f /q to force deletion of all files from the original location ( THIS DELETES ALL FILES from directory by which it is run without prompting to confirm you want to do this, so be careful )

If you want to be prompted you can just use Code: [Select]erase *.* /f and select Y or N to proceed

If you need to delete most data, but also keep some data at the parent directory, you would want to perform a custom deletion of file types by extensions such as if you have a parent directory with AVI , TXT, and DOC files in addition to a program with an EXE there and BIN, DLL, LOG, and other files there, you will not want to run the erase instruction above as for it would wipe out all contents and you need to retain some.

So to ditch the TXT and DOC files for example you would simply run the following:

Code: [Select]erase *.doc
erase *.txtThis one-line batch script will delete all files in its own folder that do not have the extension .avi

Save it with the .bat extension

@for %%A in (*.*) do if not "%%~xA"==".avi" del "%%A"

or open a prompt in that folder and paste this

@for %A in (*.*) do if not "%~xA"==".avi" del "%A"




Salmon Trout, great pone-line solution.
Thank you both. You have MADE my day.Quote from: Salmon Trout on May 25, 2013, 02:41:55 PM


@for %%A in (*.*) do if not "%%~xA"==".avi" del "%%A"


Add the /i switch or it will delete .AVI files or .aVi files etc. The CASE insensitive switch will fix that.

Code: [Select]for %%A in (*.*) do if /i not "%%~xA"==".avi" del "%%A"Quote from: foxidrive on May 25, 2013, 10:47:23 PM
Add the /i switch or it will delete .AVI files or .aVi files etc. The case insensitive switch will fix that.

Code: [Select]for %%A in (*.*) do if /i not "%%~xA"==".avi" del "%%A"
Cool ! Didn't know.Quote from: foxidrive on May 25, 2013, 10:47:23 PM
Add the /i switch or it will delete .AVI files or .aVi

I can't imagine that mattering.
Not mattering if it deletes your collection of AVI files?

Quote from: foxidrive on May 26, 2013, 02:22:03 AM
Not mattering if it deletes your collection of AVI files?

Of course... I posted UNTHINKINGLY... a very good point. You can force lower case with a rename of the extension.
But then it would not be a one-liner.

rename *.AVI *.avi
Quote from: Geek-9pm on May 26, 2013, 09:48:47 AM
You can force lower case with a rename of the extension.
But then it would not be a one-liner.

rename *.AVI *.avi

As the REN command ignores case, we can protect the avi files with a temporary dummy extension, and then rename back, forcing every .avi extension to a chosen case. In this example it is lower case. It will all go in one line.

in a batch...

ren *.avi *.avi$$$ & for /f "delims=" %%A in ('dir /b *.* ^| find /v "%~nx0"') do if not "%%~xA"==".avi$$$" del "%%A" & if exist *.avi$$$ ren *.avi$$$ *.avi

before


aaaaa.bat
test0.AVI
test1.Avi
test2.aVI
test3.aVI
test4.aVi
test5.aVI
test6.avi
test7.txt
test8.doc
test9.zip
testA.aaa
testB.bbb
testC.ccc

after


aaaaa.bat
test0.avi
test1.avi
test2.avi
test3.avi
test4.avi
test5.avi
test6.aviUpdate (ignores directories in the folder)
Note: batch does not delete itself

Code: [Select]ren *.avi *.avi$$$ & for /f "delims=" %%A in ('dir /b /a-d ^| find /v "%~nx0"') do if not "%%~xA"==".avi$$$" del "%%A" & if exist *.avi$$$ ren *.avi$$$ *.aviYour original suggestion with case insensitive switch will work fine, except it will delete the bat file. This should cater for that.

Code: [Select]for %%A in (*.*) do if /i not "%%~nxA"==%~nx0" if /i not "%%~xA"==".avi" del "%%A"


Discussion

No Comment Found