Solved

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

Posted on 2014-11-17
6
396 Views
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
cls
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!!!!!!

Thanks,
Ganesh
0
Comment
Question by:Ganesh Vijaykumar
  • 2
  • 2
  • 2
6 Comments
 
LVL 9

Accepted Solution

by:
dlb6597 earned 500 total points
Comment Utility
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

0
 

Author Closing Comment

by:Ganesh Vijaykumar
Comment Utility
@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..!!!!!!
0
 
LVL 9

Expert Comment

by:dlb6597
Comment Utility
you're welcome!
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 24

Expert Comment

by:SunBow
Comment Utility
For: Ganesh Vijaykumar, Ganesh006, Member Since: 2014-11-17
History: three questions asked

Re:
Selecting all the check boxes excluding #N/A and Blank under Row labels in a pivot table
Neglected
http://www.experts-exchange.com/Other/Miscellaneous/Q_28585208.html
 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

[Explains]
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]
0
 

Author Comment

by:Ganesh Vijaykumar
Comment Utility
Hi,

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.
G
0
 
LVL 24

Expert Comment

by:SunBow
Comment Utility
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) *
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now