简体   繁体   English

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

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

I have been attempting to create a single batch file that will do monthly work for me with files throughout various folders.我一直在尝试创建一个批处理文件,该文件将每月为我处理各个文件夹中的文件。

Currently the folder C:\\test\\WSP\\ has numerous other folders (account names) with PDF's in them.目前,文件夹C:\\test\\WSP\\有许多其他文件夹(帐户名),其中包含 PDF。 We need to:我们需要:

  1. create a folder with MM-YYYY within C:\\test\\WSP\\{Account Name}\\C:\\test\\WSP\\{Account Name}\\创建一个带有MM-YYYY的文件夹
  2. and move those PDF's into that new folder.并将这些 PDF 移动到该新文件夹中。 So the end result being C:\\test\\WSP\\{Account Name}\\08-2015\\ with all new PDF's in there.所以最终结果是C:\\test\\WSP\\{Account Name}\\08-2015\\有所有新的 PDF。
  3. Then move onto the next directory C:\\test\\WSP\\{Account Name2} ,然后移动到下一个目录C:\\test\\WSP\\{Account Name2}
  4. create the 08-2015 folder and move all the PDFs into C:\\test\\WSP\\{Account Name2}\\08-2015 so on and so forth.创建08-2015文件夹并将所有 PDF 移动到C:\\test\\WSP\\{Account Name2}\\08-2015等等。

I can do process as needed within each {Account Name} folder by placing a batch file containing:我可以通过放置包含以下内容的批处理文件在每个{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%\

And running every month in each folder, however, there are over 200 folders here.并且每个月都在每个文件夹中运行,但是,这里有 200 多个文件夹。

Is there anyway to comb through each folder, create the directory and move the PDF's into the new folder, then move onto the next directory?无论如何都要梳理每个文件夹,创建目录并将PDF移动到新文件夹中,然后移动到下一个目录?

Use for /d to enumerate the folders:用于 /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

Here is a commented batch code for this task:这是此任务的注释批处理代码:

@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="

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.要了解使用的命令及其工作原理,请打开命令提示符窗口,在那里执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

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

And see also the Microsoft article about Using command redirection operators for an explanation of >nul which redirects the information output by command MOVE about moved files to device NUL to suppress this information.另请参阅有关使用命令重定向运算符的 Microsoft 文章,以了解>nul的说明,该文章将命令MOVE输出的有关已移动文件的信息重定向到设备NUL以抑制此信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 批处理文件以在文件夹中创建新的子文件夹,将文件移动到目录中所有文件夹的新创建的子文件夹 - batch file to create new subfolder in folder, move file to newly created subfolder, for all folders in directory 将特定子文件夹中的文件移动到另一个子文件夹 - Move files in specific subfolders to another subfolder Windows 7 Batch - 创建子文件夹,然后查找文件名中包含特定文本的文件并将这些文件移动到新创建的子文件夹中 - Windows 7 Batch - Create subfolder, then find files with certain text in file name and move those files in the newly created subfolder 用相同的名称创建新的子文件夹并将文件移动到新文件夹 - Create new subfolder with same name and move files to the new folder 如何将子文件夹中的文件重命名为特定格式 - How to rename files in subfolder into a specific format 如何从子文件夹访问文件,该子文件夹位于最近使用python创建或修改的文件夹中? - How to access files from a subfolder that lies into a folder which recently created or modified using python? 将文件移动到目标目录的子文件夹中 - Move files into a subfolder of a destination directory 如何使用此批处理文件将所有* .txt文件从子文件夹复制到批处理文件文件夹? - How to copy all *.txt files from subfolder to batch file folder using this batch-file? 获取特定子文件夹中的文件列表 - get files list in a specific subfolder 批处理文件以计算文件夹中的所有文件和子文件夹文件,并使用批处理文件以文件夹名称写入输出文件 - batch file to count all files in a folder and subfolder files and write output file in folder name using batch file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM