复制文件夹,但不包括指定的子目录

时间:2013-03-24 01:15:28

标签: command-line scripting batch-file cmd dos

我需要有关复制文件夹的帮助,但不会复制具有文本文件中指定名称的子文件夹。文本文件位于:

  

U:\目录\目录\ TextFile.txt的

我需要复制文本文件中指定的文件夹,但这是

之后的catch

U:\Directory\Directory\文件夹具有随机名称,这就是它们存储在文本文件中的原因。目录树的示例:

  

U:\ Directory \ Directory \“12345”\ Pickle< --- Pickle是我想要的文件夹。

     

U:\目录\目录\ “22345” \味酸

                     ^ 
                     |
     

这是文本文件中指定的随机名称。

他们都有文件夹Pickle,这就是我所追求的。文本文件中包含以下所有文件夹的名称:U:\Directory\Directory\。文本文件如下所示:

  

1335232< ---这是随机文件夹的名称。

     

1242334< ---他们都位于:

     

2342312< --- U:\ Directory \ Directory \ ~HERE~

     

(等...)

应从U:\Directory\Directory\"12345"\Pickle to U:\Output\

复制文件夹

如果有帮助,所有文件夹的名称都是数字。谢谢Peter试图帮助我,如果我不清楚,我很抱歉。我希望这能解决问题!

1 个答案:

答案 0 :(得分:2)

@ECHO OFF
SETLOCAL
SET relroot=u:\directory
SET subdir=randomsubfoldername
::
FOR /f %%i IN (
  'dir /b /ad %relroot%\%subdir% ^|findstr /b /e /v /g:textfile.txt '
    ) DO ECHO %relroot%\%subdir%\%%i

DIR命令以基本形式(/ad)列出目录名(.b) - 即仅限名称。 findstr使用文件filename(/v)<中的行找到不(/b)开头(/e)和结束(/g:filename)的行/ p>

<小时/> 修改后的信息,并注意到原始信息清楚地显示倒数第二个目录名称是相同的,选择发生在叶子上,现在提供的单个示例......

@ECHO OFF
SETLOCAL
ECHO Here is a test structure
ECHO -----------------------------
DIR /s /b /ad u:\directory
ECHO ------Here is the textfile---------
TYPE u:\directory\textfile.txt
ECHO ====Method 1==============
FOR /f %%i IN (u:\directory\textfile.txt) DO (
DIR /s /b /ad u:\directory | FINDSTR /r ".*\\%%i\\.*" | FINDSTR /v /r ".*\\%%i\\.*\\.*"
)

ECHO ====Method 2==============
FOR /f %%i IN (u:\directory\textfile.txt) DO (
 FOR /f %%s IN (
  'DIR /s /b /ad u:\directory ^| FINDSTR /r ".*\\%%i\\.*" ^| FINDSTR /v /r ".*\\%%i\\.*\\.*"'
 ) DO ECHO selected : %%s
)


ECHO ====Method 3 - to ignore ...\target\subdir that has any subdir ==============
FOR /f %%i IN (u:\directory\textfile.txt) DO (
 FOR /f %%s IN (
  'DIR /s /b /ad u:\directory ^| FINDSTR /r ".*\\%%i\\.*" ^| FINDSTR /v /r ".*\\%%i\\.*\\.*"'
 ) DO (
 FOR /f %%c IN ( 'DIR /a:d %%s ^|FIND /c "<" ' ) DO IF %%c==2 ECHO SELECTED : %%s
 )
)

这是运行结果:

Here is a test structure
-----------------------------
u:\directory\another
u:\directory\yetanother
u:\directory\572
u:\directory\another\yetanother
u:\directory\another\yetanother\572
u:\directory\another\yetanother\1572
u:\directory\another\yetanother\5722
u:\directory\another\yetanother\572\wantthis
u:\directory\another\yetanother\572\andthis
u:\directory\another\yetanother\572\maywantthisidontknow
u:\directory\another\yetanother\572\572
u:\directory\another\yetanother\572\maywantthisidontknow\ignore
u:\directory\another\yetanother\1572\ignorethis
u:\directory\another\yetanother\5722\ignorethis
u:\directory\yetanother\572
u:\directory\yetanother\572\wantthis
u:\directory\572\wantthis
------Here is the textfile---------
23
753309
572
====Method 1==============
u:\directory\another\yetanother\572\wantthis
u:\directory\another\yetanother\572\andthis
u:\directory\another\yetanother\572\maywantthisidontknow
u:\directory\another\yetanother\572\572
u:\directory\yetanother\572\wantthis
u:\directory\572\wantthis
====Method 2==============
selected : u:\directory\another\yetanother\572\wantthis
selected : u:\directory\another\yetanother\572\andthis
selected : u:\directory\another\yetanother\572\maywantthisidontknow
selected : u:\directory\another\yetanother\572\572
selected : u:\directory\yetanother\572\wantthis
selected : u:\directory\572\wantthis
====Method 3 - to ignore ...\target\subdir that has any subdir ==============
SELECTED : u:\directory\another\yetanother\572\wantthis
SELECTED : u:\directory\another\yetanother\572\andthis
SELECTED : u:\directory\another\yetanother\572\572
SELECTED : u:\directory\yetanother\572\wantthis
SELECTED : u:\directory\572\wantthis

两个FINDSTR正则表达式结构

FINDSTR /r ".*\\%%i\\.*" 

任意数量的任何字符\,目标字符串\,任意数量的任何字符

FINDSTR /v /r ".*\\%%i\\.*\\.*"

任意数量的任何字符,\,目标字符串,\,任意数量的任何字符,\,任意数量的任何字符

但是,FINDSTR上的/v表示除了行匹配...


我对copy the sub-folder from a parent folder with a random name.

没什么意义

如果要求是从该目录的父目录复制到所选目录中,那么在验证ECHO SELECTED : %%s显示的目标目录是否用{/ 1>替换ECHO SELECTED : %%s之后

(
pushd %%s
xcopy ..\* . >nul
popd
)

>nul会抑制xcopy报告

如果它意味着其他内容,则需要更多信息。