How to use command prompt to delete file with special characters?

2

0

I found this answer on Server Fault, but it is nearly 10 years old and doesn't work anyway

I have files with names like:

AlchBagSquare_Black°.nif
League-of-Legends-фэндомы-1759411.jpeg

When I attempt to use del {filename} to delete them, it just says:

Could Not Find {filename}

I need a single, comprehensive solution that will work for all special characters via the Windows 7 command line.

Edit: I am generating a batch file to delete files, so it also cannot require user input.

Edit 2: Here's how I am using the delete command:

del /F "D:\backup\League-of-Legends-фэндомы-1759411.jpeg"

oscilatingcretin

Posted 2018-04-29T19:28:04.760

Reputation: 4 093

1Have you tried cding into the directory containing the file(s), type del (note the space) and then cycle through the files using TAB repeatedly? – sticky bit – 2018-04-29T19:49:48.273

@stickybit This will not work as I am generating a batch file to delete files, so it cannot require user input. – oscilatingcretin – 2018-04-29T19:51:05.503

Your question does not state that you are using a batch file, only command prompt. Please edit your question and provide every detail, or it will be hit-and-miss. I'm going to vote-close this as unclear what you're asking. – LPChip – 2018-04-29T19:54:01.710

Then maybe providing that batch within the question might help to help. – sticky bit – 2018-04-29T20:10:41.087

Both of you relax. I edited the question, and I wasn't downing you, @stickybit, for not being able to discern what I meant when there was a lack of information. – oscilatingcretin – 2018-04-29T20:12:59.040

1I am not able to recreate this on Windows 7 with both echo y | del *.* or for %A in (*.*) do del %~A for some reason on Windows 7 both ways deletes the files as per the example names you provided on my test Windows 7 system. Perhaps providing the exact logic of the batch script you are using will help others see if there is anything obvious at this level that may indicate what may be going on from your side? They were just frazzled by the ZZ Top beard.... lol – Pimp Juice IT – 2018-04-29T20:19:05.887

1I edited the question with the command I am using. I think the reason why your loop and wildcards are working is because the command is interpreted and executed internally where it doesn't have issues with the encoding. However, when provinding a filename with special characters at the command level, it borks because the command prompt can't handle the encoding. And, yes, my beard tends to do that to people :) – oscilatingcretin – 2018-04-29T20:27:04.593

An encoding issue indeed seems likely. I tried writing a batch like that, and copied the full path from the Explorer address field. Once I did it with SciTE, which by my settings, uses UTF-8. Result: "Could not find ...". Then I did it with plain old Notepad. That worked. – sticky bit – 2018-04-29T20:46:30.360

Maybe the file is corrupt and it's not a 'special character' issue. If for some reason del isn't deleting a file then I wouldn't use just the command prompt. If this is a one off problem then just use a linux bootable os off usb and delete it from the linux command line. Really all you have for deleting a file in the cmd prompt is DEL or RM..and you're hostage to windows using the file or having some issue. If you want to stick to cmd maybe you could experiment a bit like Move to rename it then trying to delete it. You could try the windows recovery console that has a command prompt. – barlop – 2018-04-29T21:41:16.963

Answers

1

None of your characters are invalid like the other question (which uses the forbidden : in the name)

So what you need to do is using Unicode by saving the batch file as UTF-8 without BOM and changing the codepage to UTF-8 (65001) if it's not the default

chcp 65001
del AlchBagSquare_Black°.nif
del League-of-Legends-фэндомы-1759411.jpeg

See more Deleting a file with UTF-8 Characters in the filename (ex. Japanese, Chinese characters)

phuclv

Posted 2018-04-29T19:28:04.760

Reputation: 14 930

Wow, it worked. I didn't even know about chcp. While I ended up going with another solution, this answer should be very helpful for other people encountering this issue. – oscilatingcretin – 2018-04-30T19:31:04.303

@oscilatingcretin You say you "ended up going with another solution". So hang on, did you test this answer(on a file like the one you had trouble deleting)? If so then fine, if not then please don't accept an answer if you haven't tested it. – barlop – 2018-05-01T02:58:07.510

2@barlop What part of "Wow, it worked" suggests that I didn't test it? Of course I tested it with a batch file the same as before. – oscilatingcretin – 2018-05-01T03:15:44.767

@oscilatingcretin ok that's good – barlop – 2018-05-01T03:19:06.333