在 Windows 上複製大量檔案但排除某些子資料夾

在 Windows 上複製大量檔案但排除某些子資料夾

我想備份我的工作資料夾,該資料夾主要包含在資料夾結構中,例如:

www
    - project 1
        - assets
        - node_modules
    - project 1
        - assets
        - node_modules
    - project 1
        - assets
        - node_modules

我需要複製所有項目,但排除每個項目中的 node_modules 資料夾。有沒有一種簡單的方法可以批量執行此操作?因為我有大量的項目需要完成。

在 Windows 8.1 上執行

答案1

我需要複製所有項目但排除 node_modules 資料夾

您可以xcopy與以下選項一起使用exclude

xcopy www backup /exclude:except.txt
  • except.txt包含要排除的目錄的檔案清單。

except.txt:

node_modules

/EXCLUDE:file1[+file2][+file3]...

  • 每個檔案都可以包含一個或多個要排除的完整或部分路徑名。
  • 當其中任何一個與 SOURCE 檔案的絕對路徑的任何部分相符時,該檔案將被排除。
  • 例如,指定類似\obj\或的字串.obj將分別排除目錄下的所有檔案obj或具有副檔名的所有檔案.obj

來源複製- 將檔案和/或目錄樹複製到另一個資料夾。


進一步閱讀

答案2

感謝@DavidPostill 的回答。這真是一種享受。不過,我也設法讓 robocopy 正常運作。對於那些可能有幫助的人來說,命令是:

robocopy SOURCE DEST /mir /xd node_modules

Source 是要複製的資料夾,dest 是目標。最後,我將 node_modules 作為我要排除的資料夾。

答案3

在裡面內容選單的 ”文件管理器「我加了一個powershell腳本這讓我能夠複製任何沒有 node_modules 的資料夾:

在此輸入影像描述

操作方法如下:

建立您的 PowerShell 腳本檔案script.ps1

$Source=$args[0]
$Position = $Source.lastindexofany("\")
$Destination = $Source.substring(0, $Position + 1)
$FolderName = $Source.substring($Position + 1)
$Destination = "$($Destination)$($FolderName)-DUPLICATE_WITHOUT_NODE_MODULES"
robocopy $Source $Destination /mir /xd node_modules

建立一個註冊表檔案:(例如Add_Script_To_ContextMenu.reg:)

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\DupFolderWithoutNodeModules]
@="Duplicate folder without node_modules"

[HKEY_CLASSES_ROOT\Directory\shell\DupFolderWithoutNodeModules\command]
@="C:\\\\Windows\\\\system32\\\\WindowsPowerShell\\\\v1.0\\\\powershell.exe -File \"C:\\Path of_your_script\\script.ps1\" \"%L\""

換成C:\\Path of_your_script你的。不要忘記\加倍\\

然後,雙擊 .reg 檔案!

一切都完成了。

相關內容