Solved

FSO Delete filename.*

Posted on 2004-08-27
11
1,487 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
[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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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 informatio…
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/…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

717 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