在多个条件下使用IF语句

编程入门 行业动态 更新时间:2024-10-28 00:13:59
本文介绍了在多个条件下使用IF语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要根据文件名更改多个文件名.

I need to change multiple files names based on their name.

我有这样命名的文件.

001.mp3 002.mp3 003.mp3 004.mp3 005.mp3 ...etc

我编写了一些代码来实现我的目标,

I made some code to achieve my aim like this:

@echo off for %%i in (*.mp3) do if %%~ni gtr 003 ren %%i %%~ni-new%%~xi

我已经成功获得了这样的期望结果:

I have gotten successfully desired result like this:

001.mp3 002.mp3 003.mp3 004-new.mp3 005-new.mp3 ...etc

但是现在我正在尝试不同的东西,例如'if between'.

But now I am trying something different like 'if between'.

例如:

@echo off for %%i in (*.mp3) do if %%~ni between 001 && 003 ren %%i %%~ni-chapter-1%%~xi if %%~ni between 004 && 006 ren %%i %%~ni-chapter-2%%~xi if %%~ni between 007 && 020 ren %%i %%~ni-chapter-3%%~xi if %%~ni between 021 && 030 ren %%i %%~ni-chapter-4%%~xi if %%~ni between 031 && 045 ren %%i %%~ni-chapter-5%%~xi

因此所需的结果将如下所示:

so the desired result will be like this:

001-chapter-1.mp3 002-chapter-1.mp3 003-chapter-1.mp3 004-chapter-2.mp3 005-chapter-2.mp3 006-chapter-2.mp3 007-chapter-3.mp3 008-chapter-3.mp3 009-chapter-3.mp3 010-chapter-3.mp3 ...etc

请帮助我修复该代码,如演示所示.

Please, help me to fix this code as demonstrated.

推荐答案

我想您也可以使用延迟扩展并删除嵌套的If语句:

I suppose you could use delayed expansion and remove the nested If statements too:

@Echo Off SetLocal EnableDelayedExpansion Set "i=" For /F "Delims=" %%A In ('Where .:???.mp3 2^>Nul')Do ( If 1%%~nA Gtr 1000 Set "i=1" If 1%%~nA Gtr 1003 Set "i=2" If 1%%~nA Gtr 1006 Set "i=3" If 1%%~nA Gtr 1020 Set "i=4" If 1%%~nA Gtr 1030 Set "i=5" If 1%%~nA Gtr 1045 Set "i=" If Defined i Ren "%%A" "%%~nA-chapter-!i!%%~xA" )

在上面的示例中,我已使用Where命令将返回的元变量限制为具有3字符基本名称的元变量.

In the example above, I have used the Where command to limit the returned metavariables to those with 3 character basenames. This will prevent cycling through any renamed files again, (as you were renaming them in the same directory with the same extension, which would still match your *.mp3 pattern).

请注意,此操作仅过滤具有三个字符的.mp3文件,而不能确定这些字符都是整数.我让您决定是否要自己执行类似的操作.

Please note that this only filters .mp3 files with three characters, it does not make any determination that those characters are each integers. I'll leave you to decide if you wish to implement something like that yourself.

更多推荐

在多个条件下使用IF语句

本文发布于:2023-10-16 11:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497432.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   条件下   语句

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!