Page 1 of 1

copy with Process.execute

Posted: Wed Jan 13, 2021 9:59 pm
by hover27
Hi,
I want to copy files with Process.execute()
The reason therefor is, that I want to use some parameters, which aren't possible with Switch internal copy functin but with windwos copy.
(to copy two files together)

I tried to do this:

path1 = "\"C:\\folder1\\folder2\\myFile.txt\"";
path2 = "\"C:\\folder1\\folder2\\myFileNEW.txt\"";

var theCommand = 'copy ' + path1 + ' ' + path2 ;
job.log(1, theCommand);

Process.execute(theCommand) ;
job.log(1, Process.stdout);

I get the message:
Failed to run external process: copy C:\folder1\folder2\myFile.txt C:\folder1\folder2\myFileNEW.txt

Does anybody know, what is my mistake?

Thx
hover

Re: copy with Process.execute

Posted: Wed Jan 13, 2021 10:25 pm
by Padawan
Can you try this?
job.log(1, Process.stderr);

Stdout gives normal output, stderr error output

Re: copy with Process.execute

Posted: Thu Jan 14, 2021 8:30 am
by hover27
the script does not go to the line with the log, so there is no message/log entry for job.log(1, Process.stderr)

when switch tries to execute Process.execute(theCommand)
there is a message from with type = Error and Module = Control:
Failed to run external process: copy C:\folder1\folder2\myFile.txt C:\folder1\folder2\myFileNEW.txt

Re: copy with Process.execute

Posted: Thu Jan 14, 2021 8:49 am
by jan_suhr
Can't you just do like this in Switch:
Snag_9f2a24b.png
Snag_9f2a24b.png (5.41 KiB) Viewed 9404 times

Re: copy with Process.execute

Posted: Thu Jan 14, 2021 8:58 am
by hover27
@jan_suhr
no, that is not what I need.
I need to copy two files together.
That is only possible with the internal command copy.
With the parameters of copy, I can copy two files into one file.
That's why I need to do it with Process.execute() and the copy command.

Re: copy with Process.execute

Posted: Thu Jan 14, 2021 9:16 am
by jan_suhr
OK, so want to merge two files

Re: copy with Process.execute

Posted: Thu Jan 14, 2021 9:18 am
by jan_suhr
With Switch Scripter you can read the file content from both files and merge that content in to a new file.

Re: copy with Process.execute

Posted: Thu Jan 14, 2021 11:16 am
by hover27
Ah, that's a good idea.
I will try this.

Re: copy with Process.execute

Posted: Thu Jan 14, 2021 11:59 am
by laurentd
Here is another possibility:
This is the right command: copy /b path1 + path2 outputPath

You can use the Execute command element and point to a .bat file
Output: file at path

If one file is the Switch job and the other file is a fixed file (eg when you add a header to your file):
Arguments: "%1" "%2"
Bat file content: copy /b “fixedFilePath” + %1 %2

If the two files are variable, first assemble them in a job folder, then use this:
Arguments: "%1\[Job.NestedName:Index='2',After=' ']" "%1\[Job.NestedName:Index='3',After=' ']" "%2"
Bat file content: copy /b %1 + %2 %3

Re: copy with Process.execute

Posted: Thu Jan 14, 2021 6:19 pm
by hover27
@laurentd
that is exactly what I want to do.
Thank you, good idea.

It seems, that Switch isn't able to process the copy command with Process.execute().

Re: copy with Process.execute

Posted: Thu Jan 14, 2021 7:30 pm
by jan_suhr
hover27 wrote: Thu Jan 14, 2021 6:19 pm @laurentd
that is exactly what I want to do.
Thank you, good idea.

It seems, that Switch isn't able to process the copy command with Process.execute().
It is probably because it isn't in the path for Switch to find it. A Switch Script is running in it's own environment and you have to adjust the path setting.

Re: copy with Process.execute

Posted: Fri Jan 15, 2021 1:48 pm
by hover27
jan_suhr wrote: Thu Jan 14, 2021 7:30 pm
It is probably because it isn't in the path for Switch to find it. A Switch Script is running in it's own environment and you have to adjust the path setting.
That was my first thought also, but 'copy' is an internal DOS command. There is no separate path for it.