0

このディレクトリ構造があります:

Dir1
--Dir2
    --File1
    --File2
--Dir3
    --File3
--File4
--File5

次に、バッチファイルを使用して、サブディレクトリ(Dir2、Dir3)内のすべてのファイルを親ディレクトリDir1にコピーします。私は以下のコードを思いついたが、それは完全には機能しない。以下の出力が得られます-

Directory2             --It has 4 files all together
Invalid number of parameters
Invalid number of parameters
Does E:\Directory1\Copy\File1.dat specify a file name   -- And only this file gets copied
or directory name on the target
(F = file, D = directory)?

コード-

@echo off
call :treeProcess
Pause
goto :eof

:treeProcess
rem Do whatever you want here over the files of this subdir, for example:
for /D %%d in (*) do (
    echo %%d
    cd %%d
    for %%f in (*) do xcopy %%f E:\Movies\Copy\%%f
    call :treeProcess
    cd ..
)
exit /b
4

1 に答える 1

6

バッチファイルは必要ありません。Dir1フォルダーから次のコマンドを実行します。

for /r /d %F in (*) do @copy /y "%F\*"

バッチファイルとして

@echo off
for /r /d %%F in (*) do copy /y "%%F\*"

ただし、複数の子フォルダに同じファイル名がある場合があることに注意してください。Dir1で生き残るのは1つだけです。

編集

上記は、Dir1フォルダーからコマンド(またはスクリプト)を実行していることを前提としています。スクリプトがDir1へのパスを含むように拡張されている場合は、どこからでも実行できます。

for /r "pathToDir1" /d %F in (*) do @copy /y "pathToDir1\%F\*"

またはバッチファイルとして

@echo off
set "root=pathToDir1"
for /r "%root%" /d %%F in (*) do copy /y "%root%\%%F\*"

バッチファイルへの引数としてDir1へのパスを渡すことができます。現在のフォルダを使用する場合は、パスとして渡し.ます。

@echo off
for /r %1 /d %%F in (*) do copy /y "%~1\%%F\*"
于 2013-02-11T12:17:52.740 に答える