如何将特定文件夹的每个子文件夹中的所有* .pdf文件移动到每月创建的子文件夹?

时间:2015-08-27 18:20:20

标签: windows batch-file pdf scripting

我一直在尝试创建一个单独的批处理文件,该文件将为我每月处理各种文件夹中的文件。

目前,文件夹C:\test\WSP\包含许多其他文件夹(帐户名称),其中包含PDF文件。我们需要:

  1. MM-YYYY
  2. 内创建一个C:\test\WSP\{Account Name}\的文件夹
  3. 并将这些PDF移动到该新文件夹中。因此,最终结果为C:\test\WSP\{Account Name}\08-2015\,其中包含所有新PDF。
  4. 然后转到下一个目录C:\test\WSP\{Account Name2}
  5. 创建08-2015文件夹并将所有PDF文件移至C:\test\WSP\{Account Name2}\08-2015依此类推。
  6. 我可以通过放置包含以下内容的批处理文件,在每个{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移动到新文件夹中,然后移动到下一个目录?

2 个答案:

答案 0 :(得分:0)

使用for /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

答案 1 :(得分:0)

以下是此任务的注释批处理代码:

@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文章Using command redirection operators以获取>nul的说明,该文件通过命令 MOVE 将有关已移动文件的信息输出重定向到设备 NUL 压制此信息。

相关问题