Spaces in filename causing trouble

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
karaziki
Posts: 22
Joined: 28 Nov 2014 22:16

Spaces in filename causing trouble

#1 Post by karaziki » 15 May 2015 08:06

Hi;

The code below works fine unless there is a space in filename. I also faced same before about numeric values (1 3 8) in filenames.

Code: Select all

SET cadfile=.dwg .dxf .dwf
FOR %%G in (%cadfile%) do IF /I (%~x1)==(%%~G) GOTO cadconvert

Does anyone knows how can I handle this?
Last edited by karaziki on 15 May 2015 08:16, edited 1 time in total.

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

Re: Spaces in filename causing trouble

#2 Post by Squashman » 15 May 2015 08:16

What are you passing in as %1?

karaziki
Posts: 22
Joined: 28 Nov 2014 22:16

Re: Spaces in filename causing trouble

#3 Post by karaziki » 15 May 2015 08:17

Squashman wrote:What are you passing in as %1?

Some test file like cad file 2.dwg

Note: Sorry I edit the question after I realize the space problem. But I think you already understand that problem is %1 part.

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

Re: Spaces in filename causing trouble

#4 Post by Squashman » 15 May 2015 08:22

If you are running the batch file from the cmd prompt.

Code: Select all

mybatch.bat "File with spaces.dwg"

karaziki
Posts: 22
Joined: 28 Nov 2014 22:16

Re: Spaces in filename causing trouble

#5 Post by karaziki » 15 May 2015 08:42

Sorry no :(
Due to nature of need, I'm dragging and dropping files on batch file. That avoids me to use quotes in %1

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

Re: Spaces in filename causing trouble

#6 Post by Squashman » 15 May 2015 09:11

Dragging and Dropping automatically puts quotes around the file name. Has to be something else going on.

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

Re: Spaces in filename causing trouble

#7 Post by Squashman » 15 May 2015 09:22

I am assuming you are not showing us all the code or you are showing us obfuscated code because your code works just fine.
All I did was add a few echos and some pauses to see the output.

Code: Select all

@echo off
echo %1
SET cadfile=.dwg .dxf .dwf
FOR %%G in (%cadfile%) do IF /I (%~x1)==(%%~G) goto cadconvert
echo did not convert %~nx1
pause
goto :eof

:cadconvert
echo %~nx1
pause

Output

Code: Select all

"C:\BatchFiles\TESTING\s p a c e s.dwg"
s p a c e s.dwg
Press any key to continue . . .

karaziki
Posts: 22
Joined: 28 Nov 2014 22:16

Re: Spaces in filename causing trouble

#8 Post by karaziki » 15 May 2015 12:19

Well thank you made me realize that I'm looking for wrong place.
This is the complete code, I just started and stuck :(

Code: Select all

@ECHO OFF

SET fpath=%CD%
SET epath=%b2eprogrampathname%
CD /D %epath%

ATTRIB -S -R "%~f1" >NUL 2>NUL

IF       (%1)==()             GOTO launchdirect    ELSE
   :: this line below causing the problem
   IF /I    (%~n1)==(menu)    GOTO launchmenu    ELSE
IF NOT    (%2)==()             GOTO extconvert    ELSE

SET cadfile=.dwg .dxf .dwf
FOR %%G in (%cadfile%) do IF /I (%~x1)==(%%~G) GOTO cadconvert

ECHO %~x1
PAUSE
exit


But I used that code before. I dont understand what is the matter this time?
Hope you have an idea and thank you for your help

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

Re: Spaces in filename causing trouble

#9 Post by Squashman » 15 May 2015 12:35

That CODE should never have worked!!!! You can't use the ELSE like that.

From the IF commands help file.

Code: Select all

The ELSE clause must occur on the same line as the command after the IF.  For
example:

    IF EXIST filename. (
        del filename.
    ) ELSE (
        echo filename. missing.
    )

The following would NOT work because the del command needs to be terminated
by a newline:

    IF EXIST filename. del filename. ELSE echo filename. missing

Nor would the following work, since the ELSE command must be on the same line
as the end of the IF command:

    IF EXIST filename. del filename.
    ELSE echo filename. missing

The following would work if you want it all on one line:

    IF EXIST filename. (del filename.) ELSE echo filename. missing


You are just complicating things by using the () with your IF comparisons.

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

Re: Spaces in filename causing trouble

#10 Post by Squashman » 15 May 2015 12:51

You don't need an of the ELSE statements in your code. But I am also confused on why you are checking for MENU. You said you will always be dragging and dropping files onto the batch file. Are you telling me you are going to drag a file named MENU onto the batch file just to see the menu?

karaziki
Posts: 22
Joined: 28 Nov 2014 22:16

Re: Spaces in filename causing trouble

#11 Post by karaziki » 15 May 2015 13:41

Code updated regarding to your warning, thanks.
Works as before but the second IF still causing terminate

Code: Select all

IF (%1)==() (GOTO launchdirect)
::IF (%~n1)==(menu) (GOTO launchmenu)
IF NOT    (%2)==() (GOTO extconvert)

And yes exactly as you expressed. checking for menu is just a simple way to see menu. I also tried to solve with passing menu as a parameter

Code: Select all

IF (%*)==(menu) (GOTO launchmenu)

Nothing changes, still terminates

And when I remove parentheses nothing works. using "" instead of parentheses also do not. What else I can do I don't know.
Last edited by karaziki on 15 May 2015 13:51, edited 1 time in total.

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

Re: Spaces in filename causing trouble

#12 Post by Squashman » 15 May 2015 13:46

Since we are not seeing the entire batch file we have no idea what could be the problem.

karaziki
Posts: 22
Joined: 28 Nov 2014 22:16

Re: Spaces in filename causing trouble

#13 Post by karaziki » 15 May 2015 13:54

This is complete code

Code: Select all

@ECHO OFF

SET fpath=%CD%
SET epath=%b2eprogrampathname%
CD /D %epath%

ATTRIB -S -R "%~f1" >NUL 2>NUL

IF (%1)==() (GOTO launchdirect)
::IF (%~n1)==(menu) (GOTO launchmenu)
IF NOT    (%2)==() (GOTO extconvert)

SET pldfile=.pld .dds
FOR %%G in (%pldfile%) do IF /I (%~x1)==(%%~G) GOTO launchdirect


ECHO %~x1
PAUSE
exit

:launchdirect
ECHO launchdirect
PAUSE
exit

:launchmenu
ECHO launchmenu
PAUSE
exit

:extconvert
ECHO extconvert
PAUSE
exit

Sure many things will come after but what I execute now is this. And still looking a way to trigger menu without an external file.

Notes: The variable %b2eprogrampathname% works fine. It is equal to %~dp0
When I comment (disable) second IF everything works fine.

karaziki
Posts: 22
Joined: 28 Nov 2014 22:16

Re: Spaces in filename causing trouble

#14 Post by karaziki » 15 May 2015 14:00

Ok I found a way for now using as

Code: Select all

IF (%1)==(menu) (GOTO launchmenu)
solves problem. But still confused about why it doesn't accept filename. I'll be happy on any comments or suggestions to fix my mistakes.

All bests

karaziki
Posts: 22
Joined: 28 Nov 2014 22:16

Re: Spaces in filename causing trouble

#15 Post by karaziki » 16 May 2015 16:29

This is the style you need:

Code: Select all

IF /i "%~n1"=="import" GOTO launchdirect


If it still fails then look at other similar lines and change them also.

Post Reply