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

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!!!!!!

Ganesh VijaykumarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ganesh VijaykumarAuthor Commented:
@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..!!!!!!
you're welcome!
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

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]
Ganesh VijaykumarAuthor Commented:

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.
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) *
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.