Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

Rename specific files using command prompt if exists

Shaaz
9 - Comet

Hi Team,

 

How to rename few specific files dynamically in folder using command prompt?

 

Below is the scenario.

I've few set of files in Folder A. (AB_India_12345.xlsx, AB_China_4573.xlsx, XYZ_India_12345.xlsx, PQR_TYR_12345.csv)

The file rename should happen on 1st 2 files only since it has pattern of AB_*

 

The renamed should be AB_India.xlsx, AB_China.xlsx and the last 2 files should not change.

 

In a 2nd Scenario: Assume Folder A has only XYZ_India_12345.xlsx, PQR_TYR_12345.csv

So, here workflow should run fine and should not rename any file.

1 REPLY 1
BrandonB
Alteryx
Alteryx

@Shaaz your first step should be to use a Directory tool. This will pull the list of all file in the folder of interest. You can also check the box for Include Subdirectories if you need to look through multiple folder levels for all files.

 

Then you will want to follow the Directory tool with a Filter tool. Configure the Filter tool with the Custom Filter option and use the expression Left([FileName], 3) = "AB_". This will only pass through the files that follow the AB_ pattern in the beginning of the file name. 

 

Now that you have the specific files isolated, you want to use a Formula tool as the next step to extract exactly what you want from the file. You can use the following formula:

REGEX_Replace([FileName], "(.*_)(.*)_(.*)(\..*)", "$1$2$4")

This is looking for a specific pattern in the file using a regular expression to pull only the elements you need. 

 

Then you  create the desired command which is generally REN to rename the files.

 

After that you use a Summarize tool to collapse these commands into a single cell with newline delimiters.

 

Finally, you pass this to a Run Command tool to create a batch file and execute the command. 

 

Rename.png

You can reference the Run Command tool configuration here: https://community.alteryx.com/t5/Alteryx-Designer-Desktop-Discussions/Copy-File-from-One-Folder-to-A... 

 

My workflow is attached to this post.

Labels