node.js 代理文件夹
I had a problem.
我有一个问题。
I was creating a large number of folders formatted like this:
我正在创建大量格式如下的文件夹:
1-yo
2-hey
3-cool
4-hi
5-whatsup
A number followed by a dash, and a string.
一个数字,后跟一个破折号和一个字符串。
I got up to 40 of these, and I realized I had to put one in the middle, like this:
我最多有40个这样的东西,我意识到我必须在中间放一个,就像这样:
1-yo
2-hey
3-NEWONE
3-cool
4-hi
5-whatsup
The problem was that I had to change all the numbers of the folders that now were supposed to follow the 3-NEWONE
folder.
问题是我必须更改现在应该跟随3-NEWONE
文件夹3-NEWONE
文件夹编号。
I wanted an end result like this, where all the numbers following the new entry are incremented:
我想要这样的最终结果,其中新条目后面的所有数字均递增:
1-yo
2-hey
3-NEWONE
4-cool
5-hi
6-whatsup
I did this manually once, then I realized I was definitely going to repeat this process in the future, so I made a Node.js command line app to automatize it.
我曾经手动执行过一次,然后意识到以后肯定会重复此过程,所以我制作了一个Node.js命令行应用程序来使其自动化。
I called the file increment.js
and I decided to take a command line argument to set the number I wanted to start from, like this:
我调用了文件increment.js
然后决定采用命令行参数来设置要从其开始的数字,如下所示:
node rename.js 4
Getting the number is simple, we get it from process.argv
:
获取数字很简单,我们从process.argv
获取它:
const args = process.argv.slice(2)
const startingNumber = args[0]
If no number is present, we’re going to show an error and end the program:
如果没有数字,我们将显示一个错误并结束程序:
if (!startingNumber) {
console.log('Add a number argument')
return
}
Now that we have this number, we can start getting the folder names we need to increment. The script will be positioned in the same folder that contains all the subfolders, so we can just read from ./
, which means this folder.
现在有了这个数字,我们可以开始获取需要增加的文件夹名称了。 该脚本将位于包含所有子文件夹的同一文件夹中,因此我们只需从./
读取,即该文件夹 。
This is how we can get the names of all the files and subfolders contained in the current folder:
这是我们如何获取当前文件夹中包含的所有文件和子文件夹的名称:
const fs = require('fs')
const folders = fs
.readdirSync('./')
.map(fileName => {
return fileName
})
Let’s filter this to make sure we only get the folders:
让我们对此进行过滤,以确保仅获取文件夹:
const fs = require('fs')
const isFolder = fileName => {
return !fs.lstatSync(fileName).isFile()
}
const folders = fs
.readdirSync('./')
.map(fileName => {
return fileName
})
.filter(isFolder)
Next we can iterate through the folders list:
接下来,我们可以遍历文件夹列表:
folders.map(folder => {
})
I extract the number from the folder:
我从文件夹中提取号码:
folders.map(folder => {
const result = folder.match(/(\d)+/)
})
And if there’s a match (the folder has a number in the name), I extract it and transform it from a string into a number:
如果匹配(文件夹中的名称中有一个数字),我将其提取并将其从字符串转换为数字:
folders.map(folder => {
const result = folder.match(/(\d)+/)
if (result !== null) {
const num = parseInt(result[0])
}
})
Finally, if the number is higher than the one we pass as argument, we rename the folder name incrementing the number:
最后,如果该数字大于我们作为参数传递的数字,则将文件夹名称重命名,使数字递增:
folders.map(folder => {
const result = folder.match(/(\d)+/)
if (result !== null) {
const num = parseInt(result[0])
if (num >= startingNumber) {
fs.renameSync(folder, folder.split(num).join(num + 1))
}
}
})
That’s it! Here’s the final source code of our little CLI app:
而已! 这是我们的小型CLI应用程序的最终源代码:
const fs = require('fs')
const args = process.argv.slice(2)
const startingNumber = args[0]
if (!startingNumber) {
console.log('Add a number argument')
return
}
const isFolder = fileName => {
return !fs.lstatSync(fileName).isFile()
}
const folders = fs
.readdirSync('./')
.map(fileName => {
return fileName
})
.filter(isFolder)
folders.map(folder => {
const result = folder.match(/(\d)+/)
if (result !== null) {
const num = parseInt(result[0])
if (num >= startingNumber) {
fs.renameSync(folder, folder.split(num).join(num + 1))
}
}
})
node.js 代理文件夹