?
Solved

file/folder delete question, with a space in the folder name

Posted on 2009-05-05
2
Medium Priority
?
453 Views
Last Modified: 2012-05-06
Hi I run this code to delete files and folders, it works ok with I type in cmd:
cscript DeleteFiles.vbs d:\logfiles 31 /R,

but when i type a folder with space in the name such as 'log files' or 'tom new house'
cscript DeleteFiles.vbs d:\log files 31 /R

it gave me 'Age parameter must be numeric' warning.

i think the space in the folder name caused the problem, but I can not change the folder name.
does anyone know how to modify the code?
thanks a lot!!

Robin
'************************************************************************
'*
'*	This script will loop over all files in the specified folder 
'*	and will delete any old than the specified age
'*	Care must be taken with this script as it will delete files 
'*	without prompts. 
'*
'*	Usage: 
'*	DeleteFiles.vbs [path] [age] [/R] 
'*	[Path] Specifies the path to process
'*	[Age] Specified the age of files in day which will be deleted
'*
'*	/R	Deletes files recursively. All Files under the specified folder older than specified age will be deleted. 
'*
'* 	WARNING: THIS WILL DELETE FILES WITHOUT ANY PROMPTS. DO NOT RUN THIS ON CRITICAL SYSTEM FOLDERS
'*	Example: cscript DeleteFiles.vbs d:\logfiles 31 /R
'*
'************************************************************************
 
 
 
 
dim filecnt 
dim age
dim recursive
dim path 
filecnt = 0 
if WScript.Arguments.Count <2 then 
	WScript.Echo "Usage: "
	WScript.Echo "DeleteFiles.vbs [Path] [Age] [/R]" & vbcrlf 
	Wscript.Echo "[Path] Specifies the path to process"
	Wscript.Echo "[Age] Specified the age of files in day which will be deleted" & vbcrlf 
	wScript.Echo "/R	Deletes files recursively. All Files under the specified folder older than specified age will be deleted. " & vbcrlf 
	Wscript.Echo "Example: cscript DeleteFiles.vbs d:\logfiles 31 /R" & vbcrlf 
	wscript.Echo "WARNING: THIS WILL DELETE FILES WITHOUT ANY PROMPTS. DO NOT RUN THIS ON CRITICAL SYSTEM FOLDERS"
	wscript.Echo ""
	wscript.quit(0)
else 
 
	if NOT isNumeric(wscript.arguments(1)) then 
		wScript.Echo "Age parameter must be numeric"
		wscript.quit(0)
	else 
		path = wscript.arguments(0)
		'validate path parameter ? 
		age = cint(wscript.arguments(1))
		if wscript.arguments.count = 3 then 
			if lcase(wscript.arguments(2)) = "/r" then 
				recursive = "/r"
				wscript.echo "Recursive Mode"
			end if 
		end if 
		recursefiles path
	end if 
end if 
 
 
wscript.echo filecnt  & " files were processed."
 
 
Sub RecurseFiles(Path)
	Const FOR_READING = 1
	strFolder = path
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFolder = objFSO.GetFolder(strFolder)
	wscript.echo objFolder.Path
	Set colFiles = objFolder.Files
	For Each objFile In colFiles
		ProcessFile (objFile)
	Next
	if recursive= "/r" then 
		ShowSubFolders (objFolder)
	end if 
End Sub
 
Sub ProcessFile(file)
FileAge1 = DateDiff("d",file.DateLastModified, now )
FileAge2 = DateDiff("d",file.DateCreated, now )
FileAge3 = DateDiff("d",file.DateLastAccessed, now )
if FileAge1  > Age and FileAge2 > age and FileAge3 > age then 
      on error resume next 
      filecnt = filecnt + 1
      wscript.echo "deleting " & file.Path & " " & file.datelastmodified 
      file.delete true 'true will force the deletion 
      on error goto 0 
end if 
End Sub
 
 
Sub ShowSubFolders(objFolder)
  Set colFolders = objFolder.SubFolders
  For Each objSubFolder In colFolders
    wscript.echo objSubFolder.Path
    Set colFiles = objSubFolder.Files
    For Each objFile In colFiles
      ProcessFile (objFile)
    Next
    ShowSubFolders (objSubFolder)
  If objSubFolder.Files.Count = 0 And objSubFolder.SubFolders.Count = 0 Then
    'the level above this is empty
      on error resume next 
      wscript.echo "deleting " & objSubFolder
        objSubFolder.Delete true 
      on error goto 0 
    End If
  Next
End Sub

Open in new window

0
Comment
Question by:robinyanwang
[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 Comments
 
LVL 10

Accepted Solution

by:
TakedaT earned 1000 total points
ID: 24309002
Try to enclose the path in " " like so.

cscript DeleteFiles.vbs "d:\log files" 31 /R
0
 

Author Closing Comment

by:robinyanwang
ID: 31578216
thanks.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

771 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