Finding cause of "The filename, directory name, or volume label syntax is incorrect" error when deleting files [SOLVED]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Finding cause of "The filename, directory name, or volume label syntax is incorrect" error when deleting files [SOLVED]

#1 Post by DOSadnie » 13 Jul 2023 05:08

This script

Code: Select all

@echo off
for /F "skip=1" %%D in ('wmic logicaldisk get caption') do (
    cd /D %%D
    del /AH /S /Q ~$*.xlsm
)
pause
goes through every drive [except the system one] and deletes ~$WHATEVER THE REST OF NAME.XLSM files. However it can produce an outcome like this
Could Not Find X:\~$*.xlsm
Deleted file - Y:\TEST1\~$some test file.xlsm
Deleted file - Z:\TEST2\~$another test file.xlsm
The filename, directory name, or volume label syntax is incorrect.
Could Not Find Z:\~$*.xlsm
which shows both that it does work but also is unable to access some folders [or maybe just files?] on volume Z

Is there a way to pn point this erroneous path? How to make the CMD window write where exactly it failed at accessing?
Last edited by DOSadnie on 21 Jul 2023 12:40, edited 2 times in total.

OJBakker
Expert
Posts: 90
Joined: 12 Aug 2011 13:57

Re: Finding cause of "The filename, directory name, or volume label syntax is incorrect" error when deleting files blunt

#2 Post by OJBakker » 13 Jul 2023 09:11

add

Code: Select all

dir /AH /S "~$*.xlsm"
before the

Code: Select all

del /AH /S /Q ~$*.xlsm
line.
This will show which files are selected to be deleted.
It is probably a file with a space or % or poison character like & or | in the filename.

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Finding cause of "The filename, directory name, or volume label syntax is incorrect" error when deleting files blunt

#3 Post by DOSadnie » 13 Jul 2023 09:57

As I had feared, the culprit is not what I am looking for but some other item

Because now this

Code: Select all

@echo off
for /F "skip=1" %%D in ('wmic logicaldisk get caption') do (
    cd /D %%D
    dir /AH /S "~$*.xlsm"
    del /AH /S /Q ~$*.xlsm
)
pause
[with the exact same test locations and files as before] produces this
Volume in drive X is Archive#1
Volume Serial Number is F2EA-3C73
File Not Found
Could Not Find X:\~$*.xlsm



Volume in drive Y is Archive#2
Volume Serial Number is 5CCA-44F3

Directory of Y:\TEST1

2023-07-13 12:44 0 ~$some test file.xlsm
1 File(s) 0 bytes

Total Files Listed:
1 File(s) 0 bytes
0 Dir(s) 1,029,689,573,376 bytes free
Deleted file - Y:\TEST1\~$some test file.xlsm



Volume in drive Z is Archive#3
Volume Serial Number is 1693-51F1

Directory of Z:\TEST2

2023-07-13 12:44 0 ~$some test file.xlsm
1 File(s) 0 bytes

Total Files Listed:
1 File(s) 0 bytes
0 Dir(s) 358,623,019,008 bytes free
Deleted file - Z:\TEST2\~$another test file.xlsm
The filename, directory name, or volume label syntax is incorrect.
Volume in drive Z is Archive#3
Volume Serial Number is 1693-51F1
File Not Found
Could Not Find Z:\~$*.xlsm
Last edited by DOSadnie on 16 Jul 2023 12:54, edited 1 time in total.

OJBakker
Expert
Posts: 90
Joined: 12 Aug 2011 13:57

Re: Finding cause of "The filename, directory name, or volume label syntax is incorrect" error when deleting files blunt

#4 Post by OJBakker » 13 Jul 2023 13:22

Try again with echo ON.
This will show you were your code goes wrong!
Hint, there is nothing wrong with the filename or location.

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Finding cause of "The filename, directory name, or volume label syntax is incorrect" error when deleting files blunt

#5 Post by DOSadnie » 15 Jul 2023 04:08

OJBakker wrote:
13 Jul 2023 13:22
[...]
Hint, there is nothing wrong with the filename or location.
And so it seems

Because now what I got was this

Code: Select all

X:\>(
cd /D P:
 del /AH /S /Q ~$*.xlsm
)
Could Not Find P:\~$*.xlsm

Y:\>(
cd /D W:
 del /AH /S /Q ~$*.xlsm
)
Deleted file - Y:\TEST2\~$some test file.xlsm

Z:\>(
   /D
 del /AH /S /Q ~$*.xlsm
)
The filename, directory name, or volume label syntax is incorrect.
Could Not Find Z:\~$*.xlsm

Z:\>cd /D C:\
So I went to the Disk Management of Windows changed letters assignment and even reset the system- but the outcome is always the same: the very last volume spits this apparently untrue information

Hence the question: why?

And more importantly: how do I get rid of it?
Last edited by DOSadnie on 16 Jul 2023 12:55, edited 2 times in total.

OJBakker
Expert
Posts: 90
Joined: 12 Aug 2011 13:57

Re: Finding cause of "The filename, directory name, or volume label syntax is incorrect" error when deleting files blunt

#6 Post by OJBakker » 15 Jul 2023 08:12

Ok, next hint, try the following script, that should make it clear where your problems originate!

Code: Select all

@echo off
for /F "skip=1" %%D in ('wmic logicaldisk get caption') do (
    echo Processing drive: [%%D]
    cd /D %%D
    dir /AH /S "~$*.xlsm"
    del /AH /S /Q ~$*.xlsm
)
pause

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Finding cause of "The filename, directory name, or volume label syntax is incorrect" error when deleting files blunt

#7 Post by DOSadnie » 16 Jul 2023 13:04

Now the CMD window says
Processing drive: [Z:]
Volume in drive Z is Archive#3
Volume Serial Number is 1693-51F1

Directory of Z:\TEST2

2023-07-13 12:44 0 ~$another test file.xlsm
1 File(s) 0 bytes

Total Files Listed:
1 File(s) 0 bytes
0 Dir(s) 306,194,939,904 bytes free
Deleted file - Z:\TEST2\~$another test file.xlsm
]rocessing drive: [
The filename, directory name, or volume label syntax is incorrect.
Volume in drive Z is Archive#3
Volume Serial Number is 1693-51F1
File Not Found
Could Not Find Z:\~$*.xlsm
So for reasons for me unknown there is line
]rocessing drive: [
instead of
Processing drive: [Z]
which no wonder produces such error on the account of name

OJBakker
Expert
Posts: 90
Joined: 12 Aug 2011 13:57

Re: Finding cause of "The filename, directory name, or volume label syntax is incorrect" error when deleting files blunt

#8 Post by OJBakker » 16 Jul 2023 14:00

You still do not see what happens?
Drive Z: is completely processed. The error happens after that!
So your script tries to process 'something' after all the drives are processed and that is causing the error you are getting.

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Finding cause of "The filename, directory name, or volume label syntax is incorrect" error when deleting files blunt

#9 Post by DOSadnie » 19 Jul 2023 09:53

No- apparently I just do not understand how CMD can insert such error in between two OK messages, all of them concerning the same last volume

And how is a user suppose to know that such error is a fake one and when it is a real one without having that altered script and being forced to carefully read thorough it

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Finding cause of "The filename, directory name, or volume label syntax is incorrect" error when deleting files blunt

#10 Post by ShadowThief » 19 Jul 2023 12:56

wmic adds an extra carriage return (\r) at the end of its output (see viewtopic.php?f=3&t=4266 for more details). A carriage return moves the cursor to the beginning of the line, so Processing Drive:[ is displayed, then the cursor is moved to the start of the line, and then ] is displayed, overwriting the first character.
Last edited by ShadowThief on 19 Jul 2023 16:32, edited 1 time in total.

OJBakker
Expert
Posts: 90
Joined: 12 Aug 2011 13:57

Re: Finding cause of "The filename, directory name, or volume label syntax is incorrect" error when deleting files blunt

#11 Post by OJBakker » 19 Jul 2023 13:23

Such a small script and you seem to fundamentally unable to interpreted what that script is doing.
There is nothing fake about the error message you get and your script is doing exactly what you programmed, although not what you intended it to do.

You intend to process a list of valid drive-identifiers.
What you are processing is wmic output.
What you intend to process and what you are processing is not the same thing!

The processing consists of 3 lines, ok 4 lines if you include the echo %%D line.
The echo line is not necessary but helps in understanding what is being processed. no problem. (See post from ShadowThief)
The dir/del lines produce a message for found/processed or a file not found message. no problem.
That leaves just one line, the change to drive %%D.
This succeeds with changing the drive or fails with an error message.
Not acting on this error or preventing this error from happening causes your script to continue with the dir/del lines after failing to switch to another drive.

So what is missing is sanity check on the %%D drive letters and/or error checking/handling on the change drive code.

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Finding cause of "The filename, directory name, or volume label syntax is incorrect" error when deleting files blunt

#12 Post by DOSadnie » 21 Jul 2023 12:39

I came up with an alternative method

Code: Select all

@echo off

setlocal enabledelayedexpansion

set "LIST OF VOLUMES=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"

set "PATTERN OF FILES=~$*.xlsm"

echo.Deleting from all volumes files
echo.
echo.%PATTERN OF FILES%

for %%d in (%LIST OF VOLUMES%) do (
    echo.
    echo.
    echo.
    echo.%%d:\
    echo.
    del /ah /s /q "%%d:\%PATTERN OF FILES%"
)
Thank you both for your help

Post Reply