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?
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.

NVITCommented:
Try changing to
dir/b "*.exe"

Open in new window

0

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
NVITCommented:
Or
dir /b /s "*.exe"

Open in new window

0
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
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

NVITCommented:
Sorry... Working from phone here.
Also change
start %%q

Open in new window

To
start "%%q"

Open in new window

0
NVITCommented:
Wrap the DIR command in quotes as you had originally.



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

Open in new window

0
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
0
NVITCommented:
You had single back quotes originally. See my last code post
0
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...
0
Lonelygirl_2012Author Commented:
Hello,

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

Still error..

Thanks
0
NVITCommented:
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

0
NVITCommented:
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

0
Bill PrewCommented:
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
0
Lonelygirl_2012Author Commented:
Thank you
0
NVITCommented:
It was a pleasure, LonelyGirl_2012. Aloha!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.