In a batch file, I want to run an executable that is located in another folder, and use it on files in my current folder.

I can accomplish this by running C:\directory\example\executable.exe --exampleparam examplefile, but what I want to know if it's possible to do this if C:\directory\example is unknown and stored in a variable.


@set folder=C:\directory\example
%folder%\executable.exe --exampleparam examplefile

I couldn't get it to work like this, and wording this use case is a little weird and didn't find much relevant to it.

Here's my full code. It imports the content of 40-something MS Access databases, each contained in their own folder, into a MySQL database using this program's command line.

@set settingsfolder="C:\Users\me\Documents\import"
@set mysqlfolder="C:\Program Files (x86)\DevDesktop\mysql\bin"
@set msa2mysfolder="C:\Program Files (x86)\Bullzip\MS Access to MySQL"
@set mdbfolder="C:\Users\me\Documents\dumps"
@for /D %%f in (%mdbfolder%\*) do @( 
    @set fname=%%~nf
    @set databaseid=!fname:~0,2!
    @set settings=%settingsfolder%\settings.ini
    @type NUL > !settings!
    @echo [MoveDB MSAccess to MySQL] >> !settings!
    @set importdirectory=%mdbfolder%\!fname!\db\database.mdb
    @IF EXIST !importdirectory! @(
        @echo   sourcefilename=!importdirectory! >> !settings!
        @IF !databaseid! == 001 ( type %settingsfolder%\db_init.ini >> !settings! ) ELSE ( type %settingsfolder%\db_append.ini >> !settings! )
        echo Importing !databaseid!...
        %msa2mysfolder%\msa2mys settings=!settings!, autorun
        %mysqlfolder%\mysql --user=root -e "set @dbid=!databaseid!;UPDATE data.table1 SET db_id = @dbid WHERE db_id IS NULL;UPDATE data.table2 SET db_id = @dbid WHERE db_id IS NULL;"
        echo !databaseid! finished

The error I'm getting on each iteration is "Unknown database: \mysql"

It works fine if I use the quoted paths instead of variables.

  • Yes it's possible. What happened when you tried this? Are there spaces in the directory name? If so, you must use the syntax "%folder%\executable.exe". Please do not only respond in the comments. Instead, edit the post with this information. – I say Reinstate Monica Jun 13 '19 at 14:02
  • @TwistyImpersonator Thanks, I posted my full code, I was using quotes. Does the executable name have to be included in the variable? – pidgezero Jun 13 '19 at 14:27
  • @pidgezero, I am suprised that nobody pointed this out.. All of the @ signs are MADNESS! Just @echo off once at the top. – Señor CMasMas Sep 27 '19 at 15:40

You need to enclose the full command in double-quotes, not just the directory path.

Here's the relevant code as you have it now:

set msa2mysfolder="C:\Program Files (x86)\Bullzip\MS Access to MySQL"
%msa2mysfolder%\msa2mys settings=!settings!, autorun

This evaluates to the command:

"C:\Program Files (x86)\Bullzip\MS Access to MySQL"\msa2mys settings=!settings!, autorun

But "C:\Program Files (x86)\Bullzip\MS Access to MySQL"\msa2mys isn't a valid command. Instead it should be "C:\Program Files (x86)\Bullzip\MS Access to MySQL\msa2mys" (notice how the closing quote terminates the command).

So you need to change your code to this (and update your other SET statements accordingly):

set msa2mysfolder=C:\Program Files (x86)\Bullzip\MS Access to MySQL
"%msa2mysfolder%\msa2mys" settings=!settings!, autorun

You should generally avoid including the double-quotes in the SET statement. Waiting until actually executing the command is much better. Further, by not including the double-quotes in the SET statement, you leave open the option of combining or splitting directory paths stored in variables without having to worry about first removing double-quotes.

For example if you have the code:

SET MyFolder=C:\Parent folder\Child folder
SET MySubFolder=Grandchild 1

You can easily combine them with:


Which evaluates to the valid path "C:\Parent folder\Child folder\Grandchild 1". This is much harder to do when you included the quotes in the initial SET statements.

| improve this answer | |
  • This is exactly what I was looking for, thanks! – pidgezero Jun 13 '19 at 16:53

This works quite well in my tests.

My guess is that you have a blank in your folder name, so the batch file should rather look something like this:

set "folder=C:\Program Files (x86)\afolder"
"%folder%\executable.exe" params
| improve this answer | |
  • Thanks for the response, I posted the full code above - I was using quotes when setting variables. – pidgezero Jun 13 '19 at 14:26
  • Debug your script: If the error arrives on the call to mysql, replace mysql by echo to see what is happening. – harrymc Jun 13 '19 at 14:34
  • 1
    @pidgezero, it's better to put the quotes as shown in this answer rather than having them in the variable values (like in your script), particularly when appending... – aschipfl Jun 13 '19 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.