如何创建一个忽略括号内文本而仅复制其余文本的蝙蝠?

发布于 2025-01-21 09:53:36 字数 361 浏览 1 评论 0原文

有很多这样的文件:

[2019]Text1(text2).zip

我希望我的批次仅复制text1,创建一个名称的文件夹,然后在其中移动文件。

应该这样:text1/[2019] text1(text2).zip

这是不起作用的,我不知道该如何忽略括号中的文本。

for %%F in (*.zip*.rar) do (
    for /f "tokens=2* delims=%%I in ("%%~nA") do (
    @mkdir "%%F" 2>nul & move "%%I" "%%J\" >nul

There are many files like this:

[2019]Text1(text2).zip

I want my batch to copy only Text1, create a folder with that name and move the file in it.

It should be like this: Text1/[2019]Text1(text2).zip

This doesn't work, and I don't know how to let it ignore text in brackets.

for %%F in (*.zip*.rar) do (
    for /f "tokens=2* delims=%%I in ("%%~nA") do (
    @mkdir "%%F" 2>nul & move "%%I" "%%J\" >nul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不醒的梦 2025-01-28 09:53:36
@ECHO OFF
SETLOCAL
rem The following settings for the source directory & destination directory are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files\t w o"
SET "destdir=u:\your results"

PUSHD "%sourcedir%"

FOR /f "delims=" %%b IN (
 'dir /b /a-d *.zip *.rar'
 ) DO (
 ECHO filename=%%b
 FOR /f "tokens=1,2,3,4,* delims=[]()" %%s IN ("%%b") DO (
 ECHO ++%%s++%%t++%%u++%%v++%%w++
 IF "%%w"=="" FOR %%q IN (.rar .zip) DO IF /i "%%q"=="%%v" (
  ECHO MD "%destdir%\%%t" 2>nul
  ECHO MOVE  "%%b" "%destdir%\%%t\" 2>nul
  )
 )
)

POPD

GOTO :EOF

建立源和目标目录后,请使用pushd将当前目录切换到源目录。

dir/b/ad code>命令的每一行中读取%% b the(基本列表无目录名称(/ad Ad)) - 选择两者 所有

所有

.zip和 使用[]()()的定界符检查%% B中的字符串。代码> %% t ...剩余的行向%% W

echo line只是为了显示文件名的标记 - 每个令牌 “ ++”之间显示。

2019%% T text1,%% u text2%% V 应该为空。

.zip%% W >是.zip.rar,case-nomentive(/i)和如果%% V匹配一个或另一个,执行MD创建目标目录(如果该目录已经存在,将生成一个错误消息,该消息被2> Nul)和然后将文件移至该目录。

最后,popd返回到原始当前目录

注意 md移动仅仅是echo ed,以便您可以查看指令是否正确。要实际执行MDecho您需要从这些行中删除echo关键字。

各种检查是一个合理的粗略尝试,以确保仅处理[2019] text1(text2).zip的文件。一个名为[2019] text1(text2)extra.zip[2019](text2)的文件。 [2019]] text1(text2).zip将通过检查。

可以将dir filemask更改为EG。 [*]*(*)。ZIP,但即使那将通过一些不合格的文件名。

移动命令将生成1 fie(s)在洗袜子时移动消息。您可以通过将> nul附加到move> move命令来抑制此消息。

@ECHO OFF
SETLOCAL
rem The following settings for the source directory & destination directory are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files\t w o"
SET "destdir=u:\your results"

PUSHD "%sourcedir%"

FOR /f "delims=" %%b IN (
 'dir /b /a-d *.zip *.rar'
 ) DO (
 ECHO filename=%%b
 FOR /f "tokens=1,2,3,4,* delims=[]()" %%s IN ("%%b") DO (
 ECHO ++%%s++%%t++%%u++%%v++%%w++
 IF "%%w"=="" FOR %%q IN (.rar .zip) DO IF /i "%%q"=="%%v" (
  ECHO MD "%destdir%\%%t" 2>nul
  ECHO MOVE  "%%b" "%destdir%\%%t\" 2>nul
  )
 )
)

POPD

GOTO :EOF

After establishing the source and destination directories, use PUSHD to switch the current directory to the source directory.

Read each line of a dir /b /ad command into %%b in turn (basic list without directorynames (/a-d) - selecting both all .zip and all .rar files.

The echo line is just to show the filename found. It can be deleted at your whim.

Then examine the string in %%b, using delimiters of [](). Assign the first token to %%s, the next to %%t...remainder of the line to %%w

The echo line is just to show the way the filename is tokenised - each token is shown between '++'. It can be deleted at your whim.

Using your example filename, [2019]Text1(text2).zip, %%s should be 2019, %%t Text1, %%u text2, %%v .zip and %%w should be empty.

If %%w is empty, then check that %%v is either .zip or .rar, case-insensitive (/i) and if %%v matches one or the other, execute a md to create the destination directory (if that directory already exists, an error message will be generated which is suppressed by 2>nul) and the file is then moved to that directory.

Finally, the popd returns to the original current directory

Note that the MD and MOVE are merely echoed so that you can see whether the instructions are correct. To actually perform the MD and ECHO you will need to remove the echo keyword from those lines.

The various checks are a reasonably crude attempt to make sure that only files matching [2019]Text1(text2).zip will be processed. A file named [2019]Text1(text2)extra.zip or [2019](text2).zip would fail the checks and not be processed, although [[2019]]Text1(text2).zip would pass the checks.

You could change the dir filemask to eg. [*]*(*).zip but even that would pass some non-compliant filenames.

The move command will generate a 1 fie(s) moved message when it washes its socks. You can suppress this message by appending >nul to the move command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文