file rename ...

Prev Next

Function Names

file rename, file rename all, file rename silently

Description

These functions rename files or move files from one location to another and come in following flavors:
file rename will assert an exception if the file cannot be renamed or moved (e.g. file not found, file locked, access restrictions, etc.) Execution will stop immediately.
file rename silently will rename the files until a first failure is encountered. In this case, the applicable one of the short error messages on directories and files will be returned. If no errors have been encountered, "OK" will be returned. file rename all tries to rename or move all files and skips the ones which cannot be renamed or moved. Here, the number of successfully renamed or moved files will be returned.


Moving files is only possible inside the same disk volume (i.e. same disk drive / desktop computer / server drive)

Call as: procedure or function

Restrictions

Indirect parameter passing is disabled.

Parameter count

2

Parameters

No.TypeDescription
1
input
set or string existing path and file names

Every parameter must refer to an existing file. Path name in front is needed if the file is not located in the working directory.

2
input
set or string new path and file names

Destination paths and new file names. The number of existing and new path and file names must match, otherwise an exception will be asserted.

Return value

TypeDescription
numeral or string Feedback

file rename: Always "OK", because all errors cause exceptions.
file rename silently: "OK", or short error messages on directories and files.
file rename all: Number of successful operations (files renamed or moved) is returned.

Exceptions

File not found
File locked by another application
File not accessible
File is a directory
Number of existing and new path and file names are not equal

Examples

               directory create( My Folder 1, My Folder 2 );

               file copy( Example_Program.b4p, "My Folder 1/Test123.txt" );
               file rename( My Folder 1\Test123.txt, My Folder 2\Test456.txt ); // Move and rename

               if (system info[operating system]=Windows)
                   system( 'dir /A-D /W "My Folder 2"' ); // Show directory listing
               else:
                       system( "ls 'My Folder 2'" ); // Show directory listing


               directory delete recursive( "", "My Folder ?" ); // Delete "My Folder 1" and "... 2".

Output

 Datenträger in Laufwerk C: ist Windows
Volumeseriennummer: 96E3-BA76

Verzeichnis von C:\Users\zur-b\OneDrive\Documents\Programme\Beyond4P\B4P_Docu_Maker\My Folder 2

Test456.txt   
               1 Datei(en),            598 Bytes
               0 Verzeichnis(se), 58’229’547’008 Bytes frei
Try it yourself: Open LIB_Function_file_rename.b4p in B4P_Examples.zip. Decompress before use.

See also

directory rename ... Functions
file copy ... Functions