cmd /c del to delete file

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,257
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
 

Some videos you may like

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

tyija1995

Well-known Member
Joined
Feb 26, 2019
Messages
766
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
35,520
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,257
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
766
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,257
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
35,520
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
35,520
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,109,433
Messages
5,528,738
Members
409,831
Latest member
KT50

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top