簡體   English   中英

如何避免使用 fs-extra.copySync 方法復制 .txt 和 .xml 文件類型

[英]how to avoid copying .txt and .xml file types using fs-extra.copySync method

我正在 typescript 做一個小任務,目前我正面臨下面提到的問題,我無法找到實現這個問題的方法。任何關於下面提到的問題的指導或建議將不勝感激。

我有一個小任務要將目錄復制到文件系統中的另一個位置,所以目前我在 npm package 中使用 fs-extra.copySync 方法,將目錄從一個位置復制到另一個位置,但是當我復制文件時需要排除一些文件類型(.xml、.txt)。

所以問題出在我要復制的目錄中,如果有子文件夾,則不允許的文件類型 (.xml &.txt) 也會被復制。 所以我嘗試了以下方法,但它給出了以下錯誤。

無法讀取 null 的屬性“readFiles”

下面提到了我嘗試的方法。

服務器.ts

function moveFilesAll()
{
   var moveFrom = "./src";
   var moveTo = "./Destination";

   readFiles(moveFrom,moveTo);
} 

    function readFiles(moveFrom,moveTo)
    {
      fs.readdir(moveFrom, function (err, files) {
        if (err) {
          console.error("Could not list the directory.", err);
          process.exit(1);
        }


      files.forEach(function (file, index) {

        console.log(file + " "+ index)
        // Make one pass and make the file complete
        var fromPath = path.join(moveFrom, file);
        var toPath = path.join(moveTo, file);

        fs.stat(fromPath, function (error, stat) {
          if (error) {
            console.error("Error stating file.", error);
            return;
          }

          if (stat.isFile())
          {

            console.log("'%s' is a file.", fromPath);
            console.log(path.extname(fromPath));
             //files get copying here
          if(path.extname(fromPath) =='.txt' || path.extname(fromPath) == '.xml' ||  path.extname(fromPath) == '.config'){
            console.log("Unallowed file types");
          }
          else
          {
            console.log("---------------Files copying--------------------------");
                fsExtra.copySync(fromPath, toPath);
                console.log("copied from '%s' to '%s'. ", fromPath, toPath); 
          }




          }
            else if (stat.isDirectory())
          {
            console.log("=================Directory=============");
            console.log("From path "+fromPath);
            console.log("TO path "+toPath);
            readFiles(fromPath,toPath);
            console.log("'%s' is a directory.", fromPath);
          } 
        });

    })
      })
    }

您可以使用具有 glob-pattern 支持的庫,例如copy

npm i --save copy
npm i --save-dev @types/copy

然后像這樣使用它。

// Creates the glob pattern for exluded files, result looks like this: '*.xml|*.txt'
const excludedFilesGlob:Array<string> = ['xml', 'txt']
   .map(ft => `*.${ft}`)
   .join('|');

// The source glob, include all files except for the excluded file types
const source:string = `./src/**/!(${exludedFileTypes})`;
const destination:string = './Destination';

copy(source, destination, (err, files) => {
  // This callback is called when either an error occured or all files were copied successfully
  if (err) throw err;
  // `files` is an array of the files that were copied
});

const source = '/path/to/source/dir'
const destination = '/path/to/destination/dir'

const filter = file => {
  // get the file extension
  const ext = path.extname(file)
  
  // return true if the file is not a .txt or .xml file
  return ext !== '.txt' && ext !== '.xml'
}

fs.copySync(source, destination, { filter })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM