Nodejs Course

Moving File to other directory

To move a file from a directory to another in Node.js, you can use the fs.rename(oldPath, newPath, callback) method.
Here is a moveFile() function that can be used to move a file to other directory.
- Example: move file1.htm from 'test/' to 'test/dir_1/' folder:
//moves the $file to $dir2
var moveFile = (file, dir2)=>{
  //include the fs, path modules
  var fs = require('fs');
  var path = require('path');

  //gets file name and adds it to dir2
  var f = path.basename(file);
  var dest = path.resolve(dir2, f);

  fs.rename(file, dest, (err)=>{
    if(err) throw err;
    else console.log('Successfully moved');
  });
};

//move file1.htm from 'test/' to 'test/dir_1/'
moveFile('./test/file1.htm', './test/dir_1/');

Copy file

To copy a file from one directory to another in Node.js, you can use the copyFile() function from this example:
//copy the $file to $dir2
var copyFile = (file, dir2)=>{
  //include the fs, path modules
  var fs = require('fs');
  var path = require('path');

  //gets file name and adds it to dir2
  var f = path.basename(file);
  var source = fs.createReadStream(file);
  var dest = fs.createWriteStream(path.resolve(dir2, f));

  source.pipe(dest);
  source.on('end', function() { console.log('Succesfully copied'); });
  source.on('error', function(err) { console.log(err); });
};

//example, copy file1.htm from 'test/dir_1/' to 'test/'
copyFile('./test/dir_1/file1.htm', './test/');

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag renders as emphasized text, displaying the text oblique?
<strong> <pre> <em>
<p>Web development courses: <em>CoursesWeb.net</em></p>
Which CSS property defines the space between the element border and its content?
margin padding position
h3 {
  padding: 2px 0.2em;
}
Click on the method which returns the first element that matches a specified group of selectors.
getElementsByName() querySelector() querySelectorAll()
// gets first Div with class="cls", and shows its content
var elm = document.querySelector("div.cls");
alert(elm.innerHTML);
Indicate the PHP variable that contains data from a form sent with method="post".
$_SESSION $_GET $_POST
if(isset($_POST["field"])) {
  echo $_POST["field"];
}
Node.js Move and Copy file

Last accessed pages

  1. JavaScript base64 encode decode (5827)
  2. The School for Gods (5771)
  3. 101 Zen stories (2003)
  4. Multiple Select Dropdown List with JavaScript (13464)
  5. Convert BBCode to HTML and HTML to BBCode with JavaScript (9273)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (240)
  2. Read Excel file data in PHP - PhpExcelReader (83)
  3. The Four Agreements (72)
  4. PHP Unzipper - Extract Zip, Rar Archives (72)
  5. The Mastery of Love (60)