Solved

FSO Delete filename.*

Posted on 2004-08-27
11
1,485 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Autosum input type=text when checkbox is true 28 102
Send to Print Preview database record with one click 3 61
Multiflying 2 Input Text On a Table 7 31
Html Table looping 4 25
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
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/…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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