重命名文件后,如果不存在中签++ for循环Windows批处理文件? [英] Renaming a file after IF EXIST check within ++ FOR loop in a Windows batch file?

查看:156
本文介绍了重命名文件后,如果不存在中签++ for循环Windows批处理文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜遍网络,无法找到确切的code我找...我发现东西都是非常相似的,但没有得到他们的工作。这是一个粗略的想法,我想要什么:

I scoured the web and could not find the exact code I'm looking for... I found things that are very similar but did not get them to work. Here is a rough idea what I want:

int i = 1;
FOR (i; i < 9999; i++)
IF EXIST filename.log THEN
REN filename%i%.log
ELSE IF EXIST filename%i%.log THEN
REN filename%(i+1)%.log

基本上我想检查是否文件存在,如果是这样,其重命名为filename0001 - 从那里,每个批次运行时,如果文件名####被发现时,它重命名它一后。所以第一次后,这当然是运行,当它发现该文件名存在,它将其重命名为filename0002等。

Basically I want to check if a filename exists and if so, rename it to filename0001 -- from there on, each time the batch is run, if filename#### is found, it renames it to one after that. So of course after the first time this is run, when it finds that filename exists it will rename it to filename0002 and so on.

感谢您!!

推荐答案

响应编辑评论。还增加了测试,以确保存在的debug.log。不想不必要地重命名文件

这第一个解决方案总是有最新的日志中的debug.log,下一个最新的debug0001.log,接下来为debug0002.log等最早的日志将具有最高数量。

This 1st solution always has the most recent log as debug.log, the next most recent as debug0001.log, the next as debug0002.log, etc. The oldest log will have the highest number.

@echo off
setlocal enableDelayedExpansion
set "base=debug"
if exist "%base%.log" for /f "eol=: delims=" %%F in (
  'dir /b /o-n "%base%*.log" ^| findstr /rix /c:"%base%.log" /c:"%base%[0-9][0-9][0-9][0-9].log"'
) do (
  set "name=%%~nF"
  set /a "n=10000!name:*%base%=! %% 10000 + 1"
  ren "%%F" "%base%!n!.log"
)

要做出最早的日志有0001和最新的具有最高的号码,则需要一个小的变化。只需要一个重命名。

To make the oldest log have 0001 and the newest have the highest number, then a small change is needed. Only one rename is needed.

@echo off
setlocal enableDelayedExpansion
set "base=debug"
if exist "%base%.log" for /f "eol=: delims=" %%F in (
  'dir /b /o-n "%base%*.log" ^| findstr /rix /c:"%base%.log" /c:"%base%[0-9][0-9][0-9][0-9].log"'
) do (
  set "name=%%~nF"
  set /a "n=10000!name:*%base%=! %% 10000 + 1"
  ren "%base%.log" "%base%!n!.log"
  goto :break
)
:break

这篇关于重命名文件后,如果不存在中签++ for循环Windows批处理文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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