簡體   English   中英

Bat文件復制文件夾和文件

[英]Bat file to copy Folders and Files

我有一個文本文件( %File_list% )中的文件夾和文件列表。 我的批次從文本文件中讀取要復制的對象。 我需要它將文件和文件夾從文件夾( %src_folder% )中的一個復制到另一個文件夾( %dst_folder% )。

我當前的嘗試復制文件,但不復制文件夾。

批處理腳本:

@echo off
set src_folder=C:\Users\P57949\Desktop
set dst_folder=C:\Users\P57949\Desktop\NewTestFolder
set file_list=C:\Users\P57949\Desktop\Filelist.txt

if not exist "%dst_folder%" mkdir "%dst_folder%"
for /f "delims=" %%f in (%File_list%) do (
    xcopy /y /s "%src_folder%\%%f" "%dst_folder%\"
)

文本文件:

Fixed Retail.xlsx
SecureFolder

有人可以告訴我我做錯了什么嗎? 如何復制文件夾和文件?

當源是目錄時, xcopy命令復制其內容而不是整個目錄。 要復制整個目錄,您需要相應地更改目標:

xcopy /I /Y /S "%src_folder%\%%f" "%dst_folder%\%%f"

注意/I選項,它告訴xcopy目標不是文件而是目錄。

但是,當源是單個文件時,當目標尚不存在時, xcopy會提示您目標是文件還是目錄,即使提供了/I也是如此(請參閱此相關帖子)。 因此,我們必須提前確定來源( %%f )是什么並做出充分反應:

@echo off
rem // The quoted `set` syntax protects special characters:
set "src_folder=C:\Users\P57949\Desktop"
set "dst_folder=C:\Users\P57949\Desktop\NewTestFolder"
set "file_list=C:\Users\P57949\Desktop\Filelist.txt"

rem // Append `\*` to check a directory but not a file for existence:
if not exist "%dst_folder%\*" mkdir "%dst_folder%"
rem // Use `usebackq` option and quotation to permit spaces in file path:
for /F "usebackq delims=" %%f in ("%File_list%") do (
    rem // Check whether source is a directory:
    if exist "%src_folder%\%%f\*" (
        rem // Source is a directory, so copy it entirely:
        xcopy /I /Y /S "%src_folder%\%%f" "%dst_folder%\%%f"
    ) else (
        rem // Source is a file, or it does not exist:
        xcopy /Y /S "%src_folder%\%%f" "%dst_folder%\"
    )
)

預先創建目標文件時,可以實現更簡單和更緊湊的方法,因此不會出現文件/目錄提示:

@echo off
rem // The quoted `set` syntax protects special characters:
set "src_folder=C:\Users\P57949\Desktop"
set "dst_folder=C:\Users\P57949\Desktop\NewTestFolder"
set "file_list=C:\Users\P57949\Desktop\Filelist.txt"

rem // Append `\*` to check a directory but not a file for existence:
if not exist "%dst_folder%\*" mkdir "%dst_folder%"
rem // Use `usebackq` option and quotation to permit spaces in file path:
for /F "usebackq delims=" %%f in ("%File_list%") do (
    rem // Pre-create destination file when source is a file:
    if not exist "%src_folder%\%%f\*" > "%dst_folder%\%%f" rem/
    rem // No prompt appears even when source is a file:
    xcopy /I /Y /S "%src_folder%\%%f" "%dst_folder%\%%f"
)

最簡單的方法是使用/I選項來覆蓋源是目錄的情況,並在源是文件的情況下自動填充文件/目錄提示,但這取決於語言環境(例如, F和目錄用於英語系統,但DV D用於德語系統),所以要小心:

@echo off
rem // The quoted `set` syntax protects special characters:
set "src_folder=C:\Users\P57949\Desktop"
set "dst_folder=C:\Users\P57949\Desktop\NewTestFolder"
set "file_list=C:\Users\P57949\Desktop\Filelist.txt"

rem // Append `\*` to check a directory but not a file for existence:
if not exist "%dst_folder%\*" mkdir "%dst_folder%"
rem // Use `usebackq` option and quotation to permit spaces in file path:
for /F "usebackq delims=" %%f in ("%File_list%") do (
    rem // The `/I` option copes for directories, `echo F` for files:
    rem echo F| xcopy /I /Y /S "%src_folder%\%%f" "%dst_folder%\%%f"
)

注意:
對於以上所有內容,我假設列表文件Filelist.txt包含純文件/目錄名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM