Create folder for entries in text file, and generate url.

Welcome Forums General PowerShell Q&A Create folder for entries in text file, and generate url.

Viewing 3 reply threads
  • Author
    Posts
    • #163179
      Participant
      Topics: 8
      Replies: 17
      Points: 127
      Rank: Participant

      I am working on a refinement for a process – we pull a text file with a list of dates.

      Each date gets its own folder, and each date is used in a URL after to pull data for that date.

      EX: List of dates is

      2019-03-31.957
      2019-03-30.956
      2019-03-29.955

      We then create a directory for each

      mkdir 2019-03-31.957
      mkdir 2019-03-30.956
      mkdir 2019-03-29.955

      And put the URL for each after.

      ./curl.exe –url “https://some/url/date=2019-03-31.957 | out-file -encoding utf8 “F:\output\2019-03-31.957″

       

       

      What I want to do, however, is pull from the text file the list of dates, create a folder for each, and then have the URL generate according to that list as well. I know I cannot do this with a static variable, and may possibly use a foreach loop, but this is way beyond me. Any advice or help is appreciated!

       

       

      -edit

      I have found something similar, and it looks like I can tweak it.

      However, I’m definitely stumped on creating the unique URL using each line as a variable.

      $folder=”C:\scripts\reporter\reports”;
      $datelist=”.\uniquedates.txt”;
      get-content $datelist | %{
      {
      mkdir “$folder\$_”;
      }
      }

    • #163182
      Participant
      Topics: 1
      Replies: 64
      Points: 363
      Helping Hand
      Rank: Contributor
      $textDate=(get-content MyListofDates.txt)
      
      foreach ($d in $textDate){
      new-item -itemtype directory $d
      ./curl.exe –url "https://some/url/$d" | out-file -encoding utf8 "F:\output\$d"
      
      }
    • #163185
      Participant
      Topics: 8
      Replies: 17
      Points: 127
      Rank: Participant
      PowerShell
      7 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 17.6px; width: 6.59775px; left: 215.542px; top: 35.2px;” spellcheck=”false” wrap=”off”></textarea>

      1
      2
      3
      4
      5
      6
      7
      $textDate=(get-content MyListofDates.txt)
      foreach ($d in $textDate){
      new-item itemtype directory $d
      ./curl.exeurl https://some/url/$d&#8221; | out-file encoding utf8 “F:\output\$d”
      }
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

       

      You’re made of magic, senor.

      I was just editing in my proposed solution for the folders, but the URL problem was beyond me.

      Is the $d variable powershell logic, or does it need to be predefined?

       

      -edit, answered my own question. $d is a shorthand variable name for the built-in $date variable. Nice. Thanks again!

       

      • #163188
        Participant
        Topics: 1
        Replies: 64
        Points: 363
        Helping Hand
        Rank: Contributor

        You’re more than welcome.

        When I was first learning powershell the foreach loop was like a watershed moment for me.  It just made more sense after that.

        Edit to add:

        The $d has nothing to do with a built in $date.

        Before the foreach loop we build a variable ($textDate) with content from your text file.  (you can type in $textdate.count to see how many elements are in it).  Then in the foreach loop we tell it for every item ($d) in $textdate do {something}.

         

        I could have also called it foreach ($potato in $textDate){write-host $potato} to get the same effect.

    • #163191
      Participant
      Topics: 8
      Replies: 17
      Points: 127
      Rank: Participant

      I might have yelled “Eureka” out loud in the office when I modified it, ran it, and it started doing things automatically.

      Theoretically.

       

      And – okay, so the logic I’m seeing is that you are telling it, and it understands regardless of the name, that for each $item in $mytextfile , do {thing }

Viewing 3 reply threads
  • The topic ‘Create folder for entries in text file, and generate url.’ is closed to new replies.