Solved

renaming files based on the content of the file

Posted on 2011-03-10
4
340 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article will help you understand what HashTables are and how to use them in PowerShell.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

10 Experts available now in Live!

Get 1:1 Help Now