Double quotes and spaces in filenames...cause problems

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miskox
Posts: 668
Joined: 28 Jun 2010 03:46

Double quotes and spaces in filenames...cause problems

#1 Post by miskox » 16 Feb 2014 10:23

Hi all,

let's say I have a file with a space in the filename:

Code: Select all

@echo off
>file1.txt (echo TEST)
>"file 2.txt" (echo TEST)

if exist "file1.txt" echo 1
if exist ""file1.txt"" echo 2
if exist "file 2.txt" echo 3
if exist ""file 2.txt"" echo 4


results:

Code: Select all

1
2
3


So file "file 2.txt", which has space in the filename, is not recognized and this might cause problems. If filename does not contain a space double quotes are OK.

So it is important to remove duplicate quotes for the program to work as expected.

And now make files 'file' and '2.txt' (file 'file has no extension).

Run the command procedure again. Notepad (or your default .txt editor) will open.

Remove the 'file' file and run it again. Notepad is not started.

Again create the 'file' file and remove file 2.txt.

Now the error we get is:

Code: Select all

2.txt"" is not recognized as an internal command...


Maybe this helps someone.

Saso

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Double quotes and spaces in filenames...cause problems

#2 Post by Squashman » 16 Feb 2014 11:05

You cannot name files with double quotes so why would you intentionally put two sets of double quotes around your file name?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Double quotes and spaces in filenames...cause problems

#3 Post by penpen » 16 Feb 2014 18:15

In addition:
- a separated filename/path string is evaluated to one string (<-- the reason, why ""file 2.txt"" fails), and
- the filename/path is normalized before it is used,
so the following works, too:

Code: Select all

if exist file" "2.txt echo 5
if exist "f"i"l"e" "2"."t"x"t echo 6
f exist "non existing directory\..\"file" "2.txt echo 7

penpen

miskox
Posts: 668
Joined: 28 Jun 2010 03:46

Re: Double quotes and spaces in filenames...cause problems

#4 Post by miskox » 20 Feb 2014 12:18

Squashman wrote:You cannot name files with double quotes so why would you intentionally put two sets of double quotes around your file name?


(Sorry for a late reply)

For example when using variables with quotes or something:

Code: Select all

set filnam="%fil%"


might cause problems if %fil% already has quotes etc. In this way we can have double qoutes and

Code: Select all

if exist ....


command will not work as expected. So we know that we must remove quotes that are not needed.

Saso

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Double quotes and spaces in filenames...cause problems

#5 Post by Squashman » 20 Feb 2014 12:56

miskox wrote:For example when using variables with quotes or something:

Code: Select all

set filnam="%fil%"


Why are you coding it that way?

Post Reply