Game Development Community

DOS commands

by Howard Dortch · in Torque 3D Professional · 12/06/2013 (8:35 am) · 14 replies

Is there any way to execute a *.bat file from script?
I need to create a new directory and copy data files on user creation of a new player.

#1
12/06/2013 (9:45 am)
here is the code i use to execute my dos commands from torque script:
%fileWrite=new FileObject();
   %newFileToCreate="tools/myToolScripts/commandToExcute.bat";
   if(  ! %fileWrite.openForWrite(%newFileToCreate) )
	{
	 MessageBoxOK( "Error", "failed to create"@%newFileToCreate );
	 return;
	} 

  if(isFile(%newFileToCreate))
  {
    openFile(%newFileToCreate);
  }

if u already have a bat file then just call this:

openFile(%batFileAddress);

"I need to create a new directory and copy data files on user creation of a new player."
ts already have functions to do that.no need of dos command. look into fileSystem related functions.
#2
12/07/2013 (12:46 pm)
I got it to call the bat file using the openFile(bla) method but now I can't figure out how to pass it a parameter.

in the bat file I have
mkdir %1

but I need to pass it the player name so it makes a directory with that name.

openFile("test.bat" SPC "Bobby") ( and other iterations )does not pass the variable to the batch file.
any thoughts?
#3
12/07/2013 (2:53 pm)
try compiling your .bat file into a .exe and use shellExecute() to call an external exe, I believe you can pass parameters to it then.
#4
12/08/2013 (6:12 am)
The issue is I have to call it from Torque Script
#5
12/08/2013 (10:28 am)
I'm tinkering with a solution to this. The openFile() console function calls Platform::openFile(), which only takes the file name as a parameter. So I'm adding a new function (in sources/platformWin32/winExec.cpp) like so:
// existing function for reference
void Platform::openFile(const char* path )
{
   char filePath[1024];
   Platform::makeFullPathName(path, filePath, sizeof(filePath));

#ifdef UNICODE
   WCHAR p[ 1024 ];
   convertUTF8toUTF16( filePath, p, sizeof( p ) / sizeof( p[ 0 ] ) );
#else
   char* p = filePath;
#endif

   backslash( p );

   ::ShellExecute( NULL,TEXT("open"),p, NULL, NULL, SW_SHOWNORMAL);
}

// -- Note - add declaration for this in source/platform/platform.h
void Platform::openFileParam(const char* path , const char* param)
{
   char filePath[1024];
   Platform::makeFullPathName(path, filePath, sizeof(filePath));

#ifdef UNICODE
   WCHAR p[ 1024 ];
   convertUTF8toUTF16( filePath, p, sizeof( p ) / sizeof( p[ 0 ] ) );
#else
   char* p = filePath;
#endif

   backslash( p );
   
   WCHAR parameters[ 1024 ];
   sprintf((char*)parameters, " %s", param);

   ::ShellExecute( NULL,TEXT("open"),p, parameters, NULL, SW_SHOWNORMAL);
}
// --
The new function takes a file and a space-separated string[1024] for parameters.
Then I've added a console function for it like so:
//-----------------------------------------------------------------------------

DefineEngineFunction( openFile, void, ( const char* file ),,
   "@brief Open the given @a file through the system.  This will usually open the file in its "
   "associated application.n"
   "@param file %Path of the file to open.nn"
   "@note Only present in a Tools build of Torque.n"
   "@ingroup FileSystemn")
{
   Platform::openFile( file );
}

//-----------------------------------------------------------------------------

DefineEngineFunction( openFileParam, void, ( const char* file , const char* param),,
   "@brief Open the given @a file through the system.  This will usually open the file in its "
   "associated application.  Pass command line parameters.n"
   "@param file %Path of the file to open.nn"
   "@param param %Parameters for the file to open.nn"
   "@note Only present in a Tools build of Torque.n"
   "@ingroup FileSystemn")
{
   Platform::openFileParam( file, param );
}
I noticed in the help string that these are only supposed to be available in a Tools build.

Anyway, it compiles but I haven't actually tried it yet....
#6
12/08/2013 (5:33 pm)
Thanks Richard I will have a look at that.
If I am to change engine code I might just as well expose mkdir and copy to script and problem solved. Was hoping for a easy script solution.

All this was about was creating a new directory in application data/mygame/player so each player will have their own data files for their current running game.

Thanks for the reply
#7
12/08/2013 (11:10 pm)
The thing that bugs me is the "tools build" comment - just have to make sure whatever you add is not in some #ifdef block....
#8
12/11/2013 (10:16 am)
@ahsan your method works great with one major exception.
when I use getuserhomedirectory() the value returns the path with forward slashes and when I run the batch file it fails because dos wants back slashes. .../sigh\....
thanks...
#9
12/11/2013 (5:00 pm)
How do I create a string that has quotes in the string?

"Application Data" has a space and needs to be wrapped in a quote so if I write a line of text out to a *.bat file it has to include the quotes.
/boggle

I tried '"Application Data"' but I am sure there is some obscure method to do this...
thanks..
#10
12/11/2013 (6:40 pm)
I'm not sure - perhaps escape the quotes? \"Application Data\"
#11
12/11/2013 (8:11 pm)
"hen I use getuserhomedirectory() the value returns the path with forward slashes and when I run the batch file it fails because dos wants back slashes. .../sigh\....
thanks..."

u have to replace all forward slash to backward from other part of the code.

here is the code i use to replace forward slash:

%cppFile="tools\\myToolScripts\\file_a.cpp";
%cppPath = makeFullPath ( %cppFile );
%cppPath=strreplace (%cppPath,"/","\\" );//< --- line responsible to replace forward slash
%cppPath="\""@%cppPath@"\"";
%outputPath ="\""@getMainDotCsDir( )@"\\"@ filePath (%cppFile)@ "\\"@fileName (%cppFile)@".csv"@"\"";
%outputPath=strreplace (%outputPath,"/","\\" );
#12
12/11/2013 (8:14 pm)
" it has to include the quotes."

i use this:
%outputPath ="\""@getMainDotCsDir( )@"\\"@ filePath (%cppFile)@ "\\"@fileName (%cppFile)@".csv"@"\"";
#13
12/12/2013 (9:55 am)
The strreplace works fine its the result writing to the bat file is the issue.

Here is what gets put in the data.bat file
mkdir C:\Documents and Settings\Administrator\My Documents\mccdata

It will not execute because it lacks quotes

this is what it needs to look like for dos to execute the command
mkdir "C:\Documents and Settings\Administrator\My Documents\mccdata"

any path element with a space needs to be in quotes

Thanks....
#14
12/12/2013 (11:43 am)
Ok that last post got it ahsan thanks

script:
%file.writeLine("copy cache\\user\\*.dat \"" @ %realString @ "\"");

creates this in batch file
copy cache\user\*.dat "C:\Documents and Settings\Administrator\Application Data\mccdata\"

all is right with the world!
Thanks!!