0

I'm trying to create a batch file that deletes all files from the desktop that have the same name as those in a specific folder (C:\Users\micheled\Desktop\TestDelete)

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

FOR %%x IN (*) DO (
  del C:\Users\micheled\Desktop\%%x    
)

but I have a problem with files that have a space in the name (example: "w 2.txt), the batch instead of performing delete w 2.txt executes: delete w delete 2.txt so how do I make sure that the files with the sweep are considered a single element?

Thanks in advance!

michele D
  • 1
  • 1
  • 5
    [For each file in given directory](https://stackoverflow.com/questions/180741/how-to-do-something-to-each-file-in-a-directory-with-a-batch-script), [check if same file exists on desktop](https://stackoverflow.com/questions/4340350/how-to-check-if-a-file-exists-from-inside-a-batch-file) and if it does, [remove it](https://stackoverflow.com/questions/13764103/batch-script-to-delete-files). Literally couple of lines of code, don't be lazy. – emix Jul 25 '18 at 12:32

1 Answers1

0

Its good to get into the habit of doublequoting strings, especially if they contain spaces.

Here's a single line example, according to the advice of Mofi in the comments:

@For %%A In ("C:\Users\micheled\Desktop\TestDelete\*") Do @If Exist "C:\Users\micheled\Desktop\%%~nxA" Del "C:\Users\micheled\Desktop\%%~nxA"
Compo
  • 30,301
  • 4
  • 20
  • 32