-1

The command

type “File Name with Spaces.txt”

Works!!

But

set  “Fname=File Name with Spaces.txt”

type “%Fname%”

Or

type “!Fname!”

Failed !!

Message: The system cannot find the file specified

??

npocmaka
  • 55,367
  • 18
  • 148
  • 187
Uri L
  • 1
  • 2

1 Answers1

0
set Fname=File Name with Spaces.txt
type "%Fname%"

should work. You don't really need the quotes in the set command in this case. But they are important when you access the variable's content in the type command.

Edit:
I am back at a pc where I can test things again. @Squashman and @Mofi are right about the main point that you should use straight double quotes (") and in saying that the set "Fname=File Name & Spaces.txt" is a legitimate and often the recommended syntax. However I would also like to mention another possible variation on the theme. When you do

set Fname="File Name & Spaces.txt"

The quotes will become part of the stored variable:

echo %Fname%
> "File Name & Spaces.txt"

and the type command referencing a filename containing a blank and an ampersand can then be issued without the quotes:

type %Fname%

If you want to acces the contents without the surrounding quotes you can still do

echo %Fname:"=%
> File Name with Spaces.txt

(see set /? for further details on variable handling in cmd.)

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
  • Sorry, haven't got access to a computer at the moment. I am answering from my Android smartphone. But so far set commands like this have worked for me in many cases. – Carsten Massmann Dec 17 '17 at 22:47
  • 3
    There is nothing wrong with putting quotes around the SET command. It protects special chatacters and does no harm using them. – Squashman Dec 17 '17 at 22:47
  • 1
    Please read answer on [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) The syntax `set "Fname=File Name & Spaces.txt"` (straight quotes) is highly recommended for various reasons and necessary if file name contains a Windows command line operator like `&` which should be interpreted as literal character and not as operator. – Mofi Dec 18 '17 at 09:06