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,928 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
The password reset disk is often mentioned as the best solution to deal with the lost Windows password problem. In Windows 2008, 7, Vista and XP, a password reset disk can be easily created. But besides Windows 7/Vista/XP, Windows Server 2008 and ot…
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…
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). …

809 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