Subscribe Bookmark RSS Feed

Copy files using RunProgram

pmroz

Super User

Joined:

Jun 23, 2011

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
Justin_Chilton

Joined:

Aug 27, 2015

Solution

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
4 REPLIES
Justin_Chilton

Joined:

Aug 27, 2015

Solution

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

Staff

Joined:

Mar 21, 2013

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

Craige
pmroz

Super User

Joined:

Jun 23, 2011

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

Joined:

Jun 23, 2011

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.