Solved

FSO Delete filename.*

Posted on 2004-08-27
11
1,486 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Executing a script on a timer online 5 32
MS SQL 2008 and stored prodcures and dates 5 27
Session on Html 8 42
Validating number not work with decimal 4 27
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…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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