You are viewing limited content. For full access, please sign in.

Question

Question

copy and paste windows files

SDK
asked on May 3, 2018

Hi all,

 

Using Workflow and SDK, I need to :
1. Check if windows' file exist,
2.a. If exist, Copy and Paste it from folder original to new folder.

2.b. If doesn't exist, to make an email alert.

 

Someone already did this?

If yes, can he or she share the code please?

 

Thanks in advance.

Regards

0 0

Answer

SELECTED ANSWER
replied on May 3, 2018

Olivier,

Here is a simple script that will check to see if a file exists and if it does exist it will copy it to another folder.  Note: The account that Workflow is running under must have permissions to create the new target folder and copy a file to that new folder.

The script is expecting tokens that define the SourceFileName, SourceFolder, and TargetFolder.  It returns a token called FileExists that you can evaluate in a Conditional Sequence activity.

 

namespace WorkflowActivity.Scripting.Script
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;
    using  System.IO;

    /// <summary>
    /// Provides one or more methods that can be run when the workflow scripting activity is performed.
    /// </summary>
    public class Script1 : ScriptClass90
    {
        /// <summary>
        /// This method is run when the activity is performed.
        /// </summary>
        protected override void Execute()
        {
            // Get the token values for source filename, source path, and target path...
            String sourcePath = this.GetTokenValue("SourcePath").ToString();
            String sourceFileName = this.GetTokenValue("SourceFileName").ToString();
            String targetPath = this.GetTokenValue("TargetPath").ToString();
            String sourcePathAndName = Path.Combine(sourcePath, sourceFileName);
            String targetPathAndName = Path.Combine(targetPath, sourceFileName);
            Boolean fileExists = false;

            try
            {
                // Check to see if the source file exists...
                if (File.Exists(sourcePathAndName))
                {
                    fileExists = true;

                    // Check to see if the target path exists.  If not then create it...
                    if (Directory.Exists(targetPath) == false)
                    {
                        Directory.CreateDirectory(targetPath);
                    }

                    // Copy the souorce file to the target directory...
                    File.Copy(sourcePathAndName, targetPathAndName, true);
                }

            }
            catch (System.Exception e)
            {
                this.WorkflowApi.TrackError(e.Message);
            }

            // Set the result token for later processing...
            this.SetTokenValue("FileExists", fileExists);

        }
    }
}

Here is a sample workflow;

The tokens containing the file and path info;

And the Conditional Sequence criteria;

 

2 0

Replies

replied on May 4, 2018

Hi Cliff,

 

Thanks for your return. I'm going to try it now.

Regards

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.