【问题标题】:How to move all *.pdf files in each subfolder of a specific folder to a monthly created subfolder?如何将特定文件夹的每个子文件夹中的所有 *.pdf 文件移动到每月创建的子文件夹?
【发布时间】:2015-11-22 06:32:25
【问题描述】:

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

当前文件夹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 移动到新文件夹中,然后移动到下一个目录?

【问题讨论】:

  • 好的,你的问题是?
  • 这不是问题,而是任务描述。
  • 一个批处理文件会执行多个批处理文件吗?为每个客户写一个?
  • 你能告诉我们你到目前为止做了什么吗?
  • 对不起,是的。我可以使用下面的批处理文件让它在每个文件夹中工作:@ 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 %年%\%MONTH% 移动 *.pdf %YEAR%\%MONTH%\

标签: windows batch-file pdf scripting


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    @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 /?

    另请参阅有关 Using command redirection operators 的 Microsoft 文章以了解 >nul 的说明,该文章将通过命令 MOVE 输出的有关已移动文件的信息重定向到设备 NUL 以抑制这个信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-16
      • 2017-05-04
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      • 2011-04-24
      • 1970-01-01
      • 2013-08-25
      相关资源
      最近更新 更多