README.md (5969B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Rename 22 23 > Rename a file. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var rename = require( '@stdlib/fs/rename' ); 31 ``` 32 33 #### rename( oldPath, newPath, clbk ) 34 35 Asynchronously renames a file specified by `oldPath` to `newPath`. 36 37 <!-- run-disable --> 38 39 ```javascript 40 var join = require( 'path' ).join; 41 var oldPath = join( __dirname, 'examples', 'fixtures', 'file.txt' ); 42 var newPath = join( __dirname, 'examples', 'fixtures', 'tmp.txt' ); 43 44 rename( oldPath, newPath, done ); 45 46 function done( error ) { 47 if ( error ) { 48 throw error; 49 } 50 } 51 ``` 52 53 #### rename.sync( oldPath, newPath ) 54 55 Synchronously renames a file specified by `oldPath` to `newPath`. 56 57 <!-- run-disable --> 58 59 ```javascript 60 var join = require( 'path' ).join; 61 var oldPath = join( __dirname, 'examples', 'fixtures', 'file.txt' ); 62 var newPath = join( __dirname, 'examples', 'fixtures', 'tmp.txt' ); 63 64 var err = rename.sync( oldPath, newPath ); 65 if ( err instanceof Error ) { 66 throw err; 67 } 68 ``` 69 70 </section> 71 72 <!-- /.usage --> 73 74 <section class="notes"> 75 76 ## Notes 77 78 - `oldPath` can specify a directory. In this case, `newPath` must either **not** exist, or it must specify an **empty** directory. 79 80 - `oldPath` should **not** name an ancestor directory of `newPath`. 81 82 - If `oldPath` points to the pathname of a file that is **not** a directory, `newPath` should **not** point to the pathname of a directory. 83 84 - Write access permission is **required** for both the directory containing `oldPath` and the directory containing `newPath`. 85 86 - If the link named by `newPath` exists, `newPath` is removed and `oldPath` is renamed to `newPath`. The link named by `newPath` will remain visible to other threads throughout the renaming operation and refer to either the file referred to by `newPath` or to the file referred to by `oldPath` before the operation began. 87 88 - If `oldPath` and `newPath` resolve to either the same existing directory entry or to different directory entries for the same existing file, no action is taken, and no error is returned. 89 90 - If `oldPath` points to a pathname of a symbolic link, the symbolic link is renamed. If the `newPath` points to a pathname of a symbolic link, the symbolic link is removed. 91 92 - If a link named by `newPath` exists and the file's link count becomes `0` when it is removed and no process has the file open, the space occupied by the file is freed and the file is no longer accessible. If one or more processes have the file open when the last link is removed, the link is removed before the function returns, but the removal of file contents is postponed until all references to the file are closed. 93 94 - The difference between `rename.sync` and [`fs.rename()`][node-fs] is that [`fs.renameSync()`][node-fs] will throw if an `error` is encountered (e.g., if given a non-existent path) and this API will return an `error`. Hence, the following anti-pattern 95 96 <!-- run-disable --> 97 98 ```javascript 99 var fs = require( 'fs' ); 100 101 // Check for path existence to prevent an error being thrown... 102 if ( fs.existsSync( '/path/to/file.txt' ) ) { 103 fs.renameSync( '/path/to/file.txt', '/path/to/tmp.txt' ); 104 } 105 ``` 106 107 can be replaced by an approach which addresses existence via `error` handling. 108 109 <!-- run-disable --> 110 111 ```javascript 112 var rename = require( '@stdlib/fs/rename' ); 113 114 // Explicitly handle the error... 115 var err = rename.sync( '/path/to/file.txt', '/path/to/tmp.txt' ); 116 if ( err instanceof Error ) { 117 // You choose what to do... 118 throw err; 119 } 120 ``` 121 122 </section> 123 124 <!-- /.notes --> 125 126 <section class="examples"> 127 128 ## Examples 129 130 <!-- eslint no-undef: "error" --> 131 132 ```javascript 133 var join = require( 'path' ).join; 134 var readFile = require( '@stdlib/fs/read-file' ).sync; 135 var writeFile = require( '@stdlib/fs/write-file' ).sync; 136 var exists = require( '@stdlib/fs/exists' ).sync; 137 var unlink = require( '@stdlib/fs/unlink' ).sync; 138 var rename = require( '@stdlib/fs/rename' ).sync; 139 140 var src = join( __dirname, 'examples', 'fixtures', 'file.txt' ); 141 var tmp = join( __dirname, 'examples', 'tmp.txt' ); 142 var dest = join( __dirname, 'examples', 'foo.txt' ); 143 144 // Create a temporary file: 145 writeFile( tmp, readFile( src ) ); 146 147 // Confirm that the temporary file exists: 148 console.log( exists( tmp ) ); 149 // => true 150 151 // Rename the temporary file: 152 rename( tmp, dest ); 153 154 // Confirm that the renamed temporary file exists: 155 console.log( exists( dest ) ); 156 // => true 157 158 // Remove the temporary file: 159 unlink( dest ); 160 161 // Confirm that the temporary file no longer exists: 162 console.log( exists( dest ) ); 163 // => false 164 ``` 165 166 </section> 167 168 <!-- /.examples --> 169 170 * * * 171 172 <section class="cli"> 173 174 ## CLI 175 176 <section class="usage"> 177 178 ### Usage 179 180 ```text 181 Usage: rename [options] <old_path> <new_path> 182 183 Options: 184 185 -h, --help Print this message. 186 -V, --version Print the package version. 187 ``` 188 189 </section> 190 191 <!-- /.usage --> 192 193 <section class="notes"> 194 195 ### Notes 196 197 - Relative paths are resolved relative to the current working directory. 198 - Errors are written to `stderr`. 199 200 </section> 201 202 <!-- /.notes --> 203 204 <section class="examples"> 205 206 ### Examples 207 208 <!-- run-disable --> 209 210 ```bash 211 $ rename ./examples/fixtures/file.txt ./examples/fixtures/tmp.txt 212 ``` 213 214 </section> 215 216 <!-- /.examples --> 217 218 </section> 219 220 <!-- /.cli --> 221 222 <section class="links"> 223 224 [node-fs]: https://nodejs.org/api/fs.html 225 226 </section> 227 228 <!-- /.links -->