Can anyone tell me what is wrong with this code?

Dim current_date As String
Dim filename As String

Private Sub Form_Load()
Dim path As String

current_date = DateValue(Now)
current_date = Format(DateValue(Now), "YYYYMMDD")
Debug.Print current_date
filename = "Daily SMS Backup " & current_date & " 200.sql"
Debug.Print filename
path = "P:\"
Shell ("cmd.exe /c del " & path & "filename")
End Sub

P:\ is a mapped network drive on the server.

What this code should do is

get the current date of the form YYYYMMDD. Ok
create a strng of the file I want to delete. Ok
Execute a shell to browse to P:\
and delete the file filename. This is where it goes wrong for me.

Can anyone help me please

Recommended Answers

All 6 Replies

If Dir(PathFileName) <> "" then Kill PathFileName

Good Luck

Thanks for your help.
I tried the following and it still didn't work

Dim current_date As String
Dim filename As String

Private Sub Form_Load()
Dim path As String
Dim pathfilename As String

current_date = DateValue(Now)
current_date = Format(DateValue(Now), "YYYYMMDD")
Debug.Print current_date
filename = "Daily SMS Backup " & current_date & " 0200.sql"
Debug.Print filename
path = "P:\"
pathfilename = path & filename
If Dir(pathfilename) <> "" Then Kill pathfilename
'Shell ("cmd.exe /c del " & path & "filename")
End Sub

Any ideas?

Okay, through windows explorer, on the machine and under the account that the program runs, can you delete the file from there?

I'm wondering if it may be an accesibility rights issue.

Just for a quick test...

If Dir(PathFileName) <> "" then
  MsgBox "Found"
Else
  MsgBox PathFileName
End If

Good Luck

Thanks for all your help.

The following code worked perfectly for me.

Dim current_date As String
Dim filename, filename1 As String

Private Sub Form_Load()
Dim path As String
Dim pathfilename, pathfilename1 As String

current_date = DateValue(Now)
current_date = Format(DateValue(Now - 2), "YYYYMMDD")
Debug.Print current_date
filename = "DailySMSBackup " & current_date & " 0200.sql"
filename1 = "Daily SMS Backup " & current_date & " 0200.sql"
Debug.Print filename
path = "P:\"
pathfilename = path & filename
pathfilename1 = path & filename1
If Dir(pathfilename) <> "" Then
    Kill pathfilename
ElseIf Dir(pathfilename1) <> "" Then
    Kill pathfilename1
Else
End If
End
End Sub

Ahhh... I see, you have two possible filenames...Glad you got it solved.

Shell ("cmd.exe /c del " & path & "filename") ' this is remove p:\filename

Shell ("cmd.exe /c del """ & path & filename & """") ' this is work to u

don't forgot if you want use long file name in windows you must set the path in "
means test that command in run don't work but this work
cmd.exe /c del "p:\sql 123123 12312.txt"

Can anyone tell me what is wrong with this code?

Dim current_date As String
Dim filename As String

Private Sub Form_Load()
Dim path As String

current_date = DateValue(Now)
current_date = Format(DateValue(Now), "YYYYMMDD")
Debug.Print current_date
filename = "Daily SMS Backup " & current_date & " 200.sql"
Debug.Print filename
path = "P:\"
Shell ("cmd.exe /c del " & path & "filename")
End Sub

P:\ is a mapped network drive on the server.

What this code should do is

get the current date of the form YYYYMMDD. Ok
create a strng of the file I want to delete. Ok
Execute a shell to browse to P:\
and delete the file filename. This is where it goes wrong for me.

Can anyone help me please

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.