Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
2,155 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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…

715 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