none
Renaming CSV with its own Row values Powershell RRS feed

  • Question

  • Hi All,

    I am fortunate enough to be part of this community, I am stuck with the below situation kindly help me out by improving my powershell code:

    The situation is , I have a path lets say "D:\Lzero\Input_1\OnlyTime"  were I have many CSV's generated in a certain job flow like xyz.csv, abc.csv 

    The Contents of this CSV looks like this :

    ------------------------------------------------------------------------------------------------------------------------

    xyz.csv

    CI               ,  Env  ,  Time , Site , Reboot Rights

    Random1   , Prod  ,  1600,  Mal ,  Auto

    Random2   , Prod  ,  1600,   Mal ,  Auto

    ------------------------------------------------------------------------------------------------------------------------

    ------------------------------------------------------------------------------------------------------------------------

    abc.csv

    CI               ,  Env  ,  Time , Site , Reboot Rights

    Random3   , Test ,  1400,  Cali ,  Auto

    Random4   , Test  ,  1400,  Cali ,  Auto

    ------------------------------------------------------------------------------------------------------------------------

    You see in a CSV only the CI's change and the other details like Env,time,Site etc remains same..now what I am trying is to generate CSV files that contains only "CI's" and no other details in it and the newly generated csv's should be named using the "Site+Env+Time+Reboot Rights.csv".

    and to get my results I am trying the below powershell code:

    $filepath =Get-ChildItem D:\Lzero\Input_1\OnlyTime
    ForEach($file in $filepath)
    {
    $filename = `
            $file.Site + `
            $file.Environment + `
            $file.Time + `
            $file.'Reboot Rights' + `
            ".csv"
        Import-Csv D:\Lzero\Input_1\OnlyTime\$file|select CI|Export-Csv -Path D:\Lzero\Junk\Name\$filename
    }

    Its not working for me please help me out...

    Any help in this regard is highly appreciated thanks in advance...

    one of the  output expected is :

    CaliTest1400AUTO.csv

    CI

    Random3

    Random4


    Friday, November 7, 2014 3:40 PM

Answers

  • Not tested, but I believe this should work:

    $filepaths = Get-ChildItem D:\Lzero\Input_1\OnlyTime | 
     select -ExpandProperty fullname
    
    ForEach($file in $filepaths)
      {
       $CSV = Import-Csv $file
    
       $filename = ('Site','Environment','Time','Reboot Rights' | 
        foreach {$CSV[0].$_}) -join '' 
            
     $CSV | select CI | Export-Csv -Path D:\Lzero\Junk\Name\$filename.csv
    }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, November 7, 2014 3:58 PM
    Moderator

All replies

  • Not tested, but I believe this should work:

    $filepaths = Get-ChildItem D:\Lzero\Input_1\OnlyTime | 
     select -ExpandProperty fullname
    
    ForEach($file in $filepaths)
      {
       $CSV = Import-Csv $file
    
       $filename = ('Site','Environment','Time','Reboot Rights' | 
        foreach {$CSV[0].$_}) -join '' 
            
     $CSV | select CI | Export-Csv -Path D:\Lzero\Junk\Name\$filename.csv
    }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, November 7, 2014 3:58 PM
    Moderator
  • @mjolinor, It works exactly as wanted ..:)

    Thanks a lot!!

    Friday, November 7, 2014 4:23 PM
  • @mjolinor ,

    One small request , what if I don't want the header in my newly generated CSV's or how shall I remove the header?

    Friday, November 7, 2014 4:41 PM