在多个条件下使用IF语句 [英] Using IF statements with multiple conditions

查看:127
本文介绍了在多个条件下使用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语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆