Batch file won't execute file, if it has spaces between.

Hello Experts,

I found this and tested, it worked great but the main issue is that if I have a "My Executable File.exe", the script won't run with an error message, but if I changed "My Executable File.exe" to "MyExecutableFile.exe", it would work. Also, it only found in the current directory but not recursive in my sub-directories that contains all my "*.EXE"

Thank you,

@echo off
setlocal enabledelayedexpansion
set /a counter=0
if exist list do (del list)
echo "">list2
for /f "usebackq tokens=*" %%i in (`dir/b *.exe`) do (
set /a counter+=1
echo %%i>>list2
echo !counter! %%i>>list
)
echo. & echo There are !counter! executible files in current directory. & echo.
type list
echo. & echo Please enter your choice:
set /p errorlevel=
for /f "skip=%errorlevel% tokens=*" %%q in (list2) do start %%q & echo starting %%q... & goto exit
:exit
del list & del list2
Lonelygirl_2012Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITEnd-user supportCommented:
Try changing to
dir/b "*.exe"

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NVITEnd-user supportCommented:
Or
dir /b /s "*.exe"

Open in new window

Lonelygirl_2012Author Commented:
Hi

I have tried both methods and error message now states that "The system cannot find the file specified".
a. dir/b "*.exe"
b. dir /b /s "*.exe"

Thank you,
============================================================
@echo off
setlocal enabledelayedexpansion
set /a counter=0
if exist list do (del list)
echo "">list2
for /f "usebackq tokens=*" %%i in (dir /b /s "*.exe") do (
set /a counter+=1
echo %%i>>list2
echo !counter! %%i>>list
)
echo. & echo There are !counter! executible files in current directory. & echo.
type list
echo. & echo Please enter your choice:
set /p errorlevel=
for /f "skip=%errorlevel% tokens=*" %%q in (list2) do start %%q & echo starting %%q... & goto exit
:exit
del list & del list2
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

NVITEnd-user supportCommented:
Sorry... Working from phone here.
Also change
start %%q

Open in new window

To
start "%%q"

Open in new window

NVITEnd-user supportCommented:
Wrap the DIR command in quotes as you had originally.



for /f "usebackq tokens=*" %%i in (`dir /b /s "*.exe"`) 

Open in new window

Lonelygirl_2012Author Commented:
Hello,

Still not work

a. (dir /b /s "*.exe")
b. ("dir /b /s *.exe")
c.  ("dir /b /s "*.exe"")
d. for /f "skip=%errorlevel% tokens=*" %%q in (list2) do start "%%q" & echo starting
NVITEnd-user supportCommented:
You had single back quotes originally. See my last code post
Lonelygirl_2012Author Commented:
Hello NewVillageIT (NVIT),

The error is now as "dir /b /s "*.exe" with the {'dir /b /s *.exe'}
The error is now as "dir /b /s "*.exe" with the {'dir /b /s "*.exe"'}
etc...
Please enter your choice...
Lonelygirl_2012Author Commented:
Hello,

for /f "usebackq tokens=*" %%i in (`dir /b /s "*.exe"`)

Still error..

Thanks
NVITEnd-user supportCommented:
This part works:
@echo off
 setlocal enabledelayedexpansion
 set /a counter=0
 if exist list do (del list)
 echo "">list2
 for /f "usebackq tokens=*" %%i in (`dir /s /b "*.exe"`) do (
 set /a counter+=1
 echo %%~fi>>list2
)

Open in new window

NVITEnd-user supportCommented:
This works:
Note:
- list vs list2: I don't see a reason for list

@echo off
setlocal enabledelayedexpansion
set /a counter=0
echo "">list2
for /f "usebackq tokens=*" %%i in (`dir /s /b "*.exe"`) do (
 set /a counter+=1
 echo %%~fi>>list2
)
echo. & echo There are !counter! executible files in current directory. & echo. 
type list2
echo. & echo Please enter your choice:
set /p skiplines=
for /f "skip=%skiplines% tokens=*" %%q in (list2) do start "%%q" & echo starting %%q...

:exit
for %%a in (list list2) do if exist %%a del %%a

Open in new window

Bill PrewIT / Software Engineering ConsultantCommented:
Here's how I would approach it:

@echo off
setlocal

set BaseDir=c:\temp
set ListFile=%TEMP%\l1.txt

dir /b /s "%BaseDir%\*.exe" | find /n /v "" > "%TEMP%\l1.txt"
type "%ListFile%"

echo.
set /p "Select=Please enter your choice: "

for /f "tokens=1* delims=[]" %%A in ('find "[%Select%]" ^< "%ListFile%"') do (
  echo Starting "%%~B" . . .
  start "" "%%~B"
)

if exist "%ListFile%" del "%ListFile%">NUL

Open in new window

~bp
Lonelygirl_2012Author Commented:
Thank you
NVITEnd-user supportCommented:
It was a pleasure, LonelyGirl_2012. Aloha!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.