基于使用批处理文件内容重命名文件 [英] Renaming file based on its content using Batch file

查看:244
本文介绍了基于使用批处理文件内容重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个批处理文件,读取该文件nest.txt描述名称present和基于描述名称重命名文件名。

I need a batch file that reads the description name present in the nest.txt file and rename that file name based on description name.

例如,我有所谓的nest.txt的文件名,当我们打开文本文件(nest.txt)的文件名的第二行已说明(比如描述=人),那么这个批处理文件应该重命名我nest.txt文件Man.txt

For example i have a file name called "nest.txt" and when we open the text file(nest.txt) the second line of the file name has Description(say Description=Man) then the batch file should rename my nest.txt file as Man.txt

打开文件nest.txt
复制文件nest.txt描述名称present

Open a file nest.txt Copy the description name present in file nest.txt

Header
Description=MAN
Menu KeyWords=MAN_ABC

现在关闭此文件和文件nest.txt重命名为MAN.txt

Now close this file and rename file nest.txt to MAN.txt

推荐答案

下面的批处理文件需要一个命令行参数,必须从其中要重命名的文件存在文件夹中运行。你可以很容易地添加更多code批处理文件,使之更智能的(例如,更改文件夹,硬code *。txt的代替%1 ,等等)。

The following batch file takes one command line parameter and must be run from the folder where the files to be renamed exist. You could easily add more code to the batch file to make it more intelligent (e.g., change to the folder, hard code *.txt in place of %1, etc.).

因此​​,如果批处理文件名为 fixfilenames.bat 并与TXT文件相同的文件夹,请在命令提示符处,键入 fixfilenames * .TXT 键,它会首先重命名文件的扩展名为 temp_txt ,使循环不会再次拿起文件。然后,当它完成,它重命名所有的 temp_txt 文件 TXT 文件。

So, if the batch file is called fixfilenames.bat and is in the same folder with the TXT files, from a command prompt, type fixfilenames *.txt and it will rename the files first to have the extension temp_txt so that the for loop won't pick up the files again after they are renamed. Then when it is done, it renames all temp_txt files to txt files.

@echo off

for %%i in (%1) do (
  for /f "tokens=2 delims==" %%j in ('findstr /B /I "Description=" "%%i"') do (
    ren "%%i" "%%j.temp_txt"
  )
)

ren *.temp_txt *.txt

这篇关于基于使用批处理文件内容重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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