community
cancel
Showing results for 
Search instead for 
Did you mean: 

Alteryx Designer Knowledge Base

Definitive answers from Designer experts.

Batch Files in Alteryx Part 2: Moving Files

Community Operations Manager
Community Operations Manager
Created on

This is the second article in the series of using batch files to rename, move, and delete files. This workflow will create a .bat file which you can use in workflows that use the files you specify in this workflow to move to another directory.

 

Moving Files

 

Creating the Batch file workflows:

Tools needed in order of use: (1)Directory Input, (2)Select, (3)Formula, (4)Summarize, (5)Output Tools.

   Directory Tool (1) –

  1. Browse to the file or files you are looking to change

                                      

  Select Tool (2)–

  1. Fields you will need are Fullpath, Directory, and File Name

 

  Formula Tool (3)– Output Fields Created: Command and Batch File.

  1. Create a new field, Command (String)This is the field you want to build your command.
    • Note -The directory that you want to move the files to can be hard coded by adding the directory straight into the function or you can append another directory from another Directory tool before adding to the Formula Tool. In the sample, we hard coded the directory
    • Expression: 
      'Move '+ '"'+[FullPath]+ '"'+ ' ' + + '"'+ 'C:\Users\jdoe\Documents\Sample Data\Command Line Move'+ '"'
    • Sample Output - Move "C:\Users\jdoe\Documents\Sample Data\Command Line Test\$200.xlsx" "C:\Users\jdoe\Documents\Sample Data\Command Line Move"
  2. Create a new field, Batch File (String) – You will take the Directory field from the Directory Input and add a name of what you want the Batch File to be.
    • Expression: 
      [Directory]+'Moving_files.bat'
    • Sample Output - C:\Users\JDoe\Documents\Sample Data\Command Line Test\Moving_files.bat

 

Summarize Tool (4) –

  1. Select the Command field and the action will be to concatenate using a separator as \n.
  2. select the Batch file and the action will  be First.
  3. You can rename the Output fields if you choose as it will change the fields to Concat_Command and First Batch File.

Summarize.png

 

Output Tool (5) –

  1. Browse to save file as any name you want (you will be replacing this name). You will want to use the Save as type: (.csv) and add .bat to end of the name. We want to transform that txt file to a bat file.
  2. In the Delimiters Row in the Options section add \0 as the delimiter.
  3. Select Take/Table Name From Field and choose Change Entire File Path.
  4. Select the First_Batch file (or name you called the field) in the Field Containing File Name or Part of File Name and unselect Keep Field in Output.

 

 

Adding Batch File to Your Workflow:

The last step is to add this batch file to the Events tab in a Workflow that you want to move the file or files you have chosen in the .bat file in the Configuration window:

  1. Select Add > Run Command> Run Event When> After Run>
  2. Command: Browse to the bat file and click Ok.

 

See attached Sample Workflow.

Attachments
Comments
Alteryx Certified Partner

Thansk for this!

Meteoroid

Hello,

 

Thank you very much for the post, I found it really useful.

I would have a query in regards to this; is it possible to use this same procedure to copy the files instead moving them? In this case, which parameters should be changed?

I would truly appreciate any help.

Thank you

Alteryx Certified Partner

@CO

 

Yes this is possible

 

You just have to use the Copy function instead of the Move

 

As you would do in the Windows prompt command tool

Meteoroid

I got it! Thank you very much!

Alteryx Certified Partner

@CO

 

command should be like this :

 

Copy "source path" "destination path"

Hi Dan,

 

Thanks for your solution first 🙂

 

I am using your solution and it's working but i am also getting below error can you help me on this ?

 Designer x64 The Designer x64 reported: Error running Event #1: Failed to run external program "D:\NEW_DATASET\test\Moving_File.bat": The system cannot find the file specified. ¶ (2)
 

Community Operations Manager
Community Operations Manager

@shukla_sumit The error indicates that the workflow you are using the bat file in cannot find that file. Can you confirm that the file is in that location? Are you creating this file within the workflow that you are attempting to execute the bat file in?

@DanM: I am creating copyfile.csv.bat file using output tool into destination location. but i am not sure about Moving_files.bat .Why we are creating two bat files and when we are using it.

 one more thing at the last step when we are adding bat file using  workflow event at that time which bat file to be used is it copyfiles.csv.bat from the output tool or Moving_files.bat.

 

Can you please clear my doubts ?

wrote:

This is the second article in the series of using batch files to rename, move, and delete files. This workflow will create a .bat file which you can use in workflows that use the files you specify in this workflow to move to another directory.

 

Moving Files

 

Creating the Batch file workflows:

Tools needed in order of use: (1)Directory Input, (2)Select, (3)Formula, (4)Summarize, (5)Output Tools.

   Directory Tool (1) –

  1. Browse to the file or files you are looking to change

                                      

  Select Tool (2)–

  1. Fields you will need are Fullpath, Directory, and File Name

 

  Formula Tool (3)– Output Fields Created: Command and Batch File.

  1. Create a new field, Command (String)  This is the field you want to build your command.
    • Note -The directory that you want to move the files to can be hard coded by adding the directory straight into the function or you can append another directory from another Directory tool before adding to the Formula Tool. In the sample, we hard coded the directory
    • Expression: 
      'Move '+ '"'+[FullPath]+ '"'+ ' ' + + '"'+ 'C:\Users\jdoe\Documents\Sample Data\Command Line Move'+ '"'
    • Sample Output - Move "C:\Users\jdoe\Documents\Sample Data\Command Line Test\$200.xlsx" "C:\Users\jdoe\Documents\Sample Data\Command Line Move"
  2. Create a new field, Batch File (String) – You will take the Directory field from the Directory Input and add a name of what you want the Batch File to be.
    • Expression: 
      [Directory]+'Moving_files.bat'
    • Sample Output - C:\Users\JDoe\Documents\Sample Data\Command Line Test\Moving_files.bat
    • Why creating this and when to use ?

 

Summarize Tool (4) –

  1. Select the Command field and the action will be to concatenate using a separator as \n.
  2. select the Batch file and the action will  be First.
  3. You can rename the Output fields if you choose as it will change the fields to Concat_Command and First Batch File.

Summarize.png

 

Output Tool (5) –

  1. Browse to save file as any name you want (you will be replacing this name). You will want to use the Save as type: (.csv) and add .bat to end of the name. We want to transform that txt file to a bat file.
  2. In the Delimiters Row in the Options section add \0 as the delimiter.
  3. Select Take/Table Name From Field and choose Change Entire File Path.
  4. Select the First_Batch file (or name you called the field) in the Field Containing File Name or Part of File Name and unselect Keep Field in Output.
  5. why creating this and when to use it ?

 

 

Adding Batch File to Your Workflow: (Creating fresh workflow at that time that file is not there then how to add this or when)

The last step is to add this batch file to the Events tab in a Workflow that you want to move the file or files you have chosen in the .bat file in the Configuration window:

  1. Select Add > Run Command> Run Event When> After Run>
  2. Command: Browse to the bat file and click Ok.

 

See attached Sample Workflow.

 

I wanted to run that workflow for every file one by one but when i am running it second time getting error that process is used by other process.

 

@DanMwrote:

This is the second article in the series of using batch files to rename, move, and delete files. This workflow will create a .bat file which you can use in workflows that use the files you specify in this workflow to move to another directory.

 

Moving Files

 

Creating the Batch file workflows:

Tools needed in order of use: (1)Directory Input, (2)Select, (3)Formula, (4)Summarize, (5)Output Tools.

   Directory Tool (1) –

  1. Browse to the file or files you are looking to change

                                      

  Select Tool (2)–

  1. Fields you will need are Fullpath, Directory, and File Name

 

  Formula Tool (3)– Output Fields Created: Command and Batch File.

  1. Create a new field, Command (String)  This is the field you want to build your command.
    • Note -The directory that you want to move the files to can be hard coded by adding the directory straight into the function or you can append another directory from another Directory tool before adding to the Formula Tool. In the sample, we hard coded the directory
    • Expression: 
      'Move '+ '"'+[FullPath]+ '"'+ ' ' + + '"'+ 'C:\Users\jdoe\Documents\Sample Data\Command Line Move'+ '"'
    • Sample Output - Move "C:\Users\jdoe\Documents\Sample Data\Command Line Test\$200.xlsx" "C:\Users\jdoe\Documents\Sample Data\Command Line Move"
  2. Create a new field, Batch File (String) – You will take the Directory field from the Directory Input and add a name of what you want the Batch File to be.
    • Expression: 
      [Directory]+'Moving_files.bat'
    • Sample Output - C:\Users\JDoe\Documents\Sample Data\Command Line Test\Moving_files.bat

 

Summarize Tool (4) –

  1. Select the Command field and the action will be to concatenate using a separator as \n.
  2. select the Batch file and the action will  be First.
  3. You can rename the Output fields if you choose as it will change the fields to Concat_Command and First Batch File.

Summarize.png

 

Output Tool (5) –

  1. Browse to save file as any name you want (you will be replacing this name). You will want to use the Save as type: (.csv) and add .bat to end of the name. We want to transform that txt file to a bat file.
  2. In the Delimiters Row in the Options section add \0 as the delimiter.
  3. Select Take/Table Name From Field and choose Change Entire File Path.
  4. Select the First_Batch file (or name you called the field) in the Field Containing File Name or Part of File Name and unselect Keep Field in Output.

 

 

Adding Batch File to Your Workflow:

The last step is to add this batch file to the Events tab in a Workflow that you want to move the file or files you have chosen in the .bat file in the Configuration window:

  1. Select Add > Run Command> Run Event When> After Run>
  2. Command: Browse to the bat file and click Ok.

 

See attached Sample Workflow.

Can you please answer red colour questions ?

Community Operations Manager
Community Operations Manager

@shukla_sumit This article shows you how to create a workflow that creates the move command in a batch file. You would then use this file that is created to run either in the Events tab or in the Run Command tool in Alteryx.

 

If you already have a bat file that copy's your data, then you don't need this other bat file. You can either add that existing file to the Events or the Run Command tool.

 

For multiple files to be moved in one workflowIf you would have to create the expression for every file that you want to move. So for each file you will have to create a command to move that file. You could create a batch macro that could accomplish this for you. In the process above you are using the Events window to add this bat file. If you are looking to use a more dynamic process, you will want to take a look at the Run Command tool. This will allow you to dynamically update the pieces of the command so that if you have multiple files, you will be able to run those bat files in a batch process. Information on the Run Command Tool https://help.alteryx.com/2018.1/index.htm#cshid=RunCommand.htm

 

 

Asteroid

Is there an easier way to just move all the files in a folder to a backup folder? Thanks