Solved

FSO Delete filename.*

Posted on 2004-08-27
11
1,481 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

20 Experts available now in Live!

Get 1:1 Help Now