Solved

renaming files based on the content of the file

Posted on 2011-03-10
4
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

630 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