Script to delete unwanted files.

sparker3669

Junior Member
Feb 1, 2013
22
0
0
I have a folder on my computer that I use to store stuff. It has all sub folders in it for the different stuff I get. And those sub folders will also have sub folders.

I want to start at the top and work my way through all the folders in that main folder and delete files I don't want. This issue is I may not always know the extension of the files I want to delete. I do know the extensions of the files I want to keep.

What would be the best way to do this? Now bear with me I am not a programmer. I have done some a while ago but nothing recently.

I am willing to learn what needs to be done. This doesn't seem to be like a hard task except the traversing the directories.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,284
3,905
75
I don't suppose you're using Linux? in Linux the find command with exec would do it. Here's some Windows equivalents

http://superuser.com/questions/401495/equivalent-of-unix-find-command-on-windows
 

sparker3669

Junior Member
Feb 1, 2013
22
0
0
I run windows.

The files I want to keep are all video files so avi mp4 mpg wmv mkv mov and one other file nfo. How would I use find to exclude those and return the other files in the directory so I can delete them?
 

KB

Diamond Member
Nov 8, 1999
5,401
386
126
Heres a vbscript to do it. Paste the code into a .vbs file.
Point the strFolder to your folder.
I commented out the delete statement. I think you should first move all the files out of the folder structure into another folder where they can all be reviewed before they are deleted. If you disagree just uncomment the delete statement (remove the ') and comment out the MoveFile statement.

Use at your own risk.

Code:
Dim objFSO, objFolder, colFiles, objSubFolder, objSubFile
'change these to your paths
strFolder = "c:\test"
strMoveToFolder = "c:\test1\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)

RecurseFolder(objFolder)

MsgBox "Done"

Sub RecurseFolder(objFolder)

		Set colFiles = objFolder.Files
		For Each objFile in Colfiles
			Dim ext 
			ext = LCase(objFSO.GetExtensionName(objFile.Path))

			If ext <> "avi" AND ext <> "mp4" AND ext <> "mpg" AND ext <> "wmv" AND ext <> "mkv" AND ext <> "mov" AND ext <> "nfo" Then
				'objFSO.DeleteFile objFile, true
				objFSO.MoveFile objFile.Path, strMoveToFolder
				'MsgBox objFile.Path
			End If
		Next


		Set colFolders = objFolder.SubFolders
		For Each objSubFolder In colFolders
			RecurseFolder(objSubFolder)
		Next
End Sub
 

sparker3669

Junior Member
Feb 1, 2013
22
0
0
Heres a vbscript to do it. Paste the code into a .vbs file.
Point the strFolder to your folder.
I commented out the delete statement. I think you should first move all the files out of the folder structure into another folder where they can all be reviewed before they are deleted. If you disagree just uncomment the delete statement (remove the ') and comment out the MoveFile statement.

Use at your own risk.

Thanks very much for this. I do like the moving of the files and then manually deleting them. Does the move to folder have to exist or will it create it?

Now if I want to modify this on my own what is the best way to learn vbscript? I would probably like to add somethings to it on my own. Make it a bit more interactive.
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
I have a folder on my computer that I use to store stuff. It has all sub folders in it for the different stuff I get. And those sub folders will also have sub folders.

I want to start at the top and work my way through all the folders in that main folder and delete files I don't want. This issue is I may not always know the extension of the files I want to delete. I do know the extensions of the files I want to keep.

What would be the best way to do this? Now bear with me I am not a programmer. I have done some a while ago but nothing recently.

I am willing to learn what needs to be done. This doesn't seem to be like a hard task except the traversing the directories.

If you know programming it is pretty easy with C#.

/facepalm
 

Charles Kozierok

Elite Member
May 14, 2012
6,762
1
0
Go to the top level and type "dir /b /s >filelist.txt". Then edit it with a good text editor, removing all the lines of the files with extensions you want to keep. Then put quotes around them and "del " in front and save in a batch file. I can do it for you if you want to send me the list.
 

KB

Diamond Member
Nov 8, 1999
5,401
386
126
Thanks very much for this. I do like the moving of the files and then manually deleting them. Does the move to folder have to exist or will it create it?

Now if I want to modify this on my own what is the best way to learn vbscript? I would probably like to add somethings to it on my own. Make it a bit more interactive.

You need to create the folder. For bonus points figure out the script code to create it for you.

Learning vbscript is easy:

http://technet.microsoft.com/en-us/library/ee176792.aspx
 

TridenT

Lifer
Sep 4, 2006
16,810
45
91
You could also do a windows search for the file extensions you want to keep... Just use the search bar in the top right corner of the explorer window.

Something like "NOT folder or NOT *.avi or NOT *.pdf or NOT *.mp4 or NOT *.mp3" etc. Ctrl+A all the stuff and delete. Presto...

Delete whatever shows up. It does work and I usually do "NOT folder or NOT *.mp3" for my music folder and delete whatever shows up. (Usually a bunch of artwork or playlist files that I don't want)
 

alkemyst

No Lifer
Feb 13, 2001
83,967
19
81
You need to create the folder. For bonus points figure out the script code to create it for you.

Learning vbscript is easy:

http://technet.microsoft.com/en-us/library/ee176792.aspx

QFT.

I love vbscript. Back in 1999 when we had to write a CPU's workflow in C++, I knew there was a ton of cheating in the class. Many already had the solution.

I asked if I could write it in vbscript.

It ran slow, but got the job done.

I got the only A. Half the class was expelled. They were also in my DB class. Mass cheating.
 

sm625

Diamond Member
May 6, 2011
8,172
137
106
Just do a search for all files and organize the results by type.

With the search result window open, create a regular text file and name it mydelete.bat or (whateveryouwant.bat). Right click the file and click Edit.

Go back to the search results and scroll down. Note the type of each file you dont want. If the first file is a .txt file type, go to the batch file editor window and type in

del *.txt /s

Once you run this batch file it will delete all txt files in the current folder and in all subfolders. Just do that for every type you find that you dont want.

Do the same for other types. When you're done you may have a batch file that looks like this:

del *.txt /s
del *.js /s
del *.css /s
del *.[1] /s
del *.ico /s
del *.log /s
del *.dat /s
del *.ini /s


I recommend doing it this way because you may find that there may be types of media you didnt know about or havent listed (like .flv?) and this way you will not delete them unless you specifically choose to.
 
Last edited:
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |