none
How do I extract part of a url? RRS feed

  • Question

  • In Powershell...if I have a url like this http://www.domain.com/folder1/thisfile.aspx. 

    How do I go about stripping this down? I basically want to take out the http://, then change all the slashes ("/") into underscores ("_"). My goal is to dynamically create a file name from a site url. Please help. 

    Wednesday, April 2, 2014 4:20 PM

Answers

  • Here's a procedural starting point that may help. First we set the variable to your URL, replace the http:// with nothing, and finally replace the forward slashes with underscores. Hope this is helpful.

    $URL = 'http://www.domain.com/folder1/thisfile.aspx'
    $URL = $URL.Replace('http://','')
    $URL = $URL.Replace('/','_')

    This is the same example, but less procedural (done as one step instead of multiple steps).

    $URL = (('http://www.domain.com/folder1/thisfile.aspx').Replace('http://','')).Replace('/','_')
    Edit: Added 2nd example.



    • Proposed as answer by Mike Laughlin Wednesday, April 2, 2014 4:29 PM
    • Edited by tommymaynard Wednesday, April 2, 2014 4:31 PM
    • Marked as answer by Spawn10 Wednesday, April 2, 2014 6:24 PM
    Wednesday, April 2, 2014 4:27 PM

All replies

  • Here's a procedural starting point that may help. First we set the variable to your URL, replace the http:// with nothing, and finally replace the forward slashes with underscores. Hope this is helpful.

    $URL = 'http://www.domain.com/folder1/thisfile.aspx'
    $URL = $URL.Replace('http://','')
    $URL = $URL.Replace('/','_')

    This is the same example, but less procedural (done as one step instead of multiple steps).

    $URL = (('http://www.domain.com/folder1/thisfile.aspx').Replace('http://','')).Replace('/','_')
    Edit: Added 2nd example.



    • Proposed as answer by Mike Laughlin Wednesday, April 2, 2014 4:29 PM
    • Edited by tommymaynard Wednesday, April 2, 2014 4:31 PM
    • Marked as answer by Spawn10 Wednesday, April 2, 2014 6:24 PM
    Wednesday, April 2, 2014 4:27 PM
  • Many thanks tommymaynard...that was exactly what I needed.
    Wednesday, April 2, 2014 6:25 PM
  • You're very welcome!
    Wednesday, April 2, 2014 7:53 PM