找到所有文件,并复制到另一个目标保持文件夹结构 [英] Find all files and copy to another destination keeping the folder structure

查看:410
本文介绍了找到所有文件,并复制到另一个目标保持文件夹结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆散落的文件夹 *。JPG 文件的文件夹结构。

现在我想找到(只有一列),或在一个CSV文件中列出的一些文件,由行的文本文件中的行像

  one.jpg
ten.jpg
five.jpg

和复制所有的 * JPG 文件到另一个文件夹保存的文件夹结构,如:

  outputfolder \\ somefolder \\ somefolder \\ one.jpgoutputfolder \\ somefolder \\ ten.jpgoutputfolder \\ somefolder \\ somefolder \\ somefolder \\ five.jpg

我怎样才能做到这一点?

下面是我试过

 关闭@echo
CLS
REM检查管理权限
COPY / B / Y NUL%WINDIR%\\ 06CF2EB6-94E6-4a60-91D8-AB945AE8CF38> NUL 2 - ;&放大器; 1
IF ERRORLEVEL 1 GOTO:非管理员
DEL%WINDIR%\\ 06CF2EB6-94E6-4a60-91D8-AB945AE8CF38> NUL 2 - ;&放大器; 1
:ADMIN
REM GOT管理员权限
颜色
1F ECHO请稍候...
FOR / R%〜DP0%% I IN(这一点)
FOR / F有usebackq delims =%%一中(%〜dp0list.txt)做
回声开发| XCOPY%% I \\ %%一个C:\\ B2B_output_files/ E / I
COLOR 2F
暂停
GOTO:EOF
:非管理员
REM NO管理员权限
COLOR 4F
暂停
GOTO:EOF


解决方案

Stack Overflow是不是免费的code写作服务,请参阅帮助主题的我这里问什么议题?

不过,我仍然写的整批code完成这个任务。从这个评论code和下一次学习尝试自行编写批处理code和只问如果你坚持的一个问题,你可以自己经过多次试验没有解决,而不是找对堆栈溢出或解决方案任何其他网站。

源和目标基站的文件夹的路径必须在低于批脚本的顶部被定义

包含文件的文件名由线到线复制文本文件必须命名为 FileNames.txt ,且必须存放在源代码库文件夹使用批处理$ C $下面C。

 关闭@echo
SETLOCAL ENABLEEXTENSIONS EnableDelayedExpansionREM定义源和目标的基础文件夹。REM注意:REM的路径不应包含等号则
REM字符串替换下面不会为$ C $裁谈会工作。该
REM目标基本文件夹可以是源连一个子文件夹
REM基本文件夹。设置SourceBaseFolder = C:\\ TEMP
设置TargetBaseFolder = C:\\ TEMP \\ OutputFolderREM SET源代码库文件夹作为当前目录。在previous
REM当前目录由命令ENDLOCAL月底恢复。如果不存在%SourceBaseFolder%\\ *(
    回声%〜NX0:没有文件夹%SourceBaseFolder%
    集的ErrorCount = 1
    转到HaltOnError
)CD / D%SourceBaseFolder%如果不存在FileNames.txt(
    回声%〜NX0:没有文件%SourceBaseFolder%\\ FileNames.txt
    集的ErrorCount = 1
    转到HaltOnError
)REM对于在文本文件中FileNames.txt每个文件名
下面REM源代码库文件夹中的循环做以下几点:REM 1.递归搜索与当前文件名的文件
REM在源代码库文件夹的整个目录树。REM 2,如果能找到一个文件,检查其路径。跳过
如果发现文件的路径中包含目标文件REM
REM文件夹路径,以避免将文件复制到自身。这个
REM若条件使得它有可能是目标基地
REM文件夹是源基文件夹的子文件夹。REM 3.创建文件相对于源中找到的文件夹
在目标基本文件夹物权的基本路径。然后检查,如果这
REM成功通过验证,如果目标文件夹
REM确实存在并将该文件复制现有的文件夹或
REM输出失败创建文件夹的错误消息。集的ErrorCount = 0
FOR / F有usebackq delims =%% N的(FileNames.txt),做(
    用于/ R %%的J(%% N *),做(
        设置文件路径= %%〜民主党
        如果文件路径:%!TargetBaseFolder%= ==!文件路径! (
            设置TARGETPATH​​ =%TargetBaseFolder%\\文件路径:%!SourceBaseFolder%\\ =!
            MD!TARGETPATH​​! 2 - ; NUL
            如果存在!TARGETPATH​​!\\ *(
                回声复制文件%%〜FJ
                复制/ Y%%〜FJ!TARGETPATH​​! > NUL
            )其他(
                集/ A的ErrorCount + = 1
                回声无法创建目录!TARGETPATH​​!
            )
        )
    )
):HaltOnError
如果%的ErrorCount%NEQ 0(
    回声。
    暂停

ENDLOCAL

有关理解使用的命令以及它们如何工作,打开命令提示符窗口中,执行有下面的命令,并阅读完全针对每个命令显示的所有帮助页面非常谨慎。


  • 呼叫/? ...为的解释%〜NX0

  • 复制/?

  • 回声/?

  • ENDLOCAL /?

  • 为/?

  • 转到/?

  • 如果/?

  • MD /?

  • 暂停/?

  • REM /?

  • 设置/?

  • SETLOCAL /?

和也了解 Microsoft文章使用命令重定向操作符以理解 2方式> NUL 的写入SUP pressing错误信息的 STDERR

I have a folder structure with a bunch of *.jpg files scattered across the folders.

Now I want to find some files listed in a CSV file (one column only) or a text file line by line like

one.jpg  
ten.jpg  
five.jpg

and copy all those *.jpg files to another folder keeping the folder structure like:

outputfolder\somefolder\somefolder\one.jpg

outputfolder\somefolder\ten.jpg

outputfolder\somefolder\somefolder\somefolder\five.jpg

How can I achieve this?

Here is what I've tried

@echo off 
CLS 
REM CHECK FOR ADMIN RIGHTS 
COPY /b/y NUL %WINDIR%\06CF2EB6-94E6-4a60-91D8-AB945AE8CF38 >NUL 2>&1 
IF ERRORLEVEL 1 GOTO:NONADMIN 
DEL %WINDIR%\06CF2EB6-94E6-4a60-91D8-AB945AE8CF38 >NUL 2>&1 
:ADMIN 
REM GOT ADMIN RIGHTS 
COLOR 
1F ECHO Please wait... 
FOR /R "%~dp0" %%I IN (.) DO 
for /f "usebackq delims=" %%a in ("%~dp0list.txt") do 
echo d |xcopy "%%I\%%a" "C:\B2B_output_files" /e /i 
COLOR 2F 
PAUSE 
GOTO:EOF 
:NONADMIN 
REM NO ADMIN RIGHTS 
COLOR 4F 
pause 
GOTO:EOF 

解决方案

Stack Overflow is not a free code writing service, see help topic What topics can I ask about here?

However, I have nevertheless written the entire batch code for this task. Learn from this commented code and next time try to write the batch code by yourself and ask only if you stick on a problem you can't solve by yourself after several trials and not finding a solution on Stack Overflow or any other website.

The paths of source and target base folder must be defined at top of the batch script below.

The text file containing the name of the files to copy line by line must be named FileNames.txt and must be stored in source base folder with using batch code below.

@echo off
setlocal EnableExtensions EnableDelayedExpansion

rem Define source and target base folders.

rem Note:

rem The paths should not contain an equal sign as then the
rem string substitutions below would not work as coded. The
rem target base folder can be even a subfolder of the source
rem base folder.

set "SourceBaseFolder=C:\Temp"
set "TargetBaseFolder=C:\Temp\OutputFolder"

rem Set source base folder as current directory. The previous
rem current directory is restored by command endlocal at end.

if not exist "%SourceBaseFolder%\*" (
    echo %~nx0: There is no folder %SourceBaseFolder%
    set "ErrorCount=1"
    goto HaltOnError
)

cd /D "%SourceBaseFolder%"

if not exist "FileNames.txt" (
    echo %~nx0: There is no file %SourceBaseFolder%\FileNames.txt
    set "ErrorCount=1"
    goto HaltOnError
)

rem For each file name in text file FileNames.txt in
rem source base folder the loops below do following:

rem 1. Search recursively for a file with current file name
rem    in entire directory tree of source base folder.

rem 2. If a file could be found, check its path. Skip the
rem    file if the path of found file contains the target
rem    folder path to avoid copying files to itself. This
rem    IF condition makes it possible that target base
rem    folder is a subfolder of source base folder.

rem 3. Create the folders of found file relative to source
rem    base path in target base folder. Then check if this
rem    was successful by verifying if the target folder
rem    really exists and copy the file on existing folder or
rem    output an error message on failure creating the folder.

set "ErrorCount=0"
for /F "usebackq delims=" %%N in ("FileNames.txt") do (
    for /R %%J in ("%%N*") do (
        set "FilePath=%%~dpJ"
        if "!FilePath:%TargetBaseFolder%=!" == "!FilePath!" (
            set "TargetPath=%TargetBaseFolder%\!FilePath:%SourceBaseFolder%\=!"
            md "!TargetPath!" 2>nul
            if exist "!TargetPath!\*" (
                echo Copying file %%~fJ
                copy /Y "%%~fJ" "!TargetPath!" >nul
            ) else (
                set /A ErrorCount+=1
                echo Failed to create directory !TargetPath!
            )
        )
    )
)

:HaltOnError
if %ErrorCount% NEQ 0 (
    echo.
    pause
)
endlocal

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /? ... for an explanation of %~nx0
  • copy /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • md /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?

And read also the Microsoft article about Using command redirection operators to understand 2>nul for suppressing error messages written to STDERR.

这篇关于找到所有文件,并复制到另一个目标保持文件夹结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆