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
1,888 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now