繁体   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