[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Rename files which contain specific data

Posted on 2013-11-27
5
Medium Priority
?
438 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Most folks would know the basics of how Dropbox works, so that’s not the purpose of this article. Security is what it’s all about, so here I’ll share how I choose to secure my Dropbox Account and the Data it contains.
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

873 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