Report

Open a file with spaces from batch file? [Solved/Closed]

Ask a question Branflakes91093 1Posts Saturday July 26, 2008Registration date July 27, 2008 Last seen - Last answered on May 13, 2017 at 01:11 PM by jom
Hello,
I have made a batch file that will ask you what file name you want to open and what directory it's in.
The only problem is that I can't open files with spaces in the name because it treats the first word as the whole file.
Can someone help me?
Here's the code:

cls
@echo off
:CD
cls
echo Change directory?
echo.
set /p "cho=>"
if %cho%==y goto Change
if %cho%==Y goto Change
if %cho%==n goto Open
if %cho%==N goto Open
cls
echo That's not a valid choice, dumbass.
pause
goto CD
:Open
cls
echo What would you like to open? (Name and Extension) Or type "End" to exit.
echo.
set /p "n=>"
if %n%==End goto End
if %n%==end goto End
if %n%=="End" goto End
if %n%=="end" goto End
start %n%
goto Open
goto End
:Change
cls
echo Enter the desired directory.
echo.
set /p "x=>"
CHDIR %x%
goto Open
:end

Placing quotes around the input variables opens up another cmd window for some reason...
Thanks.
Helpful
+29
plus moins
Hi Korra,

the problem with quotes appears for the follwing reason:

syntax of START

START ["title"] [path]

It treats the string in quotes as a title of the new cmd window. So, you may do the following:

START "" "path"

First quotes will be treated as a title and second will be treated as a path.

Hope this helps.
Was this answer helpful?  
Amatuer- Jul 1, 2010 at 04:51 PM
thanks for that ...
PaulSmith1975- Aug 17, 2010 at 07:34 AM
The simplist solutions are always the most obvious...thank you
maga- Dec 7, 2012 at 09:45 PM
thank you, been looking for this in a million websites
jom- May 13, 2017 at 01:11 PM
thank you it helped alot :)
Reply
Helpful
+16
plus moins
Dear frnds,
I know you all suffering from the problem of space in .bat file...
Here is the solution...
Use " " (doublequote space doublequote) instead of space..your problem will be solved...
It worked on my machine..
If you are satisfied with this information..plz let me know on my id
Helpful
+6
plus moins
surround file name with double quotes
anonymous- Dec 14, 2008 at 01:50 AM
Short & Sweet Answer. Thanks. Shrawan.
Korra- Feb 1, 2010 at 08:39 AM
great answer!! thanks!!
ammy- Oct 6, 2010 at 01:13 AM
thanks":-)"
Florida Man- Jun 22, 2011 at 12:57 PM
Thanks for taking the time to answer!
Junaid- Jan 2, 2013 at 02:55 AM
thaks. my problem has also solved
Helpful
+0
plus moins
Hello Korra,
I want to open multiple files with the same extention with a batch file. Can you please send me your batch file or let me know how to create it? I've written one and it works partially. My email address is u3185168@anu.ed.au.
Thanks
Housh

Member requests are more likely to be responded to.

Members can monitor the statuses of their requests from their account pages.

A CCM membership gives you access to additional options.

Not a member yet?

Sign up now. It takes less than a minute and is completely free!