LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to rename a file when moving it

Solved!
Go to solution
How would I rename a file when trying to move it to another directory and it already exists?
I was thinking that I could use a match pattern on both the source and destination paths, because after a certain point, the paths would be the same, and then with another match pattern I would extract just the name and use a build path.
I just don't know if this would actually work. Can someone give me any suggestions?
Thanks in advance!
0 Kudos
Message 1 of 11
(4,309 Views)

Hi Nando,

 

yes, that would work.

Just try the way you already described…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 11
(4,297 Views)

I've not actually tried this, but the Move function has an "Overwrite" switch that will allow you to Overwrite a file if it already exists.  So if I want to move OldFolder/OldFile.txt to NewFolder/NewFile.txt, all I should have to do is specify the two Paths and set Overwrite = T, and the file will be deleted from OldFolder and will appear in NewFolder as NewFile.txt, overwriting any file of that name that already exists.  All in a single (logical) step.

 

If this doesn't work this way, please respond, and I'll be very surprised (and embarrassed).

 

Bob Schor

0 Kudos
Message 3 of 11
(4,271 Views)
The thing is I wish to keep both the old and the new file, that is why I want to rename the file.
0 Kudos
Message 4 of 11
(4,264 Views)

Hi Nando,

 

The thing is I wish to keep both the old and the new file, that is why I want to rename the file.

Ok, just do it!

 

Pseudocode:

while path(oldfile) == path(newfile)
   append suffix to newfile filename
wend
move(newfile)

 

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 5 of 11
(4,258 Views)

@Nando88 wrote:
The thing is I wish to keep both the old and the new file, that is why I want to rename the file.

OK, let's try this again.  You have "OriginalFolder/OriginalFile.txt" and you want "NewFolder/RenamedFile.txt", correct?  And you want OriginalFile to stay put, just have a second copy (with another, or even the same name) somewhere else.  Is that right?

 

In that case, just use the Copy function, and if you want to overwrite a file that is in the Destination folder, set Overwrite to True.

 

[I may be incorrect about this, but I think Unix introduced the notion that "Move" and "Rename" were the same operation.  Hence when you said "Move", I assumed it was "Copy and delete the Original"].

 

In either case, it is a one-step simple operation.  The only time you need to do any extra processing is if you want, at run time, to be able to decide whether or not to overwrite an already-existing file in the Destination Folder.

 

Bob Schor

 

P.S. -- I just noticed GerdW's response.  By "rename", did you mean what Gerd is suggesting, that is, copy but do not overwrite if a file exists?  Yes, in that case, you need to do a little more work to come up with a unique file name, either for the "new" file you are bringing over, or for the existing file.  When I've encountered this situation, I've added a numeric suffix (.001, .002, etc.) to the existing file, keeping them in sequence.  So going back to my example, above, if I'm copying OriginalFolder/OriginalFile.txt to NewFolder/OriginalFile.txt, I would first see if the file "NewFolder/OriginalFile.txt" existed, and if so, would "Rename" (a Move function) it to "OriginalFile.nnn.txt" (where "nnn" starts with 001, and is the lowest number that is not already present in NewFolder).  Once the Move/Rename is done, I can safely Copy without worrying about a "name clash".

 

BS

0 Kudos
Message 6 of 11
(4,253 Views)
How would I progeamatically add the number at the end of the name programatically?
0 Kudos
Message 7 of 11
(4,236 Views)

I think I have a solution. Please correct me if I am wrong.

I would have to compare if the file exists and if it exists, I would increment a shift register, and I would have to split the filename with match pattern, and concatenate with the shift register and the file extension.

0 Kudos
Message 8 of 11
(4,231 Views)

Hi Nando,

 

you don't need MatchPattern when you would use LabVIEWs function that gives you filename and suffix…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 9 of 11
(4,210 Views)
Which vi is that?
0 Kudos
Message 10 of 11
(4,206 Views)