cmd /c del to delete file

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,621
Office Version
  1. 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

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

tyija1995

Well-known Member
Joined
Feb 26, 2019
Messages
781
Office Version
  1. 365
Platform
  1. Windows
I think you have to put the filename in double quotes too for it to work with spaces.
 
Last edited:

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
38,820
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
As in:

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

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,621
Office Version
  1. 2019
Platform
  1. Windows
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:

tyija1995

Well-known Member
Joined
Feb 26, 2019
Messages
781
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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:

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,621
Office Version
  1. 2019
Platform
  1. Windows
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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
38,820
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS

ADVERTISEMENT

Afraid it didn't work.

Works fine for me - what happened when you tried it?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
38,820
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Any error would appear briefly in the command prompt window before it disappeared, so you probably wouldn't see it.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,650
Messages
5,838,566
Members
430,556
Latest member
Peachforyou

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
Top