Solved

Vbscript to delete all folders and files in a directory except to 2 files that are always the same

Posted on 2010-11-18
4
2,006 Views
Last Modified: 2012-05-10
looking for a script that will clean out all the folders and files in a specified folder with the exception of 2 files that I need to have remain in the folder
any scripts out there?

Thanks
0
Comment
Question by:chasmant
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Expert Comment

by:torque_200bc
ID: 34166164
Set fso=CreateObject("Scripting.FileSystemObject")
CleanPath="path_to_file"
'something like "c:\users\delete"

For Each file In fso.GetFolder(CleanPath).Files
if (file <> "path_to_file_no_to_delete") then file.delete
'something like "c:\users\delete\nodelete.exe". Be careful with the path as it is case sensitive.
Next
0
 
LVL 4

Expert Comment

by:torque_200bc
ID: 34166231
Forgot the folder part srry.

Add theese lines at the end of the script

For each folder in fso.getfolder(cleanpath).subfolders
folder.delete
next

----

The complete script would be:

Set fso=CreateObject("Scripting.FileSystemObject")
CleanPath="path_to_file"
'something like "c:\users\delete"

For Each file In fso.GetFolder(CleanPath).Files
if (file <> "path_to_file_no_to_delete") then file.delete
'something like "c:\users\delete\nodelete.exe". Be careful with the path as it is case sensitive.
Next

For each folder in fso.getfolder(cleanpath).subfolders
folder.delete
next

0
 
LVL 1

Accepted Solution

by:
wlramsey earned 500 total points
ID: 34166235
Try something like this.  You will need to modify the first three lines to fit your needs.  It isn't pretty, but it is something quick.

strfile1 = "test2.csv"
strfile2 = "test1.csv"
strPath = "C:\\Scripts"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objfso=CreateObject("Scripting.FileSystemObject")
Set objFolders = objFSO.GetFolder(strPath)
Set objSubFolders = objFolders.SubFolders
For Each Folder in objSubFolders
    objFSO.DeleteFolder Folder.Path, True  
Next
For Each file In objfso.GetFolder(strPath).Files
	If (objfso.GetFileName(file.Name)=strfile1) or (objfso.GetFileName(file.name)=strfile2) Then
		'skip file
	Else
		file.delete
	End If
Next

Open in new window

0
 
LVL 1

Author Closing Comment

by:chasmant
ID: 34166932
great thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had a hard drive that you can't boot into, but need to change the registry? Here is the solution! This article guides you through accessing and editing a registry of a non-primary drive. To read registry information on a non-prim…
This is a little timesaver I have been using for setting up Microsoft Small Business Server (SBS) in the simplest possible way. It may not be appropriate for every customer. However, when you get a situation where the person who owns the server is i…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question