cmd /c del to delete file

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I want to use the cmd /c del command to delete a file, instead of Kill.

This works:

Code:
  Dim Loc As String
    
    Loc = "C:\MyFolder\abcdef.xlsm"
    
    Dim cmd As String


    cmd = "cmd /c del " & Loc
    
    Shell cmd

but if the filename contains a space like this, it fails


Code:
    Dim Loc As String
    
    Loc = "C:\MyFolder\abcde f.xlsm"
    
    Dim cmd As String

    
    cmd = "cmd /c del " & Loc
    
    Shell cmd

How can I make it work when the filename contains spaces?


Thanks
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I think you have to put the filename in double quotes too for it to work with spaces.
 
Last edited:
Upvote 0
As in:

Code:
cmd = "cmd /c del """ & Loc & """"
 
Upvote 0
I think you have to put the filename in double quotes too for it to work with spaces.



As in:
Code:
cmd = "cmd /c del """ & Loc & """"





Afraid it didn't work.

If I wasn't using variables, then doubling up the quotes does work, ie


Code:
strcmd = "cmd /c del C:\MyFolder\" & """abcde f.xlsm"""
 
Last edited:
Upvote 0
What about this code: (Altered your first post code slightly)

Code:
  Dim Loc As String
  Dim fileName As String
  
  fileName = "abcde f.xlsm"
    
    Loc = "C:\MyFolder\" & Chr(34) & fileName & Chr(34)
    
    Dim cmd As String


    cmd = "cmd /c del " & Loc
    
    Shell cmd
 
Last edited:
Upvote 0
What about this code: (Altered your first post code slightly)

Code:
  Dim Loc As String
  Dim fileName As String
  
  fileName = "abcde f.xlsm"
    
    Loc = "C:\MyFolder\" & Chr(34) & fileName & Chr(34)
    
    Dim cmd As String


    cmd = "cmd /c del " & Loc
    
    Shell cmd

Thanks, yes this version worked.
 
Upvote 0
Any error would appear briefly in the command prompt window before it disappeared, so you probably wouldn't see it.
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top