Delete Files Based On a Text Field (1 Viewer)

Learn2010

Registered User.
Local time
Today, 18:09
Joined
Sep 15, 2010
Messages
415
I have a database and use part of it for billing. I create a file and upload it to a site. The structure of the file is M123456.1. The next file generated would be M123456.2, and so on. In order to keep the folder cleaned out and make it easy to identify the file to upload, I get rid of similar files in the folder with the following line of code:
If Dir("C:\Folder1\Folder2\M*.*") <> "" Then Kill "C:\Folder1\Folder2\M*.*"
I would like to use this for something similar. Instead of using the M123456 as the file name to delete, I would like to use a field name on a subform from where this code is activated. This field is used to identify customers. For example, if the Customer ID is 123456, I would like to delete all files that start with that number. I can’t get it to work.
If Dir("C:\Folder1\Folder2\CustomerID") <> "" Then Kill "C:\Folder1\Folder2\CustomerID"
Any ideas?
Thank you.
 

RuralGuy

AWF VIP
Local time
Today, 16:09
Joined
Jul 2, 2005
Messages
13,826
Try something like:

Dim YourStringVar As String

YourStringVar = C:\Folder1\Folder2" & CustomerID

If Dir( YourStringVar ) <> "" Then Kill YourStringVar

Warning: Untested
 

isladogs

MVP / VIP
Local time
Today, 23:09
Joined
Jan 14, 2017
Messages
18,209
Create a string variable called strCustID
It doesn't matter if your customer id field is a number as its going to be used as art of a string.

When the button is clicked add code similar to this

Code:
strCustID = Me.CustomerID

Then replace your Kill statement with

Code:
 Kill "C:\Folder1\Folder2\" & '" & strCustID & "'"

EDIT RuralGuy got there first. Hi Allan
 

Learn2010

Registered User.
Local time
Today, 18:09
Joined
Sep 15, 2010
Messages
415
Tried several variations and could not get it to work.
 

Learn2010

Registered User.
Local time
Today, 18:09
Joined
Sep 15, 2010
Messages
415
Dim MyVar As String
MyVar = "C:\Folder1\Folder2" & CustomerID
If Dir(MyVar) <> "" Then Kill MyVar
 

isladogs

MVP / VIP
Local time
Today, 23:09
Joined
Jan 14, 2017
Messages
18,209
You didn't say what happened but its easy to fix.
Add a backslash after Folder2
 

Learn2010

Registered User.
Local time
Today, 18:09
Joined
Sep 15, 2010
Messages
415
Nothing happened. But, the Customer ID will be a number with six digits.
 

RuralGuy

AWF VIP
Local time
Today, 16:09
Joined
Jul 2, 2005
Messages
13,826
I ttried editing my post right away and it would not stick. change this line:
MyVar = "C:\Folder1\Folder2" & CustomerID
...to...
MyVar = "C:\Folder1\Folder2" & CustomerID
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 23:09
Joined
Jan 14, 2017
Messages
18,209
Try my similar code instead
If it still doesn't work, add a Debug.Print line to view the output in the immediate window
 

isladogs

MVP / VIP
Local time
Today, 23:09
Joined
Jan 14, 2017
Messages
18,209
Hi Allan. Have another look at your post #9.
 

RuralGuy

AWF VIP
Local time
Today, 16:09
Joined
Jul 2, 2005
Messages
13,826
The site will not let me post that trailing "".
???? I reported it to the Mods.
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:09
Joined
Sep 21, 2011
Messages
14,238
C:\Folder1\Folder2\

"C:\Folder1\Folder2"

Doesn't appear to work when you use quotes and outside of a code window?
 

RuralGuy

AWF VIP
Local time
Today, 16:09
Joined
Jul 2, 2005
Messages
13,826
So adding the code tags allowed the trailing ""?
Code:
"\"
 

Users who are viewing this thread

Top Bottom