Batch file 批处理以读取文件夹名称并移动到相应的子目录
我一直在尝试创建一个批处理文件,以帮助我自动将文件夹分为以下子文件夹: 作为标准,我想要排序的文件夹都以以下格式命名 “(代码)XXX名称(来源)XXX” //XXX表示可能存在的随机文本 //第一个括号也不总是存在 我需要的是批处理读取第二个括号内的文本,它对应于源的名称,然后检查是否已经有一个具有该源名称的文件夹,并将该文件夹存储在源文件夹中(如果没有具有源名称的文件夹,则创建它)。。。这适用于文件夹中存在的每个文件 例如: 我在一个名为“动物”的文件夹中有两个文件,第一个文件名为“(X21)狐狸(食肉动物)”,第二个文件名为“(X23432)兔子(食草动物)” 其想法是让批处理获得“肉食动物”和“草食动物”,查看“动物”文件夹中是否有名为“肉食动物”和/或“草食动物”的文件夹,如果不存在,则创建它们,最后将原始文件移到相应的文件夹中 这完全可行吗?我一直在读关于比较文件名字符串的文章,但不管我怎么做都不能让它工作,尽管我不是一个专家 编辑: 我在网上找到了代码,并试图根据自己的需要对其进行修改,但老实说,我并不了解其中的大部分内容Batch file 批处理以读取文件夹名称并移动到相应的子目录,batch-file,Batch File,我一直在尝试创建一个批处理文件,以帮助我自动将文件夹分为以下子文件夹: 作为标准,我想要排序的文件夹都以以下格式命名 “(代码)XXX名称(来源)XXX” //XXX表示可能存在的随机文本 //第一个括号也不总是存在 我需要的是批处理读取第二个括号内的文本,它对应于源的名称,然后检查是否已经有一个具有该源名称的文件夹,并将该文件夹存储在源文件夹中(如果没有具有源名称的文件夹,则创建它)。。。这适用于文件夹中存在的每个文件 例如: 我在一个名为“动物”的文件夹中有两个文件,第一个文件名为“(X21
@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
例如:我在一个名为“动物”的文件夹中有2个文件夹,
第一个文件夹称为“(X21)狐狸(食肉动物)”,第二个文件夹称为“
其中一个叫“(X23432)兔(食草动物)”
这个想法是让这批人得到“食肉动物”和“食草动物”,看看是否
在“动物”文件夹中,有称为“食肉动物”和/或
“食草动物”,如果它们不存在,则创建它们,最后移动
将原始文件保存到相应的文件夹中
只是文件夹!当上面的文件被替换为
文件夹。粗体文本是替换。我将遵循文件夹的逻辑
随着新规范的修订。
此脚本在当前目录中工作,因此它应该在该目录中工作
关于动物。在动物文件夹中
命令move
不处理递归,尽管它是对同一对象的廉价操作
文件系统。如果您有子文件夹,您可能需要使用xcopy
或robocopy
在要复制或移动的文件夹中
创建此代码以使用move
或robocopy
。第一个到达的goto
决定
使用哪一个。如果两个goto
s都有错误或注释,它将前进到
使用第一个,即移动
,然后转到文件末尾
主脚本:
@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%"
我在与要测试的主脚本相同的目录中使用了这个unittest脚本
@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在每次测试之前运行。欢迎使用堆栈溢出。这不是一个code/SQL/regex编写服务,在这里,您发布了一个需求列表和选择的语言,然后一个代码猴子为您编写代码。我们非常乐意提供帮助,但我们希望您首先自己努力解决问题。(我一直在尝试,没有任何代码也算不上努力。)一旦你这样做了,你可以解释你遇到的问题,包括你工作的相关部分,并提出一个具体的问题,我们将尽力提供帮助。祝你好运。我真的很想。。但是我没有什么实际的帮助,粘贴我读过的每一个源代码也不行,我编辑了一段代码,我试着从谷歌上找到的一篇类似的文章中改编,如果我有任何冒犯的地方,很抱歉。谢谢你的解释!绝对有助于我更好地理解代码!,不过我只有一个问题,我试着把蝙蝠放在动物文件夹上运行,但什么也没发生,然后试着将代码上的targetdir更改为当前目录路由,什么都没有,我是否遗漏了什么:/?脚本在动物文件夹旁边。有关使用测试的文件/文件夹结构的概述,请参见答案的编辑
(X21)Fox(Carnivore)
是一个无扩展文件。顺便说一句,如果您想让脚本执行当前目录,请将设置“targetdir=anives”
更改为设置“targetdir=。”
或设置“targetdir=%cd%”。刚刚测试过它!,这几乎就是我想学的!我现在遇到的问题是,这对文件有效,但如果我有一个名为“(X21)Fox(Carnivore)”的文件夹,它对它不起作用,不移动它,也不做任何事情:/它对无扩展名的文件和有扩展名的文件有效,但对文件夹/目录无效。我试着用我找到的这篇文章来更改参数(),但目前为止运气不佳。