Renaming external files
aabeleira
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
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
April 16, 2015 at 08:59 pm - Permalink
I don't think you need to delete the original as MoveFile does not create a copy but rather moves the original file.
April 16, 2015 at 09:09 pm - Permalink
GetFileFolderInfo/D("")
NewPath FolderPath S_Path
//Want to convert FolderPath to a string for later use
end
April 17, 2015 at 09:27 am - Permalink
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:
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.
April 17, 2015 at 09:54 am - Permalink
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!
April 17, 2015 at 10:12 am - Permalink
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.
April 17, 2015 at 10:17 am - Permalink
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.
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
April 17, 2015 at 10:35 am - Permalink
April 17, 2015 at 10:58 am - Permalink
Please read the documentation for 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.
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:
April 17, 2015 at 11:04 am - Permalink
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.
April 17, 2015 at 11:15 am - Permalink
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.
April 17, 2015 at 12:57 pm - Permalink
Thanks!
April 17, 2015 at 01:03 pm - Permalink