Spaces cause batch file closeout?

July 30, 2010 at 15:26:38
Specs: Windows XP
Hello, I've made a script that scans the path and echos yup when it finds the string. the issue is that the path causes the batch file to close when it finds a space in the path.


@echo off
setlocal enabledelayedexpansion
set thepath=%~dp0
echo %thepath%
:next
set int=0
:loop2
cls
echo !thepath:~-%int%,10!
set test="!thepath:~-%int%,10!"
if ["%test%"]==["^\tf^\sound^\"] echo Yup
set /a int=%int%+1
pause
goto loop2


See More: Spaces cause batch file closeout?

Report •


#1
July 30, 2010 at 16:40:40
Enclose the path in double quotes ("P:\a\t\h")

Report •

#2
July 30, 2010 at 17:48:08
Soldat:
set test="!thepath:~-%int%,10!"
if ["%test%"]==["^\tf^\sound^\"] echo Yup

You set test enclosed in " " so when you test using "%test%" the string is then enclosed in double double quotes e.g. "" String "" and the condition will never be true. Escaping \ is not required.

I put this together as a trial, seems to work ok here.

@echo off
setlocal enabledelayedexpansion
cls
set thepath=c:\trial\the path\tf\sound\forever\castle munro\archie bunker\
:next
set int=0
:loop2
cls
echo !thepath:~-%int%,10!
set test=!thepath:~-%int%,10!
echo Test = !test!
if ["%test%"]==["\tf\sound\"] echo Yup&& exit /b
set /a int+=1
goto loop2

Hope this helps.


Report •

#3
July 31, 2010 at 00:22:33
Ah thanks, you saved my program! This is now to be a vital part of my script! Again thanks.

Report •

Related Solutions

#4
July 31, 2010 at 01:03:58
You're welcome, thanks for coming back to report your success.

Good luck.


Report •


Ask Question