将所有文件和文件夹移动到特定子文件夹中的批处理文件

问题描述 投票:0回答:1

尝试编写一个批处理脚本来移动一些文件和文件夹,但发现我无法完全正确地处理事情。我有一个像这样的文件夹结构:

Thomas Jefferson 80564
     CPD
     Some Folder 1
     Some Folder 2
     File.txt
     File.pdf
George Washington 80746
     CPD
     Some Folder 1

有大量文件夹,其中包含一些随机文件/文件夹。我在其中创建了一个“CPD”文件夹,并且需要将所有其他现有内容移至 CPD 文件夹中。我一直在搞类似的事情,但它并没有像我想要的那样工作。有什么简单的方法可以让它按照预期的方式进行处理吗?

@echo off

rem Define the name of the specific subfolder
set "destination=CPD"
set "homepath=C:\Users\Tyler\Documents\APEGS Test"

FOR /f "tokens=*" %%G in ('dir /ad /b "%homepath%\*"') DO (

for /d %%F in (*) do (
    move "%%F" "%destination%"
)

)

echo All files and folders moved to %destination%.
pause
batch-file cmd
1个回答
0
投票

如果我的评论是正确的,也许这样的东西对你有用。让我知道我是否走在正确的道路上:

@echo off
set "root_folder=C:\Users\..."

for /d %%A in ("%root_folder%\*") do (
    if exist "%%A\CPD\" (
        for /f "delims=" %%F in ('dir /b /a:-d "%%A" ^| findstr /v /i /c:"CPD"') do (
            move "%%A\%%F" "%%A\CPD\"
        )
        for /d %%D in ("%%A\*") do (
            if not "%%~nxD"=="CPD" (
                move "%%A\%%~nxD" "%%A\CPD\"
            )
        )
    ) else (
        md "%%A\CPD"
        for /f "delims=" %%F in ('dir /b /a:-d "%%A" ^| findstr /v /i /c:"CPD"') do (
            move "%%A\%%F" "%%A\CPD\"
        )
        for /d %%D in ("%%A\*") do (
            if not "%%~nxD"=="CPD" (
                move "%%A\%%~nxD" "%%A\CPD\"
            )
        )
    )
)

© www.soinside.com 2019 - 2024. All rights reserved.