Solved

FSO Delete filename.*

Posted on 2004-08-27
11
1,478 Views
Last Modified: 2010-08-05
In a directory I have files like:

154.Xls
232.log
872.doc

The first part of the file name is the user ID plus the extension.
Everytime the user uploads a file I want to delete the old file.
The new file could have different extension than the previous one.

So lets say user 872 wants to upload a new file  872.txt, I want to check the file
and see if a file with userID 872 exists or not. If it exists then delete.
I cant get the if exists part to work.


This is the code I have:


  pathToFile2 = Server.mapPath("../../Temp/") & "\" & ID & ".*"
  'It looks something like this C:/Temp/872.*
     
           
    'Delete the old file for this User befor adding the new one
      Dim fso
    Set fso = Server.CreateObject("Scripting.FileSystemObject")
    If fso.FileExists(pathToFile2) Then
          Call fso.DeleteFile(pathToFile2)
     End If
     Set fso = Nothing

*********
So above when I take the If Exist part out it works. But when the user has no files it fails

0
Comment
Question by:AutomaticSlim
11 Comments
 
LVL 14

Accepted Solution

by:
huji earned 50 total points
ID: 11917012
I would do it like this (supposing that all file extensions are three letters, not like HTML):

pathToFolder = Server.mapPath("../../Temp/") & "\"                       ' & ID & "."

..

Set fso = Server.CreateObject("Scripting.FileSystemObject")
userFolder = fso.getFolder(pathToFolder)
FOR EACH aFile in userFolder
    IF left(aFile.name,len(aFile.name)-4)=ID THEN
        fso.deleteFile(pathToFolder & ID & aFile.name)
    END IF
NEXT


Wish I can help
Huji
0
 
LVL 14

Expert Comment

by:huji
ID: 11917067
Typo:
This line:
        fso.deleteFile(pathToFolder & ID & aFile.name)
must be changed to such:
        fso.deleteFile(pathToFolder & aFile.name)

Huji
0
 
LVL 14

Expert Comment

by:huji
ID: 11917076
And this line:

FOR EACH aFile in userFolder

must be changed to this:

FOR EACH aFile in userFolder.Files

huji
PS:I'm toooooo sleepy! Excuse me...
0
 

Author Comment

by:AutomaticSlim
ID: 11917731
I am getting the following error:

Object required: 'userFolder'
0
 

Author Comment

by:AutomaticSlim
ID: 11917735
This is what I have:

Dim fso    
Set fso = Server.CreateObject("Scripting.FileSystemObject")
userFolder = fso.getFolder(pathToFolder)
FOR EACH aFile in userFolder.Files
   IF left(aFile.name,len(aFile.name)-4)= PCID THEN
        fso.deleteFile(pathToFolder & aFile.name)
   END IF
NEXT
     
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Expert Comment

by:apirnia
ID: 11918116
Very few changes....
Try the following, it's a combination of your solution and my fellow Persian huji.
I left  "Call fso.DeleteFile(pathToFile2)" like you had it originally, it should work fine


The object Required error is because of a missing 'set' at this line:
userFolder = fso.getFolder(pathToFolder)
It should be
Set userFolder = fso.getFolder(pathToFolder)


'*****************************
'Here is my complete soloution

pathToFile2 = Server.mapPath("../../Temp/") & "\" & ID & ".*"
pathToFile3 = Server.mapPath("../../Temp/") & "\"    (This is the path without the filename)

Dim fso    
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set userFolder = fso.getFolder(pathToFile3)
FOR EACH aFile in userFolder.Files
  IF left(aFile.name,len(aFile.name)-4)= ID THEN
         Call fso.DeleteFile(pathToFile2)
  END IF
NEXT
Set fso = Nothing  
0
 
LVL 14

Expert Comment

by:huji
ID: 11922284
Yes I missed a Set.. damn that sleepiness! ;o)
Huji
PS to apirnia: How come you know I'm persian? Where are you from? Can you contact me via my email address in my profile to let me know you more?
0
 

Expert Comment

by:wonderdementic
ID: 11924911
' **********************************
' Function to check file Existance
' **********************************
Dim FileName,objFSO,SiteStatus
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

Function IsFileExists(byVal FileName)
 
 If FileName = ""  Then
  IsFileExists = False
  Exit Function
 End If
 
 If objFSO.FileExists(FileName)  Then
  IsFileExists = True
 Else
  IsFileExists = False
 End If
 
 Set objFSO = Nothing  
End Function

FileName = "filename.ext"
FileName = Server.MapPath("/" & FileName)

If IsFileExists(FileName) Then
Dim objFSO
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
      objFSO.DeleteFile FileName
      Set objFSO = Nothing  
End If

for better coding ;)
add huji's script and your done.
0
 
LVL 14

Expert Comment

by:huji
ID: 11926724
Come on AutomaticSlim.. any progress?
Huji
0
 

Author Comment

by:AutomaticSlim
ID: 11932671
sorry guys, I was out over the weekend.
0
 
LVL 14

Expert Comment

by:huji
ID: 11941151
No problem.. Happy to be helpful..
Huji
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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