Jump to content

Change a file name to something else by script


Recommended Posts

Posted
I have files called ox_sw2_123_sp.pdf, I want to them become Textbook 123.pdf

Is this possible using a scripts?

 

Thanks Andy

 

Everything is possible with a script.

 

Are all the names of the form _something__.pdf and you want to extract just the number and stick "textbook" in front of it? Are there always going to be 2 underscores before the number?

Posted
Everything is possible with a script.

 

Are all the names of the form _something__.pdf and you want to extract just the number and stick "textbook" in front of it? Are there always going to be 2 underscores before the number?

 

 

Yeah there’s always 2 underscores before it, also need to stick a dot after the first 2 numbers, but now I’ve got the text book in and removed everything else so I have textbook123 I just need to insert a dot in so it will be textbook 12.3 also there are always all the same number of characters. I was thinking about script to count so many then insert a dot in.

 

Andy

Posted

OK; try this:

sFolder="c:\temp\test\"
set oFSO=createobject("scripting.filesystemobject")
set oFolder=ofso.getfolder(sFolder)
for each oFile in oFolder.files
 sExt=lcase(ofso.getextensionname(oFile))
 if sExt="pdf" then'it is a PDF file, get more info
   sName=lcase(ofso.getbasename(oFile)) 'get the name without path and extension
   sNameBits=split(sName,"_") 'split the name into parts using the _ as separator
   sNumber=sNameBits(2) 'count from zero so the number will be item number 2 in the array
   sStart=left(sNumber,len(sNumber)-1) 'take everything but the last digit (eg 123 gives 12; 95 gives 9)
   sEnd=right(sNumber,1) 'and take the last digit
   sNewName="Textbook" & sStart & "." & sEnd & ".pdf" 'build the new name
   oFSO.movefile sFolder & oFile.name, sFolder & sNewName 'rename it
 end if
next

 

I've added some comments so I hope it makes sense but shout if not :-)

  • Thanks 1
Posted

Alternatively, if you have PS

 

$Path = 'Directory\'

#Get all pdfs in the directory
$Filenames = Get-ChildItem $Path -Recurse -Include "*.pdf"

#Loop through one at a time
foreach ($OldFile in $Filenames)
{


[indent]#Replace all non-numerics
$Number = $OldFile.Name -replace '[^0-9]'

#Construct the new filename
$NewName = "Textbook " + $Number.Insert(2,'.') + ".pdf"

#Rename the file
Rename-Item -Path ($Path + $OldFile.Name) -NewName $NewName
[/indent]

}

Posted

Here is the regex for the match. Not pretty but it works

 

(^.+_.+_)(.{2})(.+)(_.+)

 

And here is the replace string.

 

Textbook \2.\3

 

So this turns something in the format of:

 

ox_sw2_123_sp.pdf

 

to

 

Textbook 12.3.pdf

 

Are you sure you want that extra dot???

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...