批处理脚本可将文本文件中列出的文件从文件夹及其子文件夹复制到新位置 [英] Batch script to copy files listed in text file from a folder and its subfolders to a new location

查看:194
本文介绍了批处理脚本可将文本文件中列出的文件从文件夹及其子文件夹复制到新位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个批处理文件,将文本文件中列出的所有文件从一个目标复制到另一个目标.源目标可能有多个子文件夹,我希望该批处理在每个子文件夹中搜索文件名.

I would like a batch file to copy all of the files listed in a text file from one destination to another. The source destination may have multiple subfolders and I would like the batch to search each subfolder for the file name.

我不想复制文件夹本身,而只复制文件.

I do not want copy the folders themselves, only the files.

我有以下代码,但无法识别(%file_list%)

I have the following code but it isn't recognizing the (%file_list%)

set src=c:\files\sourcefolder\
set dst=c:\files\destinationfolder\
set file_list=c:\\files\files.txt

for /r "%src%" %%i in (%file_list%) do copy "%%i" "%dst%" >notcopied.txt

我还希望该文件写入一个名为"notcopied.txt"的文本文件,以便可以查看源文件夹中是否没有任何必需的文件.我希望将该文件写入目标文件夹.

I also want the file to write a text file called 'notcopied.txt' so I can see if any required files were not in the source folder. I want that file written to the destination folder.

例如:

files.txt contains
File1.pdf
File2.pdf
File3.pdf

源文件夹包含

File1.pdf
File2.pdf

notcopied.txt随后将显示

notcopied.txt will then show

File3.pdf

感谢您的见解.

推荐答案

首先,您需要启用扩展的命令行

SETLOCAL ENABLEEXTENSIONS

,因此您可以使用扩展的 FOR 循环.其次,您需要启用环境变量的延迟扩展

so you can use the extended FOR loop. Second, you need to enable delayed expansion of environment variables

SETLOCAL ENABLEDELAYEDEXPANSION

,因此您可以检查是否在以后找到搜索到的文件.

so you can check if searched file was found later.

这是结果:

@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

SET src=.\sourcefolder\
SET dst=.\destinationfolder\
SET file_list=.\files.txt

FOR /F %%f IN (%file_list%) DO (
    SET found=false
    FOR /F "usebackq" %%s IN (`where /R %src% %%f`) DO (
        SET found=true
        echo "%%s => %dst%\%%f"
        @copy /y "%%s" "%dst%\%%f" 
    )
    IF "!found!" == "false" (
        echo %%f is not found!
    )
)

此处,变量%found%需要

ENABLEDELAYEDEXPANSION ,否则,变量%found%始终为false. 此外,要在运行时扩展环境,必须使用!found!语法.

ENABLEDELAYEDEXPANSION is needed here for variable %found%, without it %found% will always be false. Furthermore, to expand environment at runtime you must use !found! syntax.

您可以这样称呼

batch.bat > result.log

,结果将被写入文件result.log

更新:这是不带变量found的bacth版本,其中包含来自 @aschipfl 的建议:

UPDATE: here is a version of bacth without variable found and with suggestions from @aschipfl:

@echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

SET "src=.\sourcefolder\"
SET "dst=.\destinationfolder\"
SET "file_list=.\files.txt"

FOR /F "usebackq eol=| delims=" %%f IN ("%file_list%") DO (
    rem just searching to find out existense of file
    WHERE /Q /R "%src%" "%%f"
    IF "!ERRORLEVEL!" == "0" (
        FOR /F "usebackq eol=| delims=" %%s IN (`WHERE /R "%src%" "%%f" 2^> nul`) DO (
            echo "%%s => %dst%\%%f"
            @copy /y "%%s" "%dst%\%%f" 
        )
    ) ELSE (
        echo %%f is not found!
    )
)

此版本可以处理文件名中带有space符号的文件.
但是两种解决方案都有相同的限制:sourcefolder名称中不能包含任何空格.由于某些原因,命令WHERE /R "%src%" "%%f"不起作用,而WHERE /R %src% "%%f"却按预期工作.

This version can handle files with a space symbol in the filename.
But both of the solutions have same restriction: sourcefolder can't contain any spaces in the name. For some reason command WHERE /R "%src%" "%%f" doesn't working, while WHERE /R %src% "%%f" working just as expected.

UPDATE2 :这是产生result.log的版本:

@echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

SET "src=.\sourcefolder\"
SET "dst=.\destinationfolder\"
SET "file_list=.\files.txt"
SET "out=.\result.log"
echo > %out%

FOR /F "usebackq eol=| delims=" %%f IN ("%file_list%") DO (
    rem just searching to find out existense of file
    WHERE /Q /R %src% "%%f"
    IF "!ERRORLEVEL!" == "0" (
        FOR /F "usebackq eol=| delims=" %%s IN (`WHERE /R %src% "%%f"`) DO (
            echo "%%s => %dst%\%%f" >> %out%
            @copy /y "%%s" "%dst%\%%f" 
        )
    ) ELSE (
        echo %%f is not found! >> %out%
    )
)

这篇关于批处理脚本可将文本文件中列出的文件从文件夹及其子文件夹复制到新位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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