Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 349
  • Last Modified:

renaming files based on the content of the file

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
resolver1
Asked:
resolver1
  • 2
1 Solution
 
prashanthdCommented:
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
 
khaazCommented:
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
 
khaazCommented:
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
 
resolver1Author Commented:
I used the prashanthd method and it worked a dream.  Thanks Khaaz, his code looked a little easier to understand.
0

Featured Post

Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now