+ Post New Thread
Results 1 to 12 of 12
Scripts Thread, Change a file name to something else by script in Coding and Web Development; 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...
  1. #1
    andy_nic's Avatar
    Join Date
    Jun 2008
    Location
    Peterborough
    Posts
    451
    Thank Post
    31
    Thanked 39 Times in 33 Posts
    Rep Power
    20

    Change a file name to something else by script

    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

  2. #2
    rh91uk's Avatar
    Join Date
    Sep 2008
    Location
    UK
    Posts
    877
    Thank Post
    137
    Thanked 132 Times in 114 Posts
    Rep Power
    36
    Are they all in the same directory?

  3. #3
    andy_nic's Avatar
    Join Date
    Jun 2008
    Location
    Peterborough
    Posts
    451
    Thank Post
    31
    Thanked 39 Times in 33 Posts
    Rep Power
    20
    yepo i managed to do a search and get them all in to 1 directory.

  4. #4
    andy_nic's Avatar
    Join Date
    Jun 2008
    Location
    Peterborough
    Posts
    451
    Thank Post
    31
    Thanked 39 Times in 33 Posts
    Rep Power
    20
    i forgot i also need to as a . in after the first 2 numbers

  5. #5

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    10,053
    Thank Post
    3,585
    Thanked 1,123 Times in 1,025 Posts
    Rep Power
    377
    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
    Last edited by mac_shinobi; 28th January 2010 at 09:55 AM.

  6. #6

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,159
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    125
    Quote Originally Posted by andy_nic View Post
    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>_something_<number>_<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?

  7. #7
    andy_nic's Avatar
    Join Date
    Jun 2008
    Location
    Peterborough
    Posts
    451
    Thank Post
    31
    Thanked 39 Times in 33 Posts
    Rep Power
    20
    Quote Originally Posted by srochford View Post
    Everything is possible with a script.

    Are all the names of the form <something>_something_<number>_<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

  8. #8

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,159
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    125
    OK; try this:
    Code:
    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 :-)

  9. Thanks to srochford from:

    andy_nic (28th January 2010)

  10. #9
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    5,007
    Thank Post
    124
    Thanked 286 Times in 263 Posts
    Rep Power
    109
    I like the bulk rename utility for this type of work.

  11. Thanks to ChrisH from:


  12. #10


    Join Date
    Mar 2008
    Location
    Surrey
    Posts
    2,317
    Thank Post
    131
    Thanked 355 Times in 293 Posts
    Blog Entries
    4
    Rep Power
    142
    Alternatively, if you have PS

    Code:
    $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)
    {
    
    #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
    }

  13. #11
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    5,007
    Thank Post
    124
    Thanked 286 Times in 263 Posts
    Rep Power
    109
    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???

  14. #12
    andy_nic's Avatar
    Join Date
    Jun 2008
    Location
    Peterborough
    Posts
    451
    Thank Post
    31
    Thanked 39 Times in 33 Posts
    Rep Power
    20
    @ srochford, thank you for that saved me changing 120 manually. Also will come in handy later on too.



SHARE:
+ Post New Thread

Similar Threads

  1. Script for Mass Password change
    By Samson in forum Windows
    Replies: 7
    Last Post: 2nd October 2008, 03:50 PM
  2. Change File Association
    By Newton in forum Windows
    Replies: 9
    Last Post: 11th July 2008, 09:46 PM
  3. Script to change folder permissions
    By scottyses in forum General Chat
    Replies: 1
    Last Post: 15th January 2008, 09:52 AM
  4. Script To Change A Registry Key
    By DaveP in forum Windows
    Replies: 6
    Last Post: 7th March 2007, 01:57 PM
  5. Replies: 4
    Last Post: 23rd March 2006, 07:27 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •