Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove.js
More file actions
Latest commit
45 lines (37 loc) · 1.51 KB
/
Copy pathmove.js
File metadata and controls
45 lines (37 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
constfs=require("fs");
constpathModule=require("path");
functionmoveFiles(sourcePath,destinationPath){
constitems=fs.readdirSync(sourcePath);
if(!fs.existsSync(destinationPath)){
fs.mkdirSync(destinationPath,{recursive: true});
}
for(constitemofitems){
constsrcPath=pathModule.join(sourcePath,item);
constdestPath=pathModule.join(destinationPath,item);
if(fs.existsSync(srcPath)){
try{
conststats=fs.lstatSync(srcPath);
if(stats.isDirectory()){
fs.cpSync(srcPath,destPath,{recursive: true});
}else{
constdestDir=pathModule.dirname(destPath);
if(!fs.existsSync(destDir)){
fs.mkdirSync(destDir,{recursive: true});
}
fs.copyFileSync(srcPath,destPath);
}
fs.rmSync(srcPath,{recursive: true,force: true});
}catch(err){
console.error(`Error moving file or folder: ${srcPath} -> ${destPath}`,err.message);
}
}else{
console.warn(`Warning: Source file or directory does not exist: ${srcPath}`);
}
}
try{
fs.rmSync(sourcePath,{recursive: true,force: true});
}catch(err){
console.error("Error removing source folder:",err.message);
}
}
module.exports={ moveFiles };