Renaming external files

I am wondering if there is a way to rename external files hosted on a user's computer. Essentially I have a folder of files that looks something like:

asdfghjkl_001.LVM
asdfghjkl_002.LVM
asdfghjkl_003.LVM

etc.

Essentially the name is meaningless and gibberish, and I would like to change them. I have written a function in R to do this, but since I use IGOR 99% of the time it would be easier for me to have one written in IGOR.

I have found the built in functions Fstatus, GetFileFolderInfo, SetFileFolderInfo, etc. But I do not think any of them have the ability to rename a user selected file. If I am wrong or missing something please let me know.

Thanks,
Andy
Look at MoveFile.

MoveFile [/D/I[=i ]/M=messageStr /O/P=pathName /S=saveMessageStr /Z[=z ] ] [srcFileStr ] [ as destFileOrFolderStr ]
The MoveFile operation moves or renames a file on disk. A file is renamed by "moving" it to the same folder it is already in using a different name.

You can then delete the original.

Andy
Quote:
You can then delete the original.


I don't think you need to delete the original as MoveFile does not create a copy but rather moves the original file.
Thanks for the replies. I looked at the details of movefile and it'll work for me. Just missed it at my first glance. This might be a basic question, but I am having trouble converted a Path to a string. I have written an example subset of a function I am working on to iteratively rename files in a folder on disk. Any suggestions on converting the Path to a string. I feel like this has to be really easy but I am just missing it.

Function TestFileName()

   GetFileFolderInfo/D("")
   NewPath FolderPath S_Path
   //Want to convert FolderPath to a string for later use

end
You can use PathInfo to get the path as a string but you already have it in S_path:

Function TestFileName()
    GetFileFolderInfo/D ""
    String fullPath = S_path
    NewPath/O FolderPath, fullPath
End


However, if you know the symbolic path name (FolderPath), you should not need the full path to the folder since nearly all Igor operations that deal with files accept the symbolic path name as a parameter via /P.

It appears you want the user to select a folder interactively. I would do it like this:
Function TestFileName()
    NewPath/O FolderPath    // Display choose folder dialog
    if (V_flag != 0)
        Print "User cancelled"
        return -1
    end
    return 0    // Success
End

You can now use FolderPath to reference the folder (e.g., via LoadWave/P). You can use PathInfo to get a full path to the folder in a string, but this is usually not needed.

Thanks Hrodstein, do you think you could explain what is happening with this function.
For instance, how is NewPath opening a dialog itself.
Why use the if statement in this case,
and what does return 0 do/how is it doing it?

thanks!
Quote:
For instance, how is NewPath opening a dialog itself.
Why use the if statement in this case, and what does return 0 do/how is it doing it?


As the help for NewPath says, if you omit the path parameter, it displays a Choose Folder dialog.

The return of 0 for success or -1 for cancel is so that the calling routine can tell if the user cancelled the Choose Folder.

Usually a function that can fail should return some indication of success or failure so that, if the function is used in a larger program, the program can gracefully handle errors.
Thanks.

But what exactly is a "Path" generated from NewPath.

Essentially I want to take the name of the Path (in the example it is FolderPath), and iteratively concatenate a name of each file (from a list of file names) so that I can rename each file in the folder with a modifiable name (which i usually do by modifying strings in a do/while loop.

function testfilename()

    NewPath/O FolderPath                                                       // Display choose folder dialog
    if (V_flag != 0)
        Print "User cancelled"
        return -1
    endif
   
    string FileNameList_str = IndexedFile(FolderPath, -1, ".dat")        //Builds a list of strings with each item in the list being the name of a file in the specified folder
   
    variable FileSelector                            
    do
        string FileName = stringfromlist(FileSelector, FileNameList_str)            //Iteratively pulls out each file name from the FileName list
        string PathToFile = FolderPath + FileName                                        //Ideally I would just concatenate the FolderPath to each FileName
        string FileRename = "ExampleName"+num2str(FileSelector)             //Would build the string that I would use to rename the file
        MoveFile $PathToFile as $FileRename

    while(//terminationcondition)
   
End
Quote:
But what exactly is a "Path" generated from NewPath.


Please read the documentation for symbolic paths:
DisplayHelpTopic "Symbolic Paths"


If the source and dest for MoveFile are in the same folder then you should use /P=FolderPath and simple file names for the original name and the new name. Read the help for /P in MoveFile for details.

Quote:
MoveFile $PathToFile as $FileRename


You should not be using $ here. $ converts from a string to a name. MoveFile is looking for a string and PathToFile is already a string, so no conversion is needed. For details on $, execute:
DisplayHelpTopic "Converting a String into a Reference Using $"

Thanks for the direction and help files.

Do you have any suggests for sorting the FileList by modified by date? The files in the folder are alphabetical, so the FileList is built alphabetically. But I need to sort chronologically by the last modified time stamp.

I am thinking something like SortList would be useful, but I need to figure out a way to sort by the last modified date/time timestamp.
Quote:
Do you have any suggests for sorting the FileList by modified by date? The files in the folder are alphabetical, so the FileList is built alphabetically. But I need to sort chronologically by the last modified time stamp.


This is a bit tricky. You need to create a text wave containing file names and a numeric wave containing modification dates for the corresponding file name. You can get the file names using IndexedFile and the modification dates using GetFileFolderInfo. Then you can use the Sort operation to sort both waves, using the modification date wave as the key.

However, it seems that the order is implied by the file name: asdfghjkl_001.LVM, asdfghjkl_002.LVM, asdfghjkl_003.LVM, ... If this is the case then you can get the list of file names in a string, using IndexedFile, and then sort the list using SortList.
I just figured it out as you responded. Sorry for taking up your time, but that is exactly what I did.

Thanks!