批量读取文件夹名称并移动到相应的子目录

时间:2018-01-03 03:16:58

标签: batch-file

我一直在尝试创建批处理文件,以帮助我自动将文件夹分类到子文件夹中,如下所示:

作为标准,我想要排序的文件夹都以下列格式命名

“(代码)XXX NAME(来源)XXX”

// XXX代表可能存在的随机文字。

//第一个括号也不总是存在。

我需要的是批处理读取第二个括号内的文本,该文本对应于源的名称,然后检查是否已存在具有该源名称的文件夹并将该文件夹存储在源中文件夹(如果没有包含源名称的文件夹,请创建它)...这对文件夹中的每个文件都有。

例如: 我在名为“Animals”的文件夹中有2个文件,第一个文件名为“(X21)Fox(Carnivore)”,第二个文件名为“(X23432)Rabbit(Herbivore)”。

想法是让批次获得“食肉动物”和“食草动物”,看看是否在“动物”文件夹中有称为“食肉动物”和/或“草食动物”的文件夹,如果它们不存在则创建它们,最后将原始文件移动到相应的文件夹。

这一切都可行吗?我一直在阅读比较文件名上的字符串,但无论我尝试什么都不能让它工作,尽管我不是那么专家。

编辑: 我在网上找到了代码并尝试根据我的需要进行调整,但老实说,我并不了解大部分内容。

@echo off
:: setLocal EnableDelayedExpansion
set targetdir=DIRECTORY HERE
set setnum=0

for /D "delims=()" %%a in ('dir /b /o-n %targetdir%\*.tib') do set setnum=%%a
REM for /f "tokens=2 delims=()" %%a in ('dir /b %targetdir%\*.tib') do (
set setnum=%%i >> list.txt
goto loopexit
)
:loopexit
echo %setnum%
pause

REM for /D %%i in (*) do echo %%i >> list.txt

1 个答案:

答案 0 :(得分:0)

  

例如:我在名为“Animals”的文件夹中有2个文件夹,   第一个文件夹名为“(X21)Fox(Carnivore)”和第二个   一个名为“(X23432)兔子(草食动物)”。

     

这个想法是批量获得“食肉动物”和“草食动物”,看看是否   在“Animals”文件夹中有一个名为“Carnivore”和/或的文件夹   “草食动物”,如果它们不存在则创造它们,最后移动它   原始文件到相应的文件夹。

只是文件夹!当上面的文件被替换时,这似乎没问题 的文件夹即可。大胆的文字是替代品。我将按照文件夹逻辑  这个新代码的修订版。 Previous revision source

此脚本在当前目录中有效,因此它应该在目录中工作 动物那是在Animals文件夹中。

命令move虽然在同一个程序上进行了廉价操作,但它不会处理递归 文件系统。如果您有子文件夹,则可能需要使用xcopyrobocopy 在要复制或移动的文件夹中。

创建此代码以使用moverobocopy。第一次到达goto决定 哪个可以使用。如果两个goto都被删除或评论,它将进展到 使用1st,move,然后转到文件结尾。

主要剧本:

@echo off
setlocal enabledelayedexpansion

:: Remove rem at start of next line to use 'move' instead of 'robocopy'.
rem goto :opt_move
goto :opt_robocopy


:opt_move

:: Handles copy of dirs to subdirs with last group name.
:: Works in the current directory.

for /d %%A in ("*") do (
    call :lastgroup "%%~A" && (
        if not exist "!lastgroup!" md "!lastgroup!"
        move /y "%%~A\*" "!lastgroup!" && rd "%%~A"
    )
)

goto :eof


:opt_robocopy

:: Works with files and subdirectories.

for /d %%A in ("*") do (
    call :lastgroup "%%~A" && (
        if not exist "!lastgroup!" md "!lastgroup!"
        robocopy /move /e "%%~A" "!lastgroup!"
    )
)

goto :eof


:: Get last group between '(' and ')'.
:: :lastgroup is called with argument of filename or dirname.
:: Requires last character to be ')'.

:lastgroup
set "lastgroup="
setlocal
    :: 1st arg set to variable str.
    set "str=%~1"
    :: Check variable str is not empty.
    if not defined str endlocal & exit /b 1
    :: Check for opening brace and closing brace.
    if "%str%"=="%str:(=%" endlocal & exit /b 1
    if "%str%"=="%str:)=%" endlocal & exit /b 1
    :loop
    :: Get remainder after 1st opening brace.
    for /f "tokens=1,* delims=^(" %%A in ("%str%") do set "str=%%~B"
    :: Check for opening brace, loop if found.
    if not "%str%"=="%str:(=%" goto :loop
    :: Check last character is ')'.
    if not "%str:~-1%"==")" endlocal & exit /b 1
    :: Trim closing brace.
    set str=%str:~0,-1%
endlocal & set "lastgroup=%str%"

我在与主脚本相同的目录中使用了这个单元测试脚本来进行测试。

@echo off

:: Test environment in root of animals.

2>nul rd /s/q Carnivore
2>nul rd /s/q Herbivore
2>nul rd /s/q Fish
2>nul rd /s/q X20 X21 X23432
2>nul md Keepme
> "Keepme\test1.txt" type nul

if exist "(X20) Fox (Carnivore)" goto :next
md "(X20) Fox (Carnivore)"
>  "(X20) Fox (Carnivore)\test1.txt" type nul
>  "(X20) Fox (Carnivore)\test2.txt" type nul
:next

if exist "(X21) Fox (Carnivore)" goto :next
md "(X21) Fox (Carnivore)"
>  "(X21) Fox (Carnivore)\test3.txt" type nul
>  "(X21) Fox (Carnivore)\test4.txt" type nul
:next

if exist "(X23432) Rabbit (Herbivore)" goto :next
md "(X23432) Rabbit (Herbivore)"
>  "(X23432) Rabbit (Herbivore)\test1.txt" type nul
>  "(X23432) Rabbit (Herbivore)\test2.txt" type nul
:next

if exist "(X20) (X21) Cod (Fish)" goto :next
md "(X20) (X21) Cod (Fish)"
>  "(X20) (X21) Cod (Fish)\test1.txt" type nul
>  "(X20) (X21) Cod (Fish)\test2.txt" type nul
:next

if exist "(X21) (X22) Cod (Fish)" goto :next
md "(X21) (X22) Cod (Fish)"
>  "(X21) (X22) Cod (Fish)\test1.txt" type nul
>  "(X21) (X22) Cod (Fish)\test2.txt" type nul
md "(X21) (X22) Cod (Fish)\subfolder"
>  "(X21) (X22) Cod (Fish)\subfolder\test1.txt" type nul
:next

运行unittest后的树:

|   main.cmd
|   unittest.cmd
|
+---(X20) (X21) Cod (Fish)
|       test1.txt
|       test2.txt
|
+---(X20) Fox (Carnivore)
|       test1.txt
|       test2.txt
|
+---(X21) (X22) Cod (Fish)
|   |   test1.txt
|   |   test2.txt
|   |
|   \---subfolder
|           test1.txt
|
+---(X21) Fox (Carnivore)
|       test3.txt
|       test4.txt
|
+---(X23432) Rabbit (Herbivore)
|       test1.txt
|       test2.txt
|
\---Keepme
        test1.txt

运行robocopy后的树:

|   main.cmd
|   unittest.cmd
|
+---Carnivore
|       test1.txt
|       test2.txt
|       test3.txt
|       test4.txt
|
+---Fish
|   |   test1.txt
|   |   test2.txt
|   |
|   \---subfolder
|           test1.txt
|
+---Herbivore
|       test1.txt
|       test2.txt
|
\---Keepme
        test1.txt

运行后的树:

|   main.cmd
|   unittest.cmd
|
+---(X21) (X22) Cod (Fish)
|   \---subfolder
|           test1.txt
|
+---Carnivore
|       test1.txt
|       test2.txt
|       test3.txt
|       test4.txt
|
+---Fish
|       test1.txt
|       test2.txt
|
+---Herbivore
|       test1.txt
|       test2.txt
|
\---Keepme
        test1.txt

运行move会产生与robocopy类似的结果 不要移动名为子文件夹的文件夹。在每次测试之前运行unittest。

相关问题