Move a file from one folder to another folder using pega

Both the File.Move method and fileUtils MoveFile method is not working. Throwing an exception(Cannot create a file when that file already exists.) for File.Move method and returning a false for MoveFile method. Please give me a solution.

Attached Screenshots.


Keep up to date on this post and subscribe to comments

November 19, 2019 - 6:39am

Make sure you give the destination path correctly.
In your case
Source: F:/test/testFile.txt

Destination: F:/test/Processed/testFile.txt
Also destination folder should not contain file testFile.txt.

November 19, 2019 - 7:18am
Response to chakri5122

Yeah, its working for a txt file, but not for a xlsx file format. How can I resolve that?

November 19, 2019 - 11:55am
Response to AswaniVD

Hi Aswani, in order to resolve and move for an xlsx file format, you should place the first full path of the xlsx file under the sourcePath (include the file name and extension) value, ex. (F:\Documents\test\testFile.xlsx), and in the destinationPath value you should include the destination directory, in addition to the filename and extension of the file that you are using to move initially, ex (F:\Documents\Processed\testFile.xlsx). This should work. I have attached a screenshot as well. This is using the MoveFile method under the FileUtils tool.

November 20, 2019 - 4:02am
Response to NiaC0459

Hi Nia,

Thanks for the update. But it seems to be not working for me. I have tried the same as you showed in the screenshot. Bad luck!!!. Still returning as false. PFA.

November 20, 2019 - 5:01am

It should work 100%. There is no doubt.

Processed folder is exist? If no then create that folder.

November 20, 2019 - 5:42am
Response to Jayachandra Reddy K

Yes. It exists. That's why the text file successfully moved. But didn't work for excel.

November 20, 2019 - 5:58am

OMG surprise. Which version of pega robotics?

Work around: if you know .net then write script as below.

Add using System.IO (in namespace section)

public void MoveFile(string SourceFile, string TargetFile)
            File.Move(SourceFile, TargetFile);

November 21, 2019 - 6:24am
Response to Jayachandra Reddy K

No way...

November 21, 2019 - 4:25pm

Your original exception was that it couldn't create the file because it already exists. Move doesn't allow you to overwrite the file - you must delete it first. This is pretty straightforward using other methods available in FileUtils.

December 6, 2019 - 1:23am
Response to jeffbadger

Thanks for all the replies...!

Bad luck for me,  not any of the methods worked for me. I have done the task by invoking PowerShell script.

December 28, 2019 - 9:52am

We have added a MoveFile method that allows you to overwrite an existing file to the FileUtils in version along with some other methods that should be helpful.

  • MoveFile moves a file to a new destination and overwrites the file, if it already exists.
  • CopyDirectory provides two overloaded methods to copy a directory from the source to a new destination. One method helps to overwrite the destination, if it is already present.
  • FindInFiles provides two overloaded methods to search for specific text in a directory of files. One method accepts wildcard searchPatterns along with searchText.
  • RenameDirectory renames a directory to the specified name.