none
Read a CSV file & sort files to a new folder RRS feed

  • Question

  • Hi,

    I need a script for reading a CSV file, which contains 3 columns, "FILE_NAME" as col 1, "LIKE" as col 2 & "COMMENTS" as col 3. The script has to copy the files which have "LIKE" as 1 and paste them in a new folder created in the same directory. I am doing a student project where photos are placed in a folder(the csv is generated from an online album for which I have written the php code), the list of photo file names are in col 1. Chosen photos have value 1 in col 2 and these need to be copied to a new folder created within the parent folder. When I run this script it should read the CSV and do the job. I am an absolute PowerShell newbie, pls help me out.

    Thanks in advance,

    Raghav

    Wednesday, May 4, 2011 6:48 PM

Answers

  • well Kazun posted how it works. you just have to put it into a script.ps1 and copy it to the directory where you want to run it. his script is in that way better that he has no fixed paths, so just copy and run it. according to the create executable , i didn´t know that one, but i´ll try it just for fun ;-)

    Import-Csv file.csv -Delimiter ";" | ? {$_.Like -eq 1} | copy -Path {$_.file_name} -Destination new
    Thursday, May 5, 2011 6:00 PM

All replies

  • Did I make myself clear enough???

    Thursday, May 5, 2011 6:29 AM
  • ###############################
    # csvlist.csv
    ###############################
    # FILE_NAME;LIKE;COMMENTS
    # list.txt;sd;sd
    ###############################
    
    cls
    ###############################
    $path = "c:\test"
    $newpath = "c:\test\new"
    $csvarray = Import-Csv c:\test\list.csv -Delimiter ";"
    $csvarray
    foreach($csvline in $csvarray){
    $oldfile = ($($path)+"\"+$($csvline.FILE_NAME))
    $newfile = ($($newpath)+"\"+$($csvline.FILE_NAME))
    	if (Test-Path (($path)+($csvline.File))){
    		#Copy-Item ($($path)+$($csvline.File)) ($($newpath)+$($csvline.FILE_NAME))
    		copy-Item $oldfile $newfile
    	}
    }
    
    
    Thursday, May 5, 2011 7:46 AM
  • Hi Kloiner,

    Thanks for your assistance.

    Where are we checking the condition "LIKE" = 1 as I want only file names with "LIKE" as 1 to be copied to a different folder? Kindly excuse me as my knowledge is zero about this.

    Also does $path = "c:\test" take the relative path? I mean if I run the script in a folder containing the files & the csv, does it understand where to create the new folder & copy the files?

    Thanks in advance.

    Regards,

    Raghav

    Thursday, May 5, 2011 8:18 AM
  • file.csv

    FILE_NAME;LIKE;COMMENTS
    photo1;1;test
    photo2;1;test1
    photo3;2;test
    

    script:

    Import-Csv file.csv | ? {$_.Like -eq 1} | copy -Path {$_.file_name} -Destination new
    

    For example:

    PS > Import-Csv file.csv -Delimiter ";"
    
    FILE_NAME                LIKE                  COMMENTS
    ---------                ----                  --------
    photo1                 1                    test
    photo2                 1                    test1
    photo3                 2                    test
    
    PS > Import-Csv ..\file.csv -Delimiter ";" | ? {$_.like -eq "1"}
    
    FILE_NAME                LIKE                  COMMENTS
    ---------                ----                  --------
    photo1                 1                    test
    photo2                 1                    test1
    
    PS > dir
    
    
      Directory: C:\Users\test
    
    
    Mode        LastWriteTime   Length Name
    ----        -------------   ------ ----
    d----    05.05.2011   14:14      new
    -a---    05.05.2011   14:13     8 photo1
    -a---    05.05.2011   14:13     8 photo2
    -a---    05.05.2011   14:13     8 photo3
    
    PS > dir new -it's empty 
    
    PS > dir new
    
    
      Directory: C:\Users\test\new
    
    
    Mode        LastWriteTime   Length Name
    ----        -------------   ------ ----
    -a---    05.05.2011   14:13     8 photo1
    -a---    05.05.2011   14:13     8 photo2
    

    Thursday, May 5, 2011 10:18 AM
  • Hi,

    Is there a way to create a .exe file so that the user can just click to run the above process?

    Regards,

    Raghav

    Thursday, May 5, 2011 4:06 PM
  • not that i know, did you try to create a link?
    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -File "pathtoyouryourscript.ps1"

    btw sory for the above post, but i worked till now...


    Thursday, May 5, 2011 4:12 PM
  • Hi,

    Thanks for your help Kloiner & Kazun but I am confused seeing both your replies. Sorry for saying this but I am seeing PowerShell scripts for the first time. I am unable to correlate both your posts & arrive at my reqd result. If you dont mind can you kindly summarize how to go about with this?

    Reg the .exe, I found this link

    http://stackoverflow.com/questions/3759456/create-a-executable-exe-file-from-powershell-script

    I couldn't make much sense of what was said there, thought you can help me out going through those posts.

    Thanks,

    Raghav 

    Thursday, May 5, 2011 5:31 PM
  • well Kazun posted how it works. you just have to put it into a script.ps1 and copy it to the directory where you want to run it. his script is in that way better that he has no fixed paths, so just copy and run it. according to the create executable , i didn´t know that one, but i´ll try it just for fun ;-)

    Import-Csv file.csv -Delimiter ";" | ? {$_.Like -eq 1} | copy -Path {$_.file_name} -Destination new
    Thursday, May 5, 2011 6:00 PM