批处理文件将文件夹从文本文件复制到另一个目录

时间:2013-05-20 23:34:33

标签: windows batch-file cmd

我正在尝试创建批处理文件以读取文本文件并将包含这些文件的文件夹复制到另一个目录:

set dest=f:\Test
for /f %%i in (C:\dirs.txt) do copy "%%i" %dest%

运行此批处理文件时,只复制文件。我想用文件复制文件夹。

IE folder \ files到destination \ folder \ files。

4 个答案:

答案 0 :(得分:2)

查看xcopy

xcopy /?

如果文本文件包含文件名,请尝试:

xcopy "%%i" %dest%\ /S

如果文本文件仅包含目录名称,请尝试:

xcopy "%%i\*" %dest%\ /S

答案 1 :(得分:0)

您拥有Vista或更高版本吗?然后给robocopy一个镜头:robocopy "source" "destination" /MIR。其他一些选择:

             Usage :: ROBOCOPY source destination [file [file]...] [options]

             source :: Source Directory (drive:\path or \\server\share\path).
        destination :: Destination Dir  (drive:\path or \\server\share\path).
               file :: File(s) to copy  (names/wildcards: default is "*.*").

::
:: Copy options :
::
                 /S :: copy Subdirectories, but not empty ones.
                 /E :: copy subdirectories, including Empty ones.
             /LEV:n :: only copy the top n LEVels of the source directory tree.

                 /Z :: copy files in restartable mode.
                 /B :: copy files in Backup mode.
                /ZB :: use restartable mode; if access denied use Backup mode.

  /COPY:copyflag[s] :: what to COPY (default is /COPY:DAT).
                       (copyflags : D=Data, A=Attributes, T=Timestamps).
                       (S=Security=NTFS ACLs, O=Owner info, U=aUditing info).

               /SEC :: copy files with SECurity (equivalent to /COPY:DATS).
           /COPYALL :: COPY ALL file info (equivalent to /COPY:DATSOU).
            /NOCOPY :: COPY NO file info (useful with /PURGE).

             /PURGE :: delete dest files/dirs that no longer exist in source.
               /MIR :: MIRror a directory tree (equivalent to /E plus /PURGE).

               /MOV :: MOVe files (delete from source after copying).
              /MOVE :: MOVE files AND dirs (delete from source after copying).

       /A+:[RASHNT] :: add the given Attributes to copied files.
       /A-:[RASHNT] :: remove the given Attributes from copied files.

            /CREATE :: CREATE directory tree and zero-length files only.
               /FAT :: create destination files using 8.3 FAT file names only.
               /FFT :: assume FAT File Times (2-second granularity).
               /256 :: turn off very long path (> 256 characters) support.

             /MON:n :: MONitor source; run again when more than n changes seen.
             /MOT:m :: MOnitor source; run again in m minutes Time, if changed.

      /RH:hhmm-hhmm :: Run Hours - times when new copies may be started.
                /PF :: check run hours on a Per File (not per pass) basis.

             /IPG:n :: Inter-Packet Gap (ms), to free bandwidth on slow lines.


Thera是更有趣的选择。

答案 2 :(得分:0)

在搞乱文件之前你应该把它测试出IE。

    @echo off
    CD drive path of parent folder
    mkdir A
    CD ...\A 
    echo hello>1.txt
    echo hello2>2.txt
    mkdir A2
    xcopy A "drive path"
    start explorer.exe "path to A2"

这将使基本文件夹设置为测试它。 之后,如果它工作,将它应用到您的文件路径。

答案 3 :(得分:0)

上述解决方案似乎将目录内容复制到目标的根目录。并且对于包含空格的文件夹有问题。 尝试以下几行解决...

set dest=e:\Test
for /f "delims=" %%i in (folders.txt) do xcopy "%%i\*" "%dest%\%%i\*" /E /F /Y

/E also copies empty subdirectories
/F lists the full filenames to copies (for debugging purposes)
/Y answers questions with yes.

将不会使用此命令创建不存在的目录,它将覆盖目标中存在的文件。

delims = part确保从文本文件中读取的目录可以包含空格