Solved

Rename files which contain specific data

Posted on 2013-11-27
5
370 Views
Last Modified: 2013-12-02
I have a script which will look for specific data.
When it finds the specific data, it will create a separate file for each instance of that data found.

What I want to do is to be able to rename the newly created files based on criteria.

The bat file newscript.bat runs the split.vbs and then runs other commands.

The newscript.bat looks similar to this:

cscript.exe split.vbs


pushd "C:\Users\user\Desktop\Files"

if exist *.edi findstr "1234" *.ed >>test.txt || del test.txt
if exist *.edi findstr "5678" *.ed >>test2.txt || del test2.txt
if exist *.edi findstr "9101112" *.ed >>test3.txt || del test3.txt

popd



The split.vbs looks like this:

Set fso = CreateObject("Scripting.FileSystemObject")

data = Split(fso.OpenTextFile("output.txt").ReadAll, "IBA*00*")
For i = 1 To UBound(data)
  fso.OpenTextFile("new" & i & ".ed", 2, True).Write "IBA*00*" & data(i)
Next

 
What I would like to do is instead of:
if exist *.edi findstr "1234" *.ed >>test.txt || del test.txt

I would like to instruct it so do something similar to this:

if exist *.ed findstr "1234" *.ed ... then just rename the file that you found the "1234" in as
importantfile.ed

and instead of:

if exist *.ed findstr "5678" *.ed >>test2.txt || del test2.txt
I would like to instruct it to do something similar to this:
if exist *.ed findstr "5678" *.ed ... then just rename the file that you found the "5678" in as
anotherfilename.ed
0
Comment
Question by:100questions
  • 4
5 Comments
 
LVL 3

Expert Comment

by:telczj9
ID: 39682541
Are you expecting to find "5678" in only one file or multiple files?

If it is multiple files how would you like to rename the extra occurrences?

Cheers!
0
 

Author Comment

by:100questions
ID: 39682583
I am expecting to find "5678" only in 1 file.
0
 

Accepted Solution

by:
100questions earned 0 total points
ID: 39682595
setlocal enabledelayedexpansion

for %%a in (C:\Temp\*.txt) do (
  find "prov1" "%%~fa" && ren "%%~fa" newfile1.txt
  find "prov2" "%%~fa" && ren "%%~fa" newfile2.txt
  find "prov3" "%%~fa" && ren "%%~fa" newfile3.txt
  find "prov4" "%%~fa" && ren "%%~fa" newfile4.txt
)
0
 

Author Comment

by:100questions
ID: 39683311
The script works but it does not loop through any multiple files.  I need a more complete script.
0
 

Author Closing Comment

by:100questions
ID: 39689474
Worked for me.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
How to record audio from input sources to your PC – connected devices, connected preamp to record vinyl discs, streaming media, that play through your audio card: Vista, Windows 7, Windows 8, Windows 8.1 and Windows 10 – both 32 bit & 64.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

895 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

14 Experts available now in Live!

Get 1:1 Help Now