Solved

renaming files based on the content of the file

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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
A brief introduction to what I consider to be the best editor for PowerShell.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

816 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

8 Experts available now in Live!

Get 1:1 Help Now