Solved

Rename files which contain specific data

Posted on 2013-11-27
5
380 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
Learn how to PXE Boot both BIOS & UEFI machines with DHCP Policies and Custom Vendor Classes
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

828 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