0

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

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

谢谢!

4

2 回答 2

0

If you cannot predict at what position the keyword will appear in the name....

@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:

  • You already have folders on H:\ into which to move the hourly folders

How it works

  • Take the name of the first directory on H:\
  • For each hourly directory, see whether the name of first H:\ directory is contained within the name of the hourly directory
  • If it is, move that hourly directory to the first directory on H:\
  • Loop to the second directory on H:\ and repeat.

Example output

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>
于 2013-02-03T03:32:28.447 回答
0

我在看到 rojo 的答案之前制作的另一个版本(使用变量“查找和替换”)......:

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

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

于 2013-02-03T05:17:52.227 回答