簡體   English   中英

如何將特定文件夾的每個子文件夾中的所有 *.pdf 文件移動到每月創建的子文件夾?

[英]How to move all *.pdf files in each subfolder of a specific folder to a monthly created subfolder?

我一直在嘗試創建一個批處理文件,該文件將每月為我處理各個文件夾中的文件。

目前,文件夾C:\\test\\WSP\\有許多其他文件夾(帳戶名),其中包含 PDF。 我們需要:

  1. C:\\test\\WSP\\{Account Name}\\創建一個帶有MM-YYYY的文件夾
  2. 並將這些 PDF 移動到該新文件夾中。 所以最終結果是C:\\test\\WSP\\{Account Name}\\08-2015\\有所有新的 PDF。
  3. 然后移動到下一個目錄C:\\test\\WSP\\{Account Name2}
  4. 創建08-2015文件夾並將所有 PDF 移動到C:\\test\\WSP\\{Account Name2}\\08-2015等等。

我可以通過放置包含以下內容的批處理文件在每個{Account Name}文件夾中根據需要進行處理:

@ECHO OFF
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set year=%%c
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set month=%%a
set MONTH="%month%"
set YEAR ="%year%""
md %YEAR%\%MONTH%
MOVE *.pdf %YEAR%\%MONTH%\

並且每個月都在每個文件夾中運行,但是,這里有 200 多個文件夾。

無論如何都要梳理每個文件夾,創建目錄並將PDF移動到新文件夾中,然后移動到下一個目錄?

用於 /d枚舉文件夾:

@echo off
for /f "tokens=2-4 delims=/.- " %%a in ('date /T') do set "year=%%c" & set "month=%%a"
set newdir=%MONTH%-%YEAR%

for /d %%d in ("C:\test\WSP\*") do (
    md "%%d\%newdir%\\"
    MOVE "%%d\*.pdf" "%%d\%newdir%\\"
)
pause

這是此任務的注釋批處理代碼:

@echo off

rem Get month and year from environment variable DATE. The
rem date format depends on region and language settings for
rem the current user. The code below expects the date in
rem format DD.MM.YYYY or DD/MM/YYYY without or with weekday
rem at beginning which is the reason why referencing the
rem characters to copy from date string is done from end of
rem the string instead of beginning. Run in a command prompt
rem window echo %DATE% to see the date format of your user
rem account.

set "MonthFolder=%DATE:~-7,2%-%DATE:~-4%"

rem Use command FOR to process each non hidden and non
rem system subdirectory of specified parent directory. The
rem loop variable D holds the name of the found subdirectory
rem with complete path.

rem If the subdirectory contains 1 or more files with the
rem file extension PDF, a subdirectory with month any year
rem is created in current subdirectory in case of not already
rem existing and all PDF files are moved into this monthly
rem created subdirectory.

for /D %%D in ("C:\test\WSP\*") do (
    if exist "%%D\*.pdf" (
        if not exist "%%D\%MonthFolder%\*" md "%%D\%MonthFolder%"
        move /Y "%%D\*.pdf" "%%D\%MonthFolder%\" >nul
    )
)

set "MonthFolder="

要了解使用的命令及其工作原理,請打開命令提示符窗口,在那里執行以下命令,並仔細閱讀為每個命令顯示的所有幫助頁面。

  • echo /?
  • for /?
  • if /?
  • md /?
  • move /?
  • rem /?
  • set /?

另請參閱有關使用命令重定向運算符的 Microsoft 文章,以了解>nul的說明,該文章將命令MOVE輸出的有關已移動文件的信息重定向到設備NUL以抑制此信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM