i m trying to run a mp3 file on dosbox plateform by c .i put the following code in to the batch file
start c:\xyz.mp3 and trying to call this as system("c:/a.bat"), the system funtion is returing 0 but it is not running it. But the same code working well on Cmd . i m using win 7 32 bit..

The following works for me:

#include <cstdlib>

//use 8.3 filename and directory name format
//my file is in "c:\batch scripts\startmp3.bat"

//To get the 8.3 directory name:
//in cmd window run > dir "c:\" /x | find /i "batch scripts"

system("c:\\batchs~1\\startmp3.bat"); //make sure to escape '\'

//should also be able to use:

See How Windows Generates 8.3 File Name from Long File Names

The important thing is to use 8.3 (DOS) filenames.

Note that system() returns a program status code. It is good practice to use this value for error handling.

commented: but i m still searching for the solution +0

when i m using system("c:\xyz.bat"); it is returning zero and print a message "Illegal commanad:c:\xyz.bat". But on using system("c:/xyz.bat"); it is not showing illegal command statement but also not running.......

Try this:

int Status = system("cmd.exe /c c:/xyz.bat");

Or on Windows 64 bit:

int Status = system("%windir%\\Sysnative\\cmd.exe /c c:/xyz.bat");

I'm not on Windows so I can't test this for you. Can't hurt.


I discovered another solution that lets you avoid 8.3 filenames. Use double-quotes. But you need to escape them with a '\'.

#include <cstdlib>

int returnVal = 0;

//need to put quotes if dir or filename contains spaces
//need to escape quotes
returnVal = system("\"c:/batch scripts/startmp3.bat\"");

if (returnVal != 0)
    printf("An error occurred while running the command.  (%i)\n",returnVal);
    printf("Command executed successfully.\n",returnVal);

return returnVal;

Here is another resource that may be of use:
How to spawn console processes with redirected standard handles

commented: its not showing any illegal command and system() returing 0, but the problem is that my mp3 file is not playing..... on cmd same bath file work good and play mp3 +0

If using '\', you may need to escape it. So use system("c:\\xyz.bat"); instead of system("c:\xyz.bat");

Can you post your code? Also, what program are you trying to use to play the mp3? Check the Windows default: Control Panel (View by: Large icons) => Default programs => Associate a file type or protocol with a program. Look for "mp3." What is the current default?

i m just trying to invoke a batch file by using system() function. Batch file containing start c:\abc.mp3 and i m passing this batch file to system("c:/xyz.bat"); both mp3 and batch file in the same directory , system is returning 0 but the mp3 file is not playing.....

Sounds like the console window is closing and killing the mp3. Add a PAUSE in the batch file after the line that plays the mp3 to keep the console window open while the mp3 plays.

I know what you're trying to do. But I want to see how you are doing it, because obviously what you are doing isn't working for you. Without the code I can't help any further. Also include the batch (.bat) file. What C compiler are you using? Also a screen shot of the error may be helpful.