Testing if folder exist when there are spaces in a path

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
honyk
Posts: 3
Joined: 17 Apr 2008 01:19

Testing if folder exist when there are spaces in a path

#1 Post by honyk » 17 Apr 2008 01:41

I am using following logic, but it fails if there are spaces in INIT_DIR variable (I use nul test to avoid errors when copying):

Code: Select all

if exist %INIT_DIR%Images\nul (
  mkdir "%OUT_DIR%Images"
  xcopy /y "%INIT_DIR%Images" "%OUT_DIR%Images" >> %LOG% 2>&1
  if ERRORLEVEL 1 (
    GOTO ERROR
  )
)

When quotes are used "%INIT_DIR%Images\nul" then nothing is found...

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#2 Post by jeb » 21 Apr 2008 15:12

Hi honyk,

there are missing the " in the first line.

try

Code: Select all

if exist "%INIT_DIR%Images\nul" (
  mkdir "%OUT_DIR%Images"
  xcopy /y "%INIT_DIR%Images" "%OUT_DIR%Images" >> %LOG% 2>&1
  if ERRORLEVEL 1 (
    GOTO ERROR
  )
)


jeb

honyk
Posts: 3
Joined: 17 Apr 2008 01:19

#3 Post by honyk » 23 Apr 2008 07:03

Please see my last sentence in the original post. This solution doesn't work.

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#4 Post by jeb » 23 Apr 2008 15:51

Sorry I does not read and does not test it

but now ...

Code: Select all

@echo off
setlocal
set INIT_DIR=%~1

call :checkIt result "%INIT_DIR%"
echo result=%result%
goto :eof

::::::::::::::::::::::::::::
:checkIt
setlocal
set p=%~a2
(
endlocal
if "%p:~0,1%" == "d" (
  set %~1=1
) else (
  set %~1=0
)
goto :eof
)


This solution test for directories and results only with one if INIT_DIR is a directory

jeb

Post Reply