Solved

renaming files based on the content of the file

Posted on 2011-03-10
4
341 Views
Last Modified: 2012-05-11
We've had an SD Card currupt with CNC program data (text files).  I've recovered the data using power quest data recovery and recovered most of the data.  However, all the files are names sequential from 1 to 1000.  I want to rename these files based on the first item on the second line
of the contents of the files.  The first few lines of a program are located in the code window

As you can see the file always starts with a % sign.  The second line starts with the program number which is a letter "O" and a 4 digdit number.  I want the file renamed to the "O1118". Any ideas how this can be performed?
% 
O1118(DRG 24778) 
(DRILL & C/S/K)
(SET X +HALF EXCESS) 
(SET Y+ HALF EXCESS)

Open in new window

0
Comment
Question by:resolver1
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
prashanthd earned 500 total points
ID: 35094263
Hi,

Try the following code, modify the objStartFolder to the recovered files folder
On Error Resume Next

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\recovered" 'folder path

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files
For Each objFile In colFiles
    Set textfile=objfso.OpenTextFile(objfile.Path,1)
    textfile.ReadLine
    newfile_name=Mid(textfile.ReadLine,1,5) & ".txt"
    textfile.Close
    newfile_path=Replace(LCase(objfile.Path),LCase(objfile.Name),LCase(newfile_name))
    objFSO.MoveFile objfile.Path , newfile_path
Next

Open in new window

0
 
LVL 2

Expert Comment

by:khaaz
ID: 35094418
this should do the job, just pass the folder path as an argument, it doesn't rename the files but copy them with the first item of the second line as the new name
Set ObjArgs=Wscript.arguments

If Objargs.count <> 1 then
	Wscript.echo "Param : ""Sourcefolder"" "
	Wscript.quit
	Else
	Strfolder=ObjArgs(0)
	
End if



Set wshshell=createobject("wscript.shell")
Set fso=createobject("scripting.filesystemobject")

set foldatraiter=fso.getfolder(strfolder)






Sub traitFolder(objFolder)
	set fColl=ObjFolder.Subfolders
	For Each oFolder in FColl
		TRaitFolder(oFolder)
	NExt
	Set collfiles=objFolder.files
	For each Objfile in CollFiles
		TraitFile(objFile)	
	Next
End sub

Sub TraitFile(OFile)
	
	StrShortName=Ofile.name
	StrLongName=Ofile.path


			
			SEt tsFile=OFile.Openastextstream(1)
			Firstline=TsFile.readline
			Secondline=TsFile.readline
			Tabline=split(secondline,"(")
			Firstitem=Tabline(0)
			Tsfile.close
			
			FSo.copyfile StrLongName,replace(strlongname,strshortname,firstitem)
			
			

End Sub

TRaitFolder(FoldAtraiter)

Open in new window

0
 
LVL 2

Expert Comment

by:khaaz
ID: 35094516
now you have two scripts :)
parshanthd rename the file with the 5 first characters found on the second line
mine rename the file with the characters found before the first "("

choose wisely ;)
0
 

Author Comment

by:resolver1
ID: 35107155
I used the prashanthd method and it worked a dream.  Thanks Khaaz, his code looked a little easier to understand.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
exchange, scripts 30 77
SQL syntax in VB.net 5 30
Global Address List will not update via Powershell or EAC. 2 39
change the windows script file to BAT 10 30
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

22 Experts available now in Live!

Get 1:1 Help Now