PowerShell read csv file and create files accordingly











up vote
0
down vote

favorite












Basically, I have a CSV file with a lot of columns. Let's say it looks like this:



_username, platform_

username1, platformX1

username2, platformY

username3, platformX2

username4, platformX2

..., ...


I would like to write a script that goes through the file and for each platform, it creates a file with the specific username in a different folder, so I would have:



platformX1username1.file

platformYusername2.file

platformX2username3.file, username4.file

etc etc...


I know I should use foreach with an if placed somewhere, but powershell is new for me, and I don't really know how to start it.










share|improve this question




















  • 1




    Please format your code and sample input/output properly.
    – mklement0
    Nov 21 at 19:40










  • Your queston may have an answer here
    – Walter Mitty
    Nov 22 at 3:57















up vote
0
down vote

favorite












Basically, I have a CSV file with a lot of columns. Let's say it looks like this:



_username, platform_

username1, platformX1

username2, platformY

username3, platformX2

username4, platformX2

..., ...


I would like to write a script that goes through the file and for each platform, it creates a file with the specific username in a different folder, so I would have:



platformX1username1.file

platformYusername2.file

platformX2username3.file, username4.file

etc etc...


I know I should use foreach with an if placed somewhere, but powershell is new for me, and I don't really know how to start it.










share|improve this question




















  • 1




    Please format your code and sample input/output properly.
    – mklement0
    Nov 21 at 19:40










  • Your queston may have an answer here
    – Walter Mitty
    Nov 22 at 3:57













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Basically, I have a CSV file with a lot of columns. Let's say it looks like this:



_username, platform_

username1, platformX1

username2, platformY

username3, platformX2

username4, platformX2

..., ...


I would like to write a script that goes through the file and for each platform, it creates a file with the specific username in a different folder, so I would have:



platformX1username1.file

platformYusername2.file

platformX2username3.file, username4.file

etc etc...


I know I should use foreach with an if placed somewhere, but powershell is new for me, and I don't really know how to start it.










share|improve this question















Basically, I have a CSV file with a lot of columns. Let's say it looks like this:



_username, platform_

username1, platformX1

username2, platformY

username3, platformX2

username4, platformX2

..., ...


I would like to write a script that goes through the file and for each platform, it creates a file with the specific username in a different folder, so I would have:



platformX1username1.file

platformYusername2.file

platformX2username3.file, username4.file

etc etc...


I know I should use foreach with an if placed somewhere, but powershell is new for me, and I don't really know how to start it.







powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 19:55









kit

1,1082516




1,1082516










asked Nov 21 at 19:33









raten

1




1








  • 1




    Please format your code and sample input/output properly.
    – mklement0
    Nov 21 at 19:40










  • Your queston may have an answer here
    – Walter Mitty
    Nov 22 at 3:57














  • 1




    Please format your code and sample input/output properly.
    – mklement0
    Nov 21 at 19:40










  • Your queston may have an answer here
    – Walter Mitty
    Nov 22 at 3:57








1




1




Please format your code and sample input/output properly.
– mklement0
Nov 21 at 19:40




Please format your code and sample input/output properly.
– mklement0
Nov 21 at 19:40












Your queston may have an answer here
– Walter Mitty
Nov 22 at 3:57




Your queston may have an answer here
– Walter Mitty
Nov 22 at 3:57












3 Answers
3






active

oldest

votes

















up vote
0
down vote













Here's something similar tweaked to what you want to do.



This created files of a specific size in your directory



$data = get-content "C:DatamasteFile.csv"
$drcty = "C:Data"

foreach($line in $data)
{
$a,$b = $line.Split("{,}")
$parent = $drcty+""+$a+""
$filename = $parent + $b.TRIM() +".txt"
write-host $filename

New-Item -ItemType directory -Path $parent

fsutil file createnew $filename 2000
}





share|improve this answer




























    up vote
    0
    down vote













    this seems to do what you want. [grin]



    # fake reading in a CSV file
    # in real life, use Import-CSV
    $InStuff = @'
    _UserName, Platform_
    username1, platformX1
    username2, platformY
    username3, platformX2
    username4, platformX2
    '@ | ConvertFrom-Csv

    $DestDir = $env:TEMP
    $Extension = 'txt'

    foreach ($IS_Item in $InStuff)
    {
    $TargetPath = Join-Path -Path $DestDir -ChildPath $IS_Item.Platform_
    if (-not (Test-Path -LiteralPath $TargetPath))
    {
    # "$Null = " will supress unwanted output from New-Item
    $Null = New-Item -Path $TargetPath -ItemType Directory
    }

    $FileName = $IS_Item._UserName, $Extension -join '.'
    $FullFileName = Join-Path -Path $TargetPath -ChildPath $FileName

    if (-not (Test-Path -LiteralPath $FullFileName))
    {
    # "$Null = " will supress unwanted output from New-Item
    $Null = New-Item -Path $FullFileName -ItemType File
    }
    }


    i think what it does is apparent, but i know i get ahead of where i otta be at times. so, if there are any questions, please feel free to ask ... [grin]






    share|improve this answer




























      up vote
      0
      down vote













      This worked for me.



      $ErrorActionPreference = "Stop"

      #Path of the CSV file resides
      $csvData = Import-Csv -Path "$env:USERPROFILEDesktopdata.csv"
      $DestinationFolder = "C:Stuffs"

      foreach($record in $csvData)
      {
      $destPlatformFolder = $DestinationFolder + "" + $record.platform_

      if(-not(Test-Path -Path $destPlatformFolder)){

      New-Item -Path $destPlatformFolder -ItemType Directory -Force | Out-Null

      }

      $destinationFile = $destPlatformFolder + "" + $record._username

      New-Item -Path $destinationFile -ItemType File -Force | Out-Null
      }





      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419348%2fpowershell-read-csv-file-and-create-files-accordingly%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        0
        down vote













        Here's something similar tweaked to what you want to do.



        This created files of a specific size in your directory



        $data = get-content "C:DatamasteFile.csv"
        $drcty = "C:Data"

        foreach($line in $data)
        {
        $a,$b = $line.Split("{,}")
        $parent = $drcty+""+$a+""
        $filename = $parent + $b.TRIM() +".txt"
        write-host $filename

        New-Item -ItemType directory -Path $parent

        fsutil file createnew $filename 2000
        }





        share|improve this answer

























          up vote
          0
          down vote













          Here's something similar tweaked to what you want to do.



          This created files of a specific size in your directory



          $data = get-content "C:DatamasteFile.csv"
          $drcty = "C:Data"

          foreach($line in $data)
          {
          $a,$b = $line.Split("{,}")
          $parent = $drcty+""+$a+""
          $filename = $parent + $b.TRIM() +".txt"
          write-host $filename

          New-Item -ItemType directory -Path $parent

          fsutil file createnew $filename 2000
          }





          share|improve this answer























            up vote
            0
            down vote










            up vote
            0
            down vote









            Here's something similar tweaked to what you want to do.



            This created files of a specific size in your directory



            $data = get-content "C:DatamasteFile.csv"
            $drcty = "C:Data"

            foreach($line in $data)
            {
            $a,$b = $line.Split("{,}")
            $parent = $drcty+""+$a+""
            $filename = $parent + $b.TRIM() +".txt"
            write-host $filename

            New-Item -ItemType directory -Path $parent

            fsutil file createnew $filename 2000
            }





            share|improve this answer












            Here's something similar tweaked to what you want to do.



            This created files of a specific size in your directory



            $data = get-content "C:DatamasteFile.csv"
            $drcty = "C:Data"

            foreach($line in $data)
            {
            $a,$b = $line.Split("{,}")
            $parent = $drcty+""+$a+""
            $filename = $parent + $b.TRIM() +".txt"
            write-host $filename

            New-Item -ItemType directory -Path $parent

            fsutil file createnew $filename 2000
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 at 20:03









            Ivhani Maselesele

            212




            212
























                up vote
                0
                down vote













                this seems to do what you want. [grin]



                # fake reading in a CSV file
                # in real life, use Import-CSV
                $InStuff = @'
                _UserName, Platform_
                username1, platformX1
                username2, platformY
                username3, platformX2
                username4, platformX2
                '@ | ConvertFrom-Csv

                $DestDir = $env:TEMP
                $Extension = 'txt'

                foreach ($IS_Item in $InStuff)
                {
                $TargetPath = Join-Path -Path $DestDir -ChildPath $IS_Item.Platform_
                if (-not (Test-Path -LiteralPath $TargetPath))
                {
                # "$Null = " will supress unwanted output from New-Item
                $Null = New-Item -Path $TargetPath -ItemType Directory
                }

                $FileName = $IS_Item._UserName, $Extension -join '.'
                $FullFileName = Join-Path -Path $TargetPath -ChildPath $FileName

                if (-not (Test-Path -LiteralPath $FullFileName))
                {
                # "$Null = " will supress unwanted output from New-Item
                $Null = New-Item -Path $FullFileName -ItemType File
                }
                }


                i think what it does is apparent, but i know i get ahead of where i otta be at times. so, if there are any questions, please feel free to ask ... [grin]






                share|improve this answer

























                  up vote
                  0
                  down vote













                  this seems to do what you want. [grin]



                  # fake reading in a CSV file
                  # in real life, use Import-CSV
                  $InStuff = @'
                  _UserName, Platform_
                  username1, platformX1
                  username2, platformY
                  username3, platformX2
                  username4, platformX2
                  '@ | ConvertFrom-Csv

                  $DestDir = $env:TEMP
                  $Extension = 'txt'

                  foreach ($IS_Item in $InStuff)
                  {
                  $TargetPath = Join-Path -Path $DestDir -ChildPath $IS_Item.Platform_
                  if (-not (Test-Path -LiteralPath $TargetPath))
                  {
                  # "$Null = " will supress unwanted output from New-Item
                  $Null = New-Item -Path $TargetPath -ItemType Directory
                  }

                  $FileName = $IS_Item._UserName, $Extension -join '.'
                  $FullFileName = Join-Path -Path $TargetPath -ChildPath $FileName

                  if (-not (Test-Path -LiteralPath $FullFileName))
                  {
                  # "$Null = " will supress unwanted output from New-Item
                  $Null = New-Item -Path $FullFileName -ItemType File
                  }
                  }


                  i think what it does is apparent, but i know i get ahead of where i otta be at times. so, if there are any questions, please feel free to ask ... [grin]






                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    this seems to do what you want. [grin]



                    # fake reading in a CSV file
                    # in real life, use Import-CSV
                    $InStuff = @'
                    _UserName, Platform_
                    username1, platformX1
                    username2, platformY
                    username3, platformX2
                    username4, platformX2
                    '@ | ConvertFrom-Csv

                    $DestDir = $env:TEMP
                    $Extension = 'txt'

                    foreach ($IS_Item in $InStuff)
                    {
                    $TargetPath = Join-Path -Path $DestDir -ChildPath $IS_Item.Platform_
                    if (-not (Test-Path -LiteralPath $TargetPath))
                    {
                    # "$Null = " will supress unwanted output from New-Item
                    $Null = New-Item -Path $TargetPath -ItemType Directory
                    }

                    $FileName = $IS_Item._UserName, $Extension -join '.'
                    $FullFileName = Join-Path -Path $TargetPath -ChildPath $FileName

                    if (-not (Test-Path -LiteralPath $FullFileName))
                    {
                    # "$Null = " will supress unwanted output from New-Item
                    $Null = New-Item -Path $FullFileName -ItemType File
                    }
                    }


                    i think what it does is apparent, but i know i get ahead of where i otta be at times. so, if there are any questions, please feel free to ask ... [grin]






                    share|improve this answer












                    this seems to do what you want. [grin]



                    # fake reading in a CSV file
                    # in real life, use Import-CSV
                    $InStuff = @'
                    _UserName, Platform_
                    username1, platformX1
                    username2, platformY
                    username3, platformX2
                    username4, platformX2
                    '@ | ConvertFrom-Csv

                    $DestDir = $env:TEMP
                    $Extension = 'txt'

                    foreach ($IS_Item in $InStuff)
                    {
                    $TargetPath = Join-Path -Path $DestDir -ChildPath $IS_Item.Platform_
                    if (-not (Test-Path -LiteralPath $TargetPath))
                    {
                    # "$Null = " will supress unwanted output from New-Item
                    $Null = New-Item -Path $TargetPath -ItemType Directory
                    }

                    $FileName = $IS_Item._UserName, $Extension -join '.'
                    $FullFileName = Join-Path -Path $TargetPath -ChildPath $FileName

                    if (-not (Test-Path -LiteralPath $FullFileName))
                    {
                    # "$Null = " will supress unwanted output from New-Item
                    $Null = New-Item -Path $FullFileName -ItemType File
                    }
                    }


                    i think what it does is apparent, but i know i get ahead of where i otta be at times. so, if there are any questions, please feel free to ask ... [grin]







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 at 20:34









                    Lee_Dailey

                    1,15776




                    1,15776






















                        up vote
                        0
                        down vote













                        This worked for me.



                        $ErrorActionPreference = "Stop"

                        #Path of the CSV file resides
                        $csvData = Import-Csv -Path "$env:USERPROFILEDesktopdata.csv"
                        $DestinationFolder = "C:Stuffs"

                        foreach($record in $csvData)
                        {
                        $destPlatformFolder = $DestinationFolder + "" + $record.platform_

                        if(-not(Test-Path -Path $destPlatformFolder)){

                        New-Item -Path $destPlatformFolder -ItemType Directory -Force | Out-Null

                        }

                        $destinationFile = $destPlatformFolder + "" + $record._username

                        New-Item -Path $destinationFile -ItemType File -Force | Out-Null
                        }





                        share|improve this answer

























                          up vote
                          0
                          down vote













                          This worked for me.



                          $ErrorActionPreference = "Stop"

                          #Path of the CSV file resides
                          $csvData = Import-Csv -Path "$env:USERPROFILEDesktopdata.csv"
                          $DestinationFolder = "C:Stuffs"

                          foreach($record in $csvData)
                          {
                          $destPlatformFolder = $DestinationFolder + "" + $record.platform_

                          if(-not(Test-Path -Path $destPlatformFolder)){

                          New-Item -Path $destPlatformFolder -ItemType Directory -Force | Out-Null

                          }

                          $destinationFile = $destPlatformFolder + "" + $record._username

                          New-Item -Path $destinationFile -ItemType File -Force | Out-Null
                          }





                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            This worked for me.



                            $ErrorActionPreference = "Stop"

                            #Path of the CSV file resides
                            $csvData = Import-Csv -Path "$env:USERPROFILEDesktopdata.csv"
                            $DestinationFolder = "C:Stuffs"

                            foreach($record in $csvData)
                            {
                            $destPlatformFolder = $DestinationFolder + "" + $record.platform_

                            if(-not(Test-Path -Path $destPlatformFolder)){

                            New-Item -Path $destPlatformFolder -ItemType Directory -Force | Out-Null

                            }

                            $destinationFile = $destPlatformFolder + "" + $record._username

                            New-Item -Path $destinationFile -ItemType File -Force | Out-Null
                            }





                            share|improve this answer












                            This worked for me.



                            $ErrorActionPreference = "Stop"

                            #Path of the CSV file resides
                            $csvData = Import-Csv -Path "$env:USERPROFILEDesktopdata.csv"
                            $DestinationFolder = "C:Stuffs"

                            foreach($record in $csvData)
                            {
                            $destPlatformFolder = $DestinationFolder + "" + $record.platform_

                            if(-not(Test-Path -Path $destPlatformFolder)){

                            New-Item -Path $destPlatformFolder -ItemType Directory -Force | Out-Null

                            }

                            $destinationFile = $destPlatformFolder + "" + $record._username

                            New-Item -Path $destinationFile -ItemType File -Force | Out-Null
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 22 at 3:50









                            Basavaraju B K

                            3318




                            3318






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419348%2fpowershell-read-csv-file-and-create-files-accordingly%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Sphinx de Gizeh

                                Dijon

                                Guerrita