有人可以告诉我这个FOR循环怎么了吗? [英] Can someone please tell me what's wrong with this FOR loop?

查看:60
本文介绍了有人可以告诉我这个FOR循环怎么了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

道歉(如果有重复的话),但到目前为止没有其他答案有帮助.

Apologies if duplicate, but no other answers so far have helped.

我要做的就是遍历文件夹中的文件,然后重命名文件/扩展名的最后一部分.

All I'm trying to do is loop through the files in a folder, and rename the last part of the file/extension.

简单地说-可能有1-90个文件,[文件名] _01-[文件名] _90,并且每天(通过Windows事件计划程序)该数字必须增加一.

Simply put - there could be 1-90 files, [filename]_01 - [filename]_90, and each day (via windows event scheduler) the number has to increment by one.

我似乎无能为力.

当文件达到某些里程碑(30-60-90)时,文件的行为也应略有不同,但是我相信,如果变量正确更新,这应该已经可以工作.

The files are also meant to behave slightly differently when they hit certain milestones (30-60-90) but this I believe should already work if the variables update properly.

我尝试了很多变量寻址的可能组合(!variable!/%variable%/etc.),虽然我可以进入循环,但它不会重复,也不会更新文件末尾的变量号.

I have tried so many possible combinations of variable addressing (!variable!/%variable%/etc.) and while I can enter the loop, it does not repeat, nor update the variable number for the end of the files.

SetLocal EnableDelayedExpansion
set cnt=0
for %%A in (*) do set /a cnt+=1
set /A fileNumber = %cnt%-1
set /A newFileNumber = %cnt%
echo %fileNumber%
echo %newFileNumber%
for /l %%F in (%fileNumber%,1,1) do (
    if %newFileNumber%==90 (
        ren "*_%fileNumber%.don" "*_%newFileNumber%.csv"
        )
    if %newFileNumber%==60 (
        ren "*_%fileNumber%.don" "*_%newFileNumber%.csv"
        )
    if %newFileNumber%==60 (
        "ren *_%fileNumber%.don" "*_%newFileNumber%.csv"
        )
    ren "*_%fileNumber%.don" "*_%newFileNumber%.don"
    set fileNumber=%fileNumber%-1
    set newFileNumber=%newFileNumber%-1
    )

这应该简单地更新目录中的所有文件,使文件名增加1.如果有人能指出我要去哪里,我将非常感激.

This should simply update all the files in the directory to increment by 1 in the file name. If anyone can point out where I'm going wrong I would really appreciate it.

推荐答案

@ECHO OFF
SetLocal EnableDelayedExpansion
:: target directory name in variable for convenience.
SET "targetdir=U:\sourcedir"
:: switch to target directory
pushD "%targetdir%"
:: Start count at 100 so that it is 3 digits long
set cnt=100
dir
for %%A in (*) do set /a cnt+=1
set /A fileNumber = %cnt%
set /A newFileNumber = %cnt%+1
:: echoing last 2 characters (will be digits) of variables
echo %fileNumber:~-2% (%filenumber%)
echo %newFileNumber:~-2% (%newfilenumber%)
:: Assign %%F to values 100+actual descending by 1 to 101
for /l %%F in (%fileNumber%,-1,100) do (
 rem note need REM remarks within the loop
 REM use !varname! for current value of variable VARNAME
    if !newFileNumber:~-2!==90 (
        ECHO ren "*_!fileNumber:~-2!.don" "*_!newFileNumber:~-2!.csv"
        )
    if !newFileNumber:~-2!==60 (
        ECHO ren "*_!fileNumber:~-2!.don" "*_!newFileNumber:~-2!.csv"
        )
    if !newFileNumber:~-2!==30 (
        ECHO ren "*_!fileNumber:~-2!.don" "*_!newFileNumber:~-2!.csv"
        )
 rem Since you've just renamed (eg) _59.don to _60.csv, _59.don is missing  
    IF EXIST "*_!fileNumber:~-2!.don" ECHO ren "*_!fileNumber:~-2!.don" "*_!newFileNumber:~-2!.don"
    set /A fileNumber=fileNumber-1
    set /A newFileNumber=newFileNumber-1
    )
:: return to original directory
popd
GOTO :EOF

所需的REN命令仅被 ECHO 用于测试目的.确认命令正确无误后,将 ECHO REN 更改为 REN ,以实际重命名文件.

The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the files.

不幸的是,您向我们展示了如何无法进行一些含糊的操作.因此,我们需要弄清楚您打算做什么,这是更多的工作,并且容易追逐野鹅.

Unfortunately, you've shown us HOW you've NOT been able to do some operation that's a little vague. We thus need to nut out what you intend to do, which is more work and prone to chasing wild geese.

您没有说要怎么处理文件* _90,所以我们在这里无能为力.

You don't say what to do with files *_90, so we can't help there.

您似乎想将* _59.don更改为* _60.csv,但是您的重命名想要将* _60.DON更改为下一次调用,因此除非名称已从_ * 60.CSV更改回* _60.DON,这行不通.

You appear to want to change *_59.don to *_60.csv, but your rename wants to change *_60.DON the next invocation, so unless the name has been changed from _*60.CSV back to *_60.DON, this isn't going to work.

请注意,此例程的基础是使用变量的最后两个字符.这是为了容纳您所说的编号方案中的前导 0 .

Note that the basis of this routine is to work with the last two characters of variables. This is to accommodate the leading 0 you say is in your numbering scheme.

标准做法是假设您将对测试目录执行任何例程以进行验证.

It's standard practice to assume that you will exercise any routine against a test directory for verification.

请注意,每个 REN 都是 ECHO ,因此它没有被执行,仅被报告.将 ECHO REN 更改为 REN 以实际执行命令.

Note that every REN is ECHOed, so it is not EXECUTED, merely reported. Change the ECHO REN to REN to actually execute the command.

还请注意,批处理在很大程度上不区分大小写.这意味着除非您愿意,否则不必耗尽SHIFT键.

Note also that batch is largely case-insensitive. This means you don't have to wear out your SHIFT key unless you want to.

这篇关于有人可以告诉我这个FOR循环怎么了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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