Page 1 of 2
Spaces in filename causing trouble
Posted: 15 May 2015 08:06
by karaziki
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?
Re: Spaces in filename causing trouble
Posted: 15 May 2015 08:16
by Squashman
What are you passing in as %1?
Re: Spaces in filename causing trouble
Posted: 15 May 2015 08:17
by karaziki
Squashman wrote:What are you passing in as %1?
Some test file like
cad file 2.dwgNote: Sorry I edit the question after I realize the space problem. But I think you already understand that problem is %1 part.
Re: Spaces in filename causing trouble
Posted: 15 May 2015 08:22
by Squashman
If you are running the batch file from the cmd prompt.
Code: Select all
mybatch.bat "File with spaces.dwg"
Re: Spaces in filename causing trouble
Posted: 15 May 2015 08:42
by karaziki
Sorry no

Due to nature of need, I'm dragging and dropping files on batch file. That avoids me to use quotes in %1
Re: Spaces in filename causing trouble
Posted: 15 May 2015 09:11
by Squashman
Dragging and Dropping automatically puts quotes around the file name. Has to be something else going on.
Re: Spaces in filename causing trouble
Posted: 15 May 2015 09:22
by Squashman
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 . . .
Re: Spaces in filename causing trouble
Posted: 15 May 2015 12:19
by karaziki
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
Re: Spaces in filename causing trouble
Posted: 15 May 2015 12:35
by Squashman
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.
Re: Spaces in filename causing trouble
Posted: 15 May 2015 12:51
by Squashman
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?
Re: Spaces in filename causing trouble
Posted: 15 May 2015 13:41
by karaziki
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
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.
Re: Spaces in filename causing trouble
Posted: 15 May 2015 13:46
by Squashman
Since we are not seeing the entire batch file we have no idea what could be the problem.
Re: Spaces in filename causing trouble
Posted: 15 May 2015 13:54
by karaziki
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.
Re: Spaces in filename causing trouble
Posted: 15 May 2015 14:00
by karaziki
Ok I found a way for now using as
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
Re: Spaces in filename causing trouble
Posted: 16 May 2015 16:29
by karaziki
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.