Renaming multiple text files based on their contents- Batch File/VB Script

Posted on 2014-11-17
Medium Priority
Last Modified: 2014-12-23
I am trying to rename a series of .txt files in a directory according to a specific phrase/text in each given text file. Put differently and more specifically, I have 100 text files with arbitrary or random names but within each file is a unique phrase/text (something like: GEMSAM_IN_USD_GEMSIT_MKE/ GEMSAM_IN_USD_IT_USA_DATEX_LAU/ GEMSAM_IN_USD_GEMSIT_DTJ). I would like to replace the arbitrary file name with the **desired** file name.

For E.G. When I open the text file and find text "GEMSAM_IN_USD_GEMSIT_MKE" the file name should be renamed as MKE.txt or if I find "GEMSAM_IN_USD_IT_USA_DATEX_LAU" file name should be LAU.txt. The text/phrase is static and will not change.

I used the below mentioned syntax but nothing happened and I am clueless, and this is my first real foray into batch programming so the syntax is fairly new to me.

@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%1 in ('type 502236373.123456.txt ^| find "GEMSAS_IN_JPY_DTYMS_JAPAN"') do (
    set line=%%1&set line=!line:~1!

copy C:\Users\gk814256\Desktop\Docstore Test\new.txt

I am looking for Batch file or VB script or anything that meets my exact needs.

Any suggestions would be greatly appreciated!

I am looking forward for the positive response!!!!!!

Question by:Ganesh Vijaykumar
  • 2
  • 2
  • 2

Accepted Solution

dlb6597 earned 2000 total points
ID: 40448017
not sure how many different phrase/desired filename pairs you have, but...

save the following code as a batch file and run it providing one of the phrases as a parameter...it will search all .txt files in the folder where you run it and find the phrase provided...if the phrase exists in a file, it will rename the file to the last three characters of the phrase with a .txt extension.  Obviously this assumes that the provided phrase only exists in a single txt file...

set nametmp=%1
set name=%nametmp:~-3%
for %%i in (*.txt) do Find "%1" %%i && ren %%i %name%.txt

Open in new window


Author Closing Comment

by:Ganesh Vijaykumar
ID: 40448313
@dlb6597 : Thanks a ton for the solution!!!!  This code worked like a gem... You are "Rock star" in my books and thanks again for making my day..!!!!!!

Expert Comment

ID: 40448323
you're welcome!
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 24

Expert Comment

ID: 40515224
For: Ganesh Vijaykumar, Ganesh006, Member Since: 2014-11-17
History: three questions asked

Selecting all the check boxes excluding #N/A and Blank under Row labels in a pivot table
 Ganesh Vijaykumar
Topics: Kernel And Operating System Specific Programming ,Miscellaneous
Participating Experts 0
Comments 0

° Hint:
You appear to be relatively new to experience in EE
I recommend you be more attentive in selecting topics (up to three, where 1st is main target) to getting best responses freely from those with precious available time
For example, observe difference (including order of attention/focus/experience/reward - recommending):

Topics: VB Script , Microsoft Excel Spreadsheet Software , Miscellaneous

At this time, with no responses yet, you should be able to make a change yourself
Some (self included) may post similar comment, within that question, that you move it to other TA
Once they/we do that, you have lost control of editing initial question
At that point you may ask new (same) question in other TAs, and delete original
While you are in process of doing that, someone else may be providing comment of value, worth either some partial points or even a complete answer, in initial question
I for one would not want to thus eliminate comments of others, even by accident

As such, I am posting this comment in a prior question of yours that is closed, soley to enable your attention without interfering with the process
On that note, welcome to EE and,

** Good Luck **

» Footnote:
You do not 'have to' provide all points to single expert, you may divide as you see fit.
For example, you may provide 400 to final best answer, and distribute 100 to those who helped to derive it. Or for example, 250 to 'best' overall answer, and divide 250 to other comments (including expert who had best answer who also contributed other comments considered of value)
¤ Note: doing so not only helps to identify/(reward) contributors, it also helps members (including you and I and future) to search this website for prior work on similar future questions

§ [and down the road when you get the time, you may try a hand at replying to questions of others, and formatting options]

Author Comment

by:Ganesh Vijaykumar
ID: 40515437

Yes am relatively new to EE. As u suggested I edited the topics and submitted request for attention... Hopefully I will get a response....

Thanks a ton for your help.
LVL 24

Expert Comment

ID: 40515617
er, and thanks for acknowledging
while we at times can seem more (too) 'competitive', we are also a community
In quick review, as it stands, It is now gone from my prior display (miscellaneous)
Found it:
Topics VB Script ,Microsoft Office Suite
Agree that is improvement.
You do get up to three
At this point rather than add Excel, I'd recommend adding Miscellaneous
Reasoning, the suite may attract the Excel experts (& VBA & VBS)
While any who already noticed it in Miscellaneous (wider audience) may be bewildered by its disappearance
One may already have begun a response
As for the 'request for attention'  - I just dunno, insufficient experience, and EE has been making some changes (enhancements)
It would typically be used for another to flag as potential undesirable question or comment, or (I presume) assistance if you'd received some comments, and then decided that all would fare better if 'moved' to other TA (eg 'help me!) in middle, midstream of thread of comments
Starting dialogue with 'moderators' 'could' make it seem initially confusing (less stable?) to potential contributors
But now methinks it too late, I suspect no way to unflag. Maybe you've some intent to get more TA suggestions, and that would work out.
On that note, one last suggestion,

When I want to submit question, I first goto TA that I think best for attracting reasonable replies. Then click on Solve (?), then on 'ask question. This provides that TA as initial/default one for placement, and another two can be added (or not) for focus.

More personally, I am more slower in typing/resolving, would be more likely to do misc, then excel, then vba/vbs when taking time-out to reply with 'answers', since others may already have a script ready to copy|paste, question answered and closed before I am half done. So it is unlikely I'll learn more about how this progresses, I am not moderator, and have other interests to attend to (including the 'other' TAs - admittedly on my history, and now think it good time to seek out some food). On that note, good luck, may the force be with you, and have

* Happy Holiday(s) *

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question