批处理文件到排序文件夹

时间:2013-02-02 22:41:59

标签: windows batch-file

我正在尝试构建简单的批处理窗口文件,该文件会根据文件名将文件夹移动到位置。 例如,如果文件夹包含new.work.directory-abc,则基于单词“work”,我希望将其移动到h:\ work。 如果文件夹名称包含基于单词“school”的old.school.stuff-1s2,则应将其移至h:\ school。

扫描的目录将始终包含新文件夹,因此我将每隔1小时运行一次批处理。

谢谢!

2 个答案:

答案 0 :(得分:0)

如果您无法预测关键字在名称中的显示位置....

@echo off
setlocal

:: path to hourly folders:
pushd "c:\temp\hourly"

for /d %%I in (h:\*) do (
    for /d %%x in (*) do (
        for /f %%A in ('echo %%~nx ^| find /i "%%~nI"') do (
            set /p a="Moving %%x to %%I... "<NUL
            move "%%x" "%%I" >NUL
            echo Done.
        )
    )
)
popd

<强> Assumtions:

  • 您已将H:\上的文件夹移至每小时文件夹

工作原理

  • H:\
  • 上第一个目录的名称
  • 对于每个每小时目录,查看第一个H:\目录的名称是否包含在每小时目录的名称中
  • 如果是,请将该每小时目录移至H:\
  • 上的第一个目录
  • 循环到H:\上的第二个目录并重复。

示例输出

C:\Temp\Hourly>dir *.
 Volume in drive C has no label.
 Volume Serial Number is CE09-0D42

 Directory of C:\Temp\Hourly

02/06/2013  08:57 AM    <DIR>          .
02/06/2013  08:57 AM    <DIR>          ..
10/20/2011  01:17 PM    <DIR>          99 Windows 7 Shortcuts
01/24/2013  02:00 PM    <DIR>          A sysinternals folder
06/18/2012  02:45 PM    <DIR>          funny
01/14/2013  12:47 PM    <DIR>          PortableApps
01/16/2013  09:44 AM    <DIR>          songs
02/06/2013  08:33 AM    <DIR>          This is a temp directory
01/24/2013  02:01 PM    <DIR>          user prompts
06/20/2012  08:00 AM    <DIR>          We Are Anonymous
               0 File(s)              0 bytes
              10 Dir(s)  176,419,254,272 bytes free

C:\Temp\Hourly>dir h:\*.
 Volume in drive C has no label.
 Volume Serial Number is CE09-0D42

 Directory of h:\

08/02/2011  02:24 PM    <DIR>          52bb3510d8007c2f356572
08/16/2011  06:35 AM    <DIR>          ATI
11/07/2012  01:22 AM    <DIR>          Desktop Document
09/20/2011  01:50 PM    <DIR>          Documentum
02/23/2011  08:07 AM    <DIR>          Drivers
01/04/2013  01:17 PM    <DIR>          ffe
12/19/2012  11:19 AM    <DIR>          flex_renamer
01/11/2012  02:44 PM    <DIR>          Games
01/08/2013  12:14 PM    <DIR>          gnuwin32
03/03/2011  09:32 AM    <DIR>          Intel
07/13/2009  10:20 PM    <DIR>          PerfLogs
09/17/2010  01:08 PM    <DIR>          PortableFrozenBubble
08/28/2012  02:27 PM    <DIR>          portaputty
11/13/2012  10:40 AM    <DIR>          Program Files
01/22/2013  02:30 PM    <DIR>          Program Files (x86)
06/01/2012  01:08 PM    <DIR>          SpamBayes
02/24/2011  04:21 PM    <DIR>          Symantec
01/24/2013  01:56 PM    <DIR>          Sysinternals
04/19/2011  08:35 AM    <DIR>          Tcl
04/19/2011  08:30 AM    <DIR>          TclDevKit
02/06/2013  08:52 AM    <DIR>          temp
02/19/2010  05:42 PM    <DIR>          universal print driver
11/07/2012  01:11 AM    <DIR>          Users
02/06/2013  08:56 AM    <DIR>          Windows
               0 File(s)              0 bytes
              24 Dir(s)  176,419,254,272 bytes free

C:\Temp\Hourly>movedir.bat
Moving A sysinternals folder to h:\Sysinternals... Done.
Moving This is a temp directory to h:\temp... Done.
Moving 99 Windows 7 Shortcuts to h:\Windows... Done.

C:\Temp\Hourly>dir *.
 Volume in drive C has no label.
 Volume Serial Number is CE09-0D42

 Directory of c:\Temp\Hourly

02/06/2013  09:00 AM    <DIR>          .
02/06/2013  09:00 AM    <DIR>          ..
06/18/2012  02:45 PM    <DIR>          funny
01/14/2013  12:47 PM    <DIR>          PortableApps
01/16/2013  09:44 AM    <DIR>          songs
01/24/2013  02:01 PM    <DIR>          user prompts
06/20/2012  08:00 AM    <DIR>          We Are Anonymous
               0 File(s)              0 bytes
               8 Dir(s)  176,419,254,272 bytes free

C:\Temp\Hourly>

答案 1 :(得分:0)

在看到rojo的答案之前我做的另一个版本(使用变量'find and replace')......:

@echo off
for /f "tokens=*" %%i in ('dir %~dp0 /b /ad') do call :match %%i
goto continue
    :match
    set dirname=%1
    if "%dirname%"=="work" goto :eof
    if "%dirname%"=="school" goto :eof
    if "%dirname:work=m%" neq "%dirname%" move "%~dp0%dirname%" "%~dp0%work\"
    if "%dirname:school=m%" neq "%dirname%" move "%~dp0%dirname%" "%~dp0%school\"
    goto :eof
:continue

对于您需要的每个不同类别,您必须制作另一个

if "%dirname%"=="category" goto eof

和另一个

if "%dirname:category=m%" neq "%dirname%" move "%~dp0%dirname%" "%~dp0%category\"

"%dirname:category=m%"中的'm'只是任意的,您可以将其更改为其他任何内容。