0

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

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

当我运行这个批处理文件时,只复制文件。我想复制带有文件的文件夹。

IE 文件夹\文件到目标\文件夹\文件。

4

4 回答 4

2

看看xcopy

xcopy /?

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

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

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

xcopy "%%i\*" %dest%\ /S
于 2013-05-20T23:39:47.693 回答
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"

这将建立一个基本的文件夹设置来测试它。之后,如果它有效,请将其应用于您的文件路径。

于 2013-10-28T19:48:50.207 回答
0

Do you own Vista or later? Then give robocopy a shot: robocopy "source" "destination" /MIR. Some other options:

             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 are more interesting options.

于 2013-05-21T00:35:39.067 回答
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= 部分确保从文本文件读取的目录可以包含空格

于 2017-12-06T09:24:16.587 回答