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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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…

759 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

18 Experts available now in Live!

Get 1:1 Help Now