语句中的条件重命名 [英] Conditional rename inside for statement

查看:88
本文介绍了语句中的条件重命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种解决方案,该解决方案允许我使用子字符串 say APPENDTEXT ,附加在末尾. 我之前已经尝试过了,效果很好!现在,我对如何进一步改进一无所知.我希望它能够识别文件名中已包含子字符串的文件,并跳过仅重命名这些文件的操作.

I'm looking for a solution that lets me rename all files, except .bat files, in the folder with a substring, say APPENDTEXT, appended at the end. I've tried this before and it worked out great! Now I'm at a loss on how to further improve it. I want it to recognize files that already have the substring inside the filename and skip renaming just those files.

在所有我的方法,但我失败了,最终追加了nonce或子字符串加倍.

In all my approaches, I've failed and ended up appending nonce or double the substring.

一种情况是某些文件夹包含混合文件,例如:

示例文件夹
| ---- file1.txt
| ---- file2 APPENDTEXT .txt
| ---- file3 APPENDTEXT .txt
| ---- file4.txt

我希望在运行批处理文件时将此文件夹中的文件重命名为以下名称:

示例文件夹
| ---- file1 APPENDTEXT .txt
| ---- file2 APPENDTEXT .txt
| ---- file3 APPENDTEXT .txt
| ---- file4 APPENDTEXT .txt

A scenario would be some folder with mixed files such as:

EXAMPLEFOLDER
|---- file1.txt
|---- file2APPENDTEXT.txt
|---- file3APPENDTEXT.txt
|---- file4.txt

I want the files in this folder to be renamed something like this when I run the batch file:

EXAMPLEFOLDER
|---- file1APPENDTEXT.txt
|---- file2APPENDTEXT.txt
|---- file3APPENDTEXT.txt
|---- file4APPENDTEXT.txt

当前,它们正像这样被重命名(或根本没有被重命名):

ExampleFOLDER
| ---- file1 APPENDTEXT .txt
| ---- file2 APPENDTEXTAPPENDTEXT .txt
| ---- file3 APPENDTEXTAPPENDTEXT .txt
| ---- file4 APPENDTEXT .txt

Currently, they are being renamed like this(or none at all):

EXAMPLEFOLDER
|---- file1APPENDTEXT.txt
|---- file2APPENDTEXTAPPENDTEXT.txt
|---- file3APPENDTEXTAPPENDTEXT.txt
|---- file4APPENDTEXT.txt

然后我正在考虑通过探索所有子文件夹来进一步改进它.

示例文件夹
| ---- file1 APPENDTEXT .txt
| ---- file2 APPENDTEXT .txt
| ---- file3 APPENDTEXT .txt
| ---- file4 APPENDTEXT .txt
| ---- ExampleFOLDER2
| ---- | ---- file21 APPENDTEXT .txt
| ---- | ---- file22 APPENDTEXT .txt

Then I'm thinking of improving it further by exploring into all subfolders.

EXAMPLEFOLDER
|---- file1APPENDTEXT.txt
|---- file2APPENDTEXT.txt
|---- file3APPENDTEXT.txt
|---- file4APPENDTEXT.txt
|---- EXAMPLEFOLDER2
|---- |---- file21APPENDTEXT.txt
|---- |---- file22APPENDTEXT.txt

我还没有这样做,因为我仍然想不出如何停止重命名已经包含子字符串 APPENDTEXT 的文件名.如果解决方案基于此答案,那就太好了.有人可以帮我吗?

I've not done it yet because I still can't figure out how to stop renaming filenames that already contain the substring APPENDTEXT. It'd be great if the solution is built upon this answer. Can someone help me?

推荐答案

@echo off
setlocal EnableDelayedExpansion

set "var=APPENDTEXT"
for /F "delims=" %%a in ('dir *.* /b /a-d ^| findstr /v /i "\.bat$" ') do (
   set "name=%%~Na"
   if "!name:%var%=!" equ "!name!" ren "%%a" "%%~na%var%%%~xa"
)
pause

这篇关于语句中的条件重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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