Running dos command from access vba

Trying to run

        Dim RetVal As Long
        RetVal = Shell("for %%i in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO @if exist %%i: dir %%i:\*.* /s /b >E:\zzz\Zip-Bak\DayBkUp\DirectoryListHistory\%%i.txt")



Get error run time error 53 file not found?

It runs fine in a dos batch file

Trying to create a series of text files with a list of all files on each drive on my computer

Thanks
DIR-ALL-Drives.txt
rogerdjrAsked:
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.

oBdACommented:
1. Because "for" is an internal command of cmd.exe, not a stand-alone executable.
2. You're not running this command as a batch file, so you need to use a single percent sign for the loop variable.
Try
"cmd.exe /c for %i in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO @if exist %i: dir %i:\*.* /s /b >E:\zzz\Zip-Bak\DayBkUp\DirectoryListHistory\%i.txt"

Open in new window

crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
hi Roger,

another option is to use a different way instead. Here is a free tool to load file names and other information into Access:

www.AccessMVP.com/strive4peace/DB_ListFiles.htm


have an awesome day,
crystal
rogerdjrAuthor Commented:
Tried single % still get the error message
oBdACommented:
Using single percent signs doesn't help without calling cmd.exe. You would have needed to put the string I provided into the Shell() command.
Here's the full version based on your sample:
Dim RetVal As Long
RetVal = Shell("cmd.exe /c for %i in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO @if exist %i: dir %i:\*.* /s /b >E:\zzz\Zip-Bak\DayBkUp\DirectoryListHistory\%i.txt")

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
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
R

From novice to tech pro — start learning today.