none
read from my text for my starting project RRS feed

  • Question

  • Im a newbie still little things I can do, just watching some tutorial , I believe starting this project I will learn more.

    here what Im trying to do.

    I want to read the data from a text file, I have room number and a image file info, when I run this, the info for the room will made a folder and the image info will be copy inside the folder. 

    this might be the text containing

    room: 1234

    image name: honson.jpg

    room: 2333

    image name: walter.jpg

    room: 5467

    image name: bamboo.jpg

    $imgdir = C:\images
    $content
    = Get-Content C:\Users\You\Documents\book.txt foreach ($line in $content)

    i dont know how to remove the "room:" and to get only 1234 and copy honson.jpg and remove "image name:"

    thank you in Advance ..

    Wednesday, June 22, 2016 11:14 AM

Answers

All replies

  • Get-Content C:\Test\vincent.txt | Foreach {
    If ($_ -like 'room*') {
             $Name = $_ -replace 'room: '
             New-Item -Name $name  -Path C:\test -ItemType Directory}
    
    
    }

    Wednesday, June 22, 2016 11:30 AM
  • Do u want to rename the jpg fie with room number

    not sure what u mean by copy image info


    Wednesday, June 22, 2016 11:32 AM
  • Hi,

    welcome to PowerShell.

    You can easily do this with a -split operator:

    $line -split ":" | Select -last 1

    Or less easily with regex:

    $line | select-string -Pattern ": (.+)$" -AllMatches | exp Matches | %{ $_.Groups[1].Value }

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Wednesday, June 22, 2016 11:33 AM
  • thank you for the starting point

    Wednesday, June 22, 2016 11:49 AM
  • make the room as a folder then just copy the image inside that folder (room)

    thank you

    Wednesday, June 22, 2016 12:01 PM
  • Hello Guys,

    this is what i come up now. I manage to make the folder and copy the images inside, my problem now, it didnt copy images properly like 1 room copies 3 images instead 2 images only.  please help, i think it doesnt read it line by line. see my code. let me know also if im doing the right way.


    $Content = Get-Content C:\Cabin\O


    $Content | foreach {
                            if ($_ -like "cabin:*" ){$Cabin = $_}

                            $Cbn = $Cabin.Split(" ")[1]

                            New-item -type directory  -Name $Cbn -Path C:\Cabin -Force

                            if ($_ -like  "Image Name:*" ){$Image = $_}

                            $Img = $Image.Split(" ")[2]


                            Copy-Item C:\Cabin\Img\$Img -Destination C:\Cabin\$Cbn

                                            }

    this is content of Out-Sample.txt

    Cabin: 6237
    Guest Name: COLLEEN OSBORNE
    Image name: P011434674.JPG
    Print Size: 8x10


    Cabin: 6320
    Guest Name: VALARIE OWENS
    Image name: P111435615.JPG
    Print Size: 8x10


    Cabin: 6368
    Guest Name: CECILIO TIBURCIO
    Image name: P101332834.JPG
    Print Size: 8x10


    Cabin: 7255
    Guest Name: YULIYA NANTZE
    Image name: P131409255.JPG
    Print Size: 8x10


    Cabin: 7267
    Guest Name: EVELYN PORTER
    Image name: P011435928.JPG
    Print Size: 8x10


    Cabin: 7267
    Guest Name: EVELYN PORTER
    Image name: P061435922.JPG
    Print Size: 8x10


    Cabin: 7267
    Guest Name: EVELYN PORTER
    Image name: P091436161.JPG
    Print Size: 8x10


    Cabin: 7267
    Guest Name: EVELYN PORTER
    Image name: P101389149.JPG
    Print Size: 8x10


    Cabin: 7267
    Guest Name: EVELYN PORTER
    Image name: P101389238.JPG
    Print Size: 8x10


    Cabin: 7267
    Guest Name: EVELYN PORTER
    Image name: P151398218.JPG
    Print Size: 8x10


    Cabin: 7267
    Guest Name: EVELYN PORTER
    Image name: P161443591.JPG
    Print Size: 8x10


    Cabin: 7311
    Guest Name: KATHRYN AMBROSE
    Image name: P031411568.JPG
    Print Size: 8x10


    Cabin: 7311
    Guest Name: KATHRYN AMBROSE
    Image name: P031444986.JPG
    Print Size: 8x10


    Cabin: 7311
    Guest Name: KATHRYN AMBROSE
    Image name: P091448429.JPG
    Print Size: 8x10


    Cabin: 7311
    Guest Name: KATHRYN AMBROSE
    Image name: P091448431.JPG
    Print Size: 8x10


    Cabin: 7311
    Guest Name: KATHRYN AMBROSE
    Image name: P151439386.JPG
    Print Size: 8x10


    Cabin: 7311
    Guest Name: KATHRYN AMBROSE
    Image name: P151439387.JPG
    Print Size: 8x10


    Cabin: 7311
    Guest Name: KATHRYN AMBROSE
    Image name: P151439388.JPG
    Print Size: 8x10


    Cabin: 7311
    Guest Name: KATHRYN AMBROSE
    Image name: P161443482.JPG
    Print Size: 8x10


    Cabin: 8268
    Guest Name: JULIANNE TRUEBLOOD
    Image name: P011436193.JPG
    Print Size: 8x10


    Cabin: 8268
    Guest Name: JULIANNE TRUEBLOOD
    Image name: P061391711.JPG
    Print Size: 8x10


    Cabin: 8268
    Guest Name: JULIANNE TRUEBLOOD
    Image name: P081371973.JPG
    Print Size: 8x10


    Cabin: 8268
    Guest Name: JULIANNE TRUEBLOOD
    Image name: P091436554.JPG
    Print Size: 8x10


    Cabin: 8268
    Guest Name: JULIANNE TRUEBLOOD
    Image name: P111436063.JPG
    Print Size: 8x10


    Cabin: 8268
    Guest Name: JULIANNE TRUEBLOOD
    Image name: P161294504.JPG
    Print Size: 8x10


    Cabin: 8380
    Guest Name: WILLIAM HUFFMAN
    Image name: P011448475.JPG
    Print Size: 8x10


    Cabin: 9384
    Guest Name: PATRICIA ROUSE
    Image name: P031444998.JPG
    Print Size: 8x10


    Cabin: 9440
    Guest Name: KENNETH SPIEGEL
    Image name: P141362303.JPG
    Print Size: 8x10

    Thursday, June 30, 2016 6:53 PM
  • i update my post hope you can help me. thanks.
    Thursday, June 30, 2016 6:55 PM
  • i update my post hope you can help me. thanks.
    Thursday, June 30, 2016 6:56 PM
  • i update my post hope you can help me. thanks.
    Thursday, June 30, 2016 6:56 PM
  • There are many ways to this end.  Here is one that is very simple:

    Get-Content .\out_sample.txt -ReadCount 6 | 
    	ForEach-Object{
    		$h = @{ }
    		$z = $_ | ConvertFrom-String -Delimiter ':'
    		$h.Add($z[0].P1, $z[0].P2)
    		$h.Add($z[1].P1, $z[0].P2)
    		$h.Add($z[2].P1, $z[0].P2)
    		$h.Add($z[3].P1, $z[0].P2)
    		[pscustomObject]$h
    	}
    
    It assumes 6 lines per block.


    \_(ツ)_/

    Friday, July 1, 2016 10:22 AM
  • With a little help from Microsoft Research I have another method that works well with this kind of file.  It is called "FlashExtract"

    help convertFrom-String -full

    Command:

    Get-Content .\out_sample.txt | 
        ConvertFrom-String -TemplateFile .\cabin-template2.txt

    Here is the template file contents:

    Cabin: {Cabin*:6237}
    Guest Name: {Guest:COLLEEN OSBORNE}
    Image name: {Image:P011434674.JPG}
    Print Size: {Print:8x10}
    
    
    Cabin: {Cabin*:6320}
    Guest Name: {Guest:VALARIE OWENS}
    Image name: {Image:P111435615.JPG}
    Print Size: {Print:8x10}

    Notice that the template file uses data from the actual data file to create a set of "hints" as to the structure of the file.  The "FlashExtracter" generates and compiles a text parser that can then convert any file with the template structure.  The Output is and  object collection.

    Here re the formal rules as a pdf: http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0ahUKEwicnO3P-NLNAhVC9IMKHdfaD5EQFggcMAA&url=http%3A%2F%2Fresearch.microsoft.com%2Fen-us%2Fum%2Fpeople%2Fsumitg%2Fpubs%2Fpldi14-flashextract.pdf&usg=AFQjCNHaiqhDR0Pmn8oP4RB6RyhrNMrutA&sig2=jVsbDp5krfkxmx96p_nLew&bvm=bv.126130881,bs.2,d.dmo&cad=rjt

    Here is Microsoft Research: https://www.microsoft.com/en-us/research/


    \_(ツ)_/


    Friday, July 1, 2016 7:00 PM
  • With a little help from Microsoft Research I have another method that works well with this kind of file. It is called "FlashExtract"

    Nice! Thanks for posting this.


    -- Bill Stewart [Bill_Stewart]

    Friday, July 1, 2016 7:29 PM
    Moderator
  • With a little help from Microsoft Research I have another method that works well with this kind of file. It is called "FlashExtract"

    Nice! Thanks for posting this.


    -- Bill Stewart [Bill_Stewart]

    Be sure to check out the link to Microsoft Research.  Along with being very cool their projects may positively impact our health and quality of life.

    https://www.microsoft.com/en-us/research/


    \_(ツ)_/

    Friday, July 1, 2016 7:46 PM