cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Submit your abstract to the call for content for Discovery Summit Americas by April 23. Selected abstracts will be presented at Discovery Summit, Oct. 21- 24.
Discovery is online this week, April 16 and 18. Join us for these exciting interactive sessions.
Choose Language Hide Translation Bar
pmroz
Super User

Copy files using RunProgram

I need to update some configuration files on users PCs now and then, and I want to do it automatically.  In the past I've created temporary .bat files and executed them with open().  Now I'm trying to use runprogram to do the same thing but it's not working.  I think I'm almost there - any help is greatly appreciated.

Here's my program:

old_file = "C:\some directory\file1.ext";
new_file = "Z:\zdrive folder\file1.ext";

d1 = last modification date(old_file);
d2 = last modification date(new_file);

if (d1 != d1,
	options_list = evallist({"copy", "/y", evalinsert("\["^new_file^"]\"), evalinsert("\["^old_file^"]\")});
	status = runprogram(executable("cmd.exe"), options(options_list), readfunction("text"));
);

If the dates are not identical it puts double quotes around each filename path and executes a copy/y in a DOS shell.  I'm running Windows 7 btw.  I ran the string in a DOS window and it copied the file no problem.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Copy files using RunProgram

There is actually a JSL function (Copy File) that will do this for you.

If you prefer to use RunPogram, you will need to include the copy command with the path to the two files within a single string option. I also needed to include the "/c" option in order to carry out the command before terminating.

Names Default To Here( 1 );

old_file = "C:\some directory\file1.ext";
new_file = "Z:\zdrive folder\file1.ext";

d1 = last modification date(old_file);
d2 = last modification date(new_file);

if (d1 != d1,
	command_string = "/c copy" || new_file || " " || old_file;
	status = RunProgram(
		Executable( "CMD.EXE" ),
		Options( {"/y", command_string} ),
		ReadFunction( "text" )
	);
);
Justin

View solution in original post

4 REPLIES 4

Re: Copy files using RunProgram

There is actually a JSL function (Copy File) that will do this for you.

If you prefer to use RunPogram, you will need to include the copy command with the path to the two files within a single string option. I also needed to include the "/c" option in order to carry out the command before terminating.

Names Default To Here( 1 );

old_file = "C:\some directory\file1.ext";
new_file = "Z:\zdrive folder\file1.ext";

d1 = last modification date(old_file);
d2 = last modification date(new_file);

if (d1 != d1,
	command_string = "/c copy" || new_file || " " || old_file;
	status = RunProgram(
		Executable( "CMD.EXE" ),
		Options( {"/y", command_string} ),
		ReadFunction( "text" )
	);
);
Justin
Craige_Hales
Super User

Re: Copy files using RunProgram

A little more information here , which explains dir and copy are built in to the cmd.exe interpreter.

Craige
pmroz
Super User

Re: Copy files using RunProgram

Thanks Justin and Craige.  I think I'll use copy file as that's a more straightforward solution.  But it's good to know how to use runprogram with builtin DOS commands.

pmroz
Super User

Re: Copy files using RunProgram

A few notes on copy file(from-file, to-file):

 

  • The to-file cannot exist, so I have to delete it first (using file exists and delete file)
  • The copy file program handles unc-path filenames (e.g. \\smith.company.com\dir1\dir2\dir3\file.ext).  The DOS copy command seemed to have trouble with that.

So, copy file rules the day!  Thanks again.