andy_nic Posted January 28, 2010 Posted January 28, 2010 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
andy_nic Posted January 28, 2010 Author Posted January 28, 2010 yepo i managed to do a search and get them all in to 1 directory.
andy_nic Posted January 28, 2010 Author Posted January 28, 2010 i forgot i also need to as a . in after the first 2 numbers
mac_shinobi Posted January 28, 2010 Posted January 28, 2010 (edited) File System Management with VBScript You would most likely have to include a counter inside of the for loop so that it could ammend that to the file name but that shouldn't be too complex Edited January 28, 2010 by mac_shinobi
srochford Posted January 28, 2010 Posted January 28, 2010 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?
andy_nic Posted January 28, 2010 Author Posted January 28, 2010 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
srochford Posted January 28, 2010 Posted January 28, 2010 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 :-) 1
ChrisH Posted January 28, 2010 Posted January 28, 2010 I like the bulk rename utility for this type of work. 1
jamesb Posted January 28, 2010 Posted January 28, 2010 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] }
ChrisH Posted January 28, 2010 Posted January 28, 2010 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???
andy_nic Posted January 28, 2010 Author Posted January 28, 2010 @ srochford, thank you for that saved me changing 120 manually. Also will come in handy later on too.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now