Opening file with spaces in Windows via Command Pr

2019-02-19 02:17发布

I want to open a file (particularly video files) in its default program via script. When I come across a file name with spaces, it is taken as several arguments which was no surprise:

  C:\folder>start test space.avi
  The system cannot find the file test.

However, when I surround the file name with quotes:

  C:\folder>start "test space.avi"

instead of opening the file in its default program (VLC), a new Command Prompt window is opened up to the directory of the file.

Opening a file without a space or quotes opens the file in VLC as expected.

How can I get around this?

4条回答
我命由我不由天
2楼-- · 2019-02-19 02:35

Adding the initial "" as Glen indicated ensures CMD continues and does not enter wait mode. This is particularly important in a batch file.

In summary:

1- Open file and wait for user to close it before proceeding to next command

start "" "test space.avi"

2- Open file and continue to next command (without waiting)

start "" "test space.avi"

Depending on your need you might opt for 1 or 2.

查看更多
可以哭但决不认输i
3楼-- · 2019-02-19 02:44

I suspect start does something special when the first char of the first argument is a quote. The first argument is a window title, and the second is the command/file to open

start "" "test space.avi"

http://ss64.com/nt/start.html

查看更多
姐就是有狂的资本
4楼-- · 2019-02-19 02:46

It is a well known problem (at least for me :-)

You will have to use a short name format in your CMD script. To find out a short name for a particular file do the following:

  1. Open a CMD window pointing to the file's folder.
  2. Run the command: $> dir /X
  3. In the middle column you will see a short name for the file of interest. In your particular case it will be something like: TESTSP~1.AVI
  4. Use this bare name in your script.

Hope, it helps

查看更多
Anthone
5楼-- · 2019-02-19 02:58

Just leave off start, and surround the full filename (including any path) with double-quotes. This works fine on my system:

C:\>"test space.avi"
查看更多
登录 后发表回答