Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
A convenience function for renaming files
ResourceFunction["FileNameEdit"][file] edits the base name of a file and optionally copies or moves it on disk. |
| "AddMissingExtension" | False | whether to add the proper extension if it is missing |
| "Append" | "" | a string which is appended to FileBaseName[file] |
| "Edit" | None | a string valued function which is applied to FileBaseName[file] |
| "Lowercase" | False | whether to force entire filenames to be lowercase |
| "LowercaseExtension" | False | whether to force extensions to be lowercase |
| "NewDirectory" | None | absolute directory (as a string) of the new filename |
| OverwriteTarget | False | specifies whether to overwrite an existing target file |
| "Action" | False | whether to perform a file system operation, either "Copy" (non-destructive) or "Move" (destructive) |
| "Prepend" | "" | a string which is prepended to FileBaseName[file] |
Easily alter the FileBaseName of a file:
| In[1]:= |
|
| Out[1]= |
|
Simultaneously append and prepend:
| In[2]:= |
|
| Out[2]= |
|
Apply arbitrary functions to base names of files:
| In[3]:= |
|
| Out[3]= |
|
Use a custom function and then lowercase all characters:
| In[4]:= |
|
| Out[4]= |
|
Use the "NewDirectory" option to copy or move files to a different directory:
| In[5]:= |
|
| Out[6]= |
|
which equivalent to this:
| In[7]:= |
|
| Out[9]= |
|
Files are not actually edited (copied or moved/renamed) on disk unless the option "Action" is specified:
| In[10]:= |
|
| In[11]:= |
|
| Out[11]= |
|
To non-destructively copy a file to one with new name, set the option "Action" to "Copy":
| In[12]:= |
|
| Out[12]= |
|
Setting the option "Action" to "Move" changes the file in-place:
| In[13]:= |
|
| In[14]:= |
|
| Out[14]= |
|
The option "AddMissingExtension" uses FileFormat to add an appropriate extension:
| In[15]:= |
|
| Out[16]= |
|
If a file with the new name already exists it will fail unless you set OverwriteTarget → True:
| In[17]:= |
|
| Out[17]= |
|
| In[18]:= |
|
| Out[18]= |
|
Use "LowercaseExtension" or "Lowercase" to lower-case the extension or entire filename, respectively:
| In[19]:= |
|
| Out[19]= |
|
| In[20]:= |
|
| Out[20]= |
|
| In[21]:= |
|
| Out[21]= |
|
Be careful when renaming files with option "Action"→"Move". In this case, FileNameEdit performs a CopyFile and DeleteFile in sequence:
| In[22]:= |
|
| Out[22]= |
|
| In[23]:= |
|
| Out[23]= |
|
This work is licensed under a Creative Commons Attribution 4.0 International License