且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何仅使用cmd从子文件夹复制文件?

更新时间:2023-12-05 13:55:58

以下内容将循环查找main下的目录,查找所有文件,并将它们全部放入目标位置中最顶层的子目录中:

The following will loop the directories under main, find all files, and put them all in the top most sub directory in destination as you're looking for:

CMD脚本:

@(SETLOCAL
  ECHO OFF
  SET "_MainDir=C:\Main\Dir"
  SET "_DestDir=%Temp%\Destination\Main\Dir"
)

FOR /D %%A in (
  %_MainDir%\*
) DO (
  IF NOT EXIST "%_DestDir%\%%~nxA" MD "%_DestDir%\%%~nxA"
  FOR /F %%a IN ('
    DIR /A-D /S /B "%%~fA\*"
  ') DO (
    ECHO Copying: "%%~fa"
    ECHO To: "%_DestDir%\%%~nxA\%%~nxa"
    COPY /B /V /Y "%%~fa" "%_DestDir%\%%~nxA\%%~nxa"
  )
)