首页  编辑  

根据目录名关键字,扫描某个目录下关键字名字的目录,并复制整个目录到另外的目标目录

Tags: /计算机文档/脚本,批处理/   Date Created:
How to scan a folder, find all folder by key name list and copy that folder to another location?

根据目录名关键字,扫描某个目录下关键字名字的目录,并复制整个目录到另外的目标目录。
代码如下,必须同时存在list.txt文件,其中每一行为一个目录名关键字
cp_folder_by_name.bat
-----------------------------------------
@echo off
setlocal enabledelayedexpansion

if "%2"=="" (
  echo Copy directory by name 1.0
  echo Copyright 电脑义工, 2016
  echo ------------------------
  echo Read path key names from list.txt, and scan SourceDir, if the folder name
  echo match key name then copy matched folder to DestDir
  echo.
  echo Usage: %~n0 SourceDir DestDir
  echo PLEASE save the path key names into list.txt at the same folder first
  echo Each path name per line.
  goto end
)

for /f %%i in ('dir %1 /ad /s /b') do (
  echo Scan folder %%i...
  for /f "delims=*" %%j in (list.txt) do (
    echo "%%i" | findstr /r "%%j[^\\]" > nul
    if !errorlevel! == 0 (
      echo Process %%i
      xcopy /s /e /y /k /h /i %%i "%2\%%j"
    )
  )
)

:end
----------------------------------------