0

I use the following batch file to delete files with full path. The first line of the output file is an error message "The filename, directory name, or volume label syntax is incorrect." All files in C:\DELETE-ALL-FULLPATH.txt are deleted except the first one. I wonder why the first line causes the problem.

@REM This script deletes files which include full path.

@if exist C:\DELETE-all-fullpath-output.txt DEL /Q c:\DELETE-all-fullpath-output.txt
@for /f "tokens=* eol=@ delims=" %%A in ( C:\DELETE-ALL-FULLPATH.txt) do (
@DEL /Q /A "%%A"
)   >> C:\DELETE-all-fullpath-output.txt 2>&1

CD /D C:\

These are the first two lines of C:\DELETE-ALL-FULLPATH.txt. All lines have the same format except the numbers are different. All the listed files are deleted except the first one.

c:\Users\administrator\documents\IMG_20190229_134600.jpg
c:\Users\administrator\documents\IMG_20191229_134202.jpg
......

enter image description here

joehua
  • 725
  • 3
  • 10
  • 25
  • 2
    Perhaps file is unicode and has a byte order mark at the start of the first line, that isn't handled when it is being read? – Lasse V. Karlsen Jul 13 '20 at 13:36
  • @Gerhard I got `C:\>(echo /Q /A "c:\Users\administrator\documents\IMG_20190229_134600.jpg" ) 1>>C:\DELETE-all-fullpath-output.txt 2>&1` Doesn't seem to be a problem there. – joehua Jul 13 '20 at 13:59
  • @Lasse very likely. I added a blank line as the first line, ran the batch file again, and the first file was deleted although the error message was still there. – joehua Jul 13 '20 at 14:03
  • @Lasse Actually, the error message is `Could Not Find C:\`. – joehua Jul 13 '20 at 14:38

1 Answers1

0

Give this a try please.

@echo off
REM This script deletes files which include full path.
del /Q c:\DELETE-all-fullpath-output.txt>nul 2>&1
for /f "usebackq delims=" %%A in ("C:\DELETE-ALL-FULLPATH.txt") do (
    if exist "%%~A" del /Q /A "%%~A"
)>>c:\DELETE-all-fullpath-output.txt 2>&1

EDIT

After reviewing your file, it indeed is UTF-8 with BOM enabled.

enter image description here

Open the file in notepad, then select save as. in the encoding tab, select UTF-8 only, without BOM.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Sure. But first try if this link would work. I have uploaded the file to Dropbox. https://www.dropbox.com/s/oh6ojtyu2aht6w2/delete-all-FULLPATH.txt?dl=0 – joehua Jul 14 '20 at 06:42
  • Now, the file in the first line is deleted. – joehua Jul 14 '20 at 08:01
  • ok, so to best explain it, just read [here](https://stackoverflow.com/questions/2223882/whats-the-difference-between-utf-8-and-utf-8-without-bom#:~:text=The%20UTF%2D8%20BOM%20is,8%2C%20the%20BOM%20is%20unnecessary.) – Gerhard Jul 14 '20 at 08:04