Solved

Rename files which contain specific data

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Deploying Windows 10 in MDT 18 67
Please help me Blue screen window 10 7 57
Undelete of overwritten files 27 81
Problem to App source 6 33
NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

808 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