I am doing the “Control Flow in Studio” course from UiPath Academy. While using “MoveFile” to move a pdf file from File.FullName to a sub-folder of the current folder [specifically strInvoiceDirectory + “2018 invoices”] I am getting an error stating that “a file cannot be created when it is already existing”. I don’t understand why the Activity “MoveFile” is trying to create that file while I would expect to only move it.
Can someone explain to me what I have done wrong?
Thank you so much
Could you check what is the value that you are passing to the Move File Activity ? Check both the Source and the Destination values, if both are same values that would mean you are trying to move the file to the same folder, but as the file is already there, it gives out the error mentioned.
Let us know if both source and Destination values are different.
Hi @supermanPunch. Thank you so much - I appreciate your help.
I use an assign activity saying:
Save To strInvoiceDirectory (strInvoiceDirectory is a string variable I declared)
Value to Save “all invoices” (“all invoices” is a folder that I can see under the Project Panel).
For variable “File” I have used a For Each activity specifying elements called “Invoice” set to “Directory.GetFiles(strInvoiceDirectory)” and then I use assign activity to Save To = File, Value to Save = new FileInfo(Invoice).
According to your above statements, the value strInvoiceDirectory+"2018 invoices" would be "all invoices2018 invoices". This is not the right way to concatenate folder paths and thus you’re getting a wrong folder path for the file to be moved.
Could you check if that is true by Debugging ?
I believe the value strInvoiceDirectory+"2018 invoices" should be changed to the below Expression :
Path.Combine(strInvoiceDirectory,"2018 invoices")
Let us know if you were able to use this expression in the Move File Activity and was able to get the required output.
I went to correct that expression to strInvoiceDirectory+“\2018 invoices”, which is what the UiPath Academy suggests (I had overlooked the "" - you are right on this one!) This makes the code go through without any error. However, what the MoveFile does is it creates a file inside the directory strInvoiceDirectory with names of “2018 invoices”, “2019 invoices”, “2020 invoices” etc (based on the file names of the original pdfs). while it does not actually MOVE the files. I.e. the pdf is still in its original place, while a file of type “file” is now found in the strInvoiceDirectory folder (and not a pdf file).
Any ideas what I did wrong.
Could you provide us with a Screenshot of the Workflow that you have implemented, if possible include all the steps that you have mentioned in the above posts. It would help us to analyse better and help you faster.
Could you expand any one Case, so that we can check what was implemented in it ? We know it is the Move File Activity but we would require to check the inputs provided to it.
Moving a file is really copying it then deleting the original. The copy is essentially creating a new file in the destination.
strInvoiceDirectory + “2018 invoices”
You should be using Path.Combine instead of the above type of concatenation. Path.Combine makes sure the backslashes are correct. I’m guessing you don’t have a backslash on the end of whatever value is in strInvoiceDirectory. Path.Combine would fix that for you automatically.
I tried that using Path.Combine(strInvoiceDirectory,“2018 invoices”) as also suggested by @supermanPunch.
However, this resulted in the respective file being stored in the directory contained in variable strInvoiceDirectory with file name “2018 invoices”.
The “For each” process in this case yielded an error after the second time an invoice from 2018 was found, because it could not be saved under the same name.
However, what the process should do is save each file under the folder named “2018 invoices”, “2019 invoices” etc, while leaving the file name unchanged. Sorry, I am at a loss somehow.
@cellery , The files and folder were not noticed by me earlier. It seems that the Folder to which you want to move the files are not yet created and hence, we are experiencing the below situation mentioned by you.
You would firstly need to create the folders inside the all invoices folder. If a dynamic method of creating these sub folders need to be done, then use the Create Folder Activity with the below Expression, do this before the Move File Activity, so that the Folder is ready :
Path.Combine(strInvoiceDirectory,"2018 invoices")
You could then use the same expression in the Move File Activity. This should be able to help you perform the move operation.
@supermanPunch, @postwick
I created the target folders (“2018 invoices” etc.) now before running the process.
This did the trick and the process went trough. Thanks a lot to both of you!!!
@postwick as soon as the folders were in place for the Move File activity to move the pdfs there was no need to specify the file name of the file to be moved. It was stored under the original name.