0

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

U:\目录\目录\文本文件.txt

我需要复制文本文件中指定的文件夹,但这里有一个问题,之后

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

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

U:\目录\目录\"22345"\Pickle

                     ^ 
                     |

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

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

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

1242334 <--- 它们都位于:

2342312 <--- U:\Directory\Directory\~这里~

(ETC...)

文件夹应该从U:\Directory\Directory\"12345"\Pickle to U:\Output\

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

4

1 回答 1

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.bfindstr/v/b/e/g:filename


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

@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\\.*\\.*"

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

但是 - /vFINDSTR 上的意思是除了匹配的行...


我无法理解copy the sub-folder from a parent folder with a random name.

ECHO SELECTED : %%s如果要求从该目录的父目录复制到所选目录,则在通过替换验证目标目录显示ECHO SELECTED : %%s

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

>nul抑制 xcopy报告

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

于 2013-03-24T06:51:35.253 回答