Array parse data

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 10 months ago.

  • Author
    Posts
  • #5944

    by todd.gallina at 2012-11-01 11:36:26

    [quote]
    I have a short script that would parse files by name. The script works with gci -filter "some filename"; however, what I will have to use will be an array's – due to the amount of files created. Certain machines create the file name by a prefix 108701 , 108702 etc etc. The last part of my code $files = gci $source is what I would like looked at.

    Kind regards,

    Todd

    #script to parse files into the proper folders

    $source ="\\127.0.0.1\baunhof\"

    $destination ="\\127.0.0.1\TestFolder1\\"
    $destination1="\\127.0.0.1\TestFolder2\\"
    $destination2="\\127.0.0.1\TestFolder3\\"

    #array for all destinations
    $destination_array=@("$destination", "$destination1", "$destination2")

    #creates folder yyyy/mm/dd
    #$today = (Get-date -format yyyy/MM/dd)
    #new-item -type directory ($today)
    $DTS = ( get-date ).ToString('yyyy/MM/dd')

    #array name for value of files
    $file_array=@{"8HP70"=108701}
    $file_array1=@{"8HP70X"=108702}
    $file_array2=@{"9HP48"=109401, 1094080, 1094090}
    $file_array3=@{"9HP48X"=109402, 1094081, 1094091, 1094082, 1094092}

    # if statement checks if $destination_array[0] is false then new item
    $destination_array[0] = "\\127.0.0.1\TestFolder1\today\"
    If (!(Test-Path -path $destination_array[0]))
    {new-item -type directory "\\127.0.0.1\TestFolder1\$DTS"}

    $destination_array[1] = "\\127.0.0.1\TestFolder2\today\"
    If (!(Test-Path -path $destination_array[1]))
    {new-item -type directory "\\127.0.0.1\TestFolder2\$DTS\"}

    $destination_array[2] = "\\127.0.0.1\TestFolder3\today\"
    If (!(Test-Path -path $destination_array[2]))
    {new-item -type directory "\\127.0.0.1\TestFolder3\$DTS\"}

    #change value of array
    remove-item variable:$destination_array
    $destination="\\127.0.0.1\TestFolder1\$DTS"
    $destination1="\\127.0.0.1\TestFolder2\$DTS"
    $destination2="\\127.0.0.1\TestFolde3\$DTS"

    $destination_array=@("$destination", "$destination1", "$destination2")

    # filter works below - need to use array

    #$files = get-childitem $source -filter "108701*" -recurse
    #foreach ($file in $files)
    #{move-item $file.fullname $destination_array[0] -force}

    $files = gci $source *.*
    if ($files -match $file_array)
    {move-item $file.fullname $destination_array[0] -force}

    echo $destination_array[0]
    echo $destination_array[1]
    echo $destination_array[2]
    echo $files

    [/quote]

    by DonJ at 2012-11-01 13:36:12

    I'm sorry – can you be a bit more specific about what exactly isn't working for you? I'm not following what this is trying to do, or where it's failing you.

    by nohandle at 2012-11-02 04:21:31

    that produces hash table not an array
    $file_array=@{"8HP70"=108701}
    -match accepts the argument as string (regex), hashtable converted to string produces
    "System.Collections.Hashtable"
    this produces true
    $file_array=@{"8HP70"=108701}
    "System.Collections.Hashtable" -match $file_array

    so the condition is imho wrong.
    if ($files -match $file_array)
    {move-item $file.fullname $destination_array[0] -force}

    even if you had a proper array in the $file_array you still would have to convert it to proper regex syntax
    if ($files -match "$($file_array -join '|')") {}

    by todd.gallina at 2012-11-02 13:05:50

    [quote="todd.gallina"][quote]
    I have a short script that would parse files by name. The script works with gci -filter "some filename"; however, what I will have to use will be an array's – due to the amount of files created. Certain machines create the file name by a prefix 108701 , 108702 etc etc. The last part of my code $files = gci $source is what I would like looked at.

    Kind regards,

    Todd

    #script to parse files into the proper folders

    $source ="\\127.0.0.1\baunhof\"

    $destination ="\\127.0.0.1\TestFolder1\\"
    $destination1="\\127.0.0.1\TestFolder2\\"
    $destination2="\\127.0.0.1\TestFolder3\\"

    #array for all destinations
    $destination_array=@("$destination", "$destination1", "$destination2")

    #creates folder yyyy/mm/dd
    #$today = (Get-date -format yyyy/MM/dd)
    #new-item -type directory ($today)
    $DTS = ( get-date ).ToString('yyyy/MM/dd')

    #array name for value of files
    $file_array=@{"8HP70"=108701}
    $file_array1=@{"8HP70X"=108702}
    $file_array2=@{"9HP48"=109401, 1094080, 1094090}
    $file_array3=@{"9HP48X"=109402, 1094081, 1094091, 1094082, 1094092}

    # if statement checks if $destination_array[0] is false then new item
    $destination_array[0] = "\\127.0.0.1\TestFolder1\today\"
    If (!(Test-Path -path $destination_array[0]))
    {new-item -type directory "\\127.0.0.1\TestFolder1\$DTS"}

    $destination_array[1] = "\\127.0.0.1\TestFolder2\today\"
    If (!(Test-Path -path $destination_array[1]))
    {new-item -type directory "\\127.0.0.1\TestFolder2\$DTS\"}

    $destination_array[2] = "\\127.0.0.1\TestFolder3\today\"
    If (!(Test-Path -path $destination_array[2]))
    {new-item -type directory "\\127.0.0.1\TestFolder3\$DTS\"}

    #change value of array
    remove-item variable:$destination_array
    $destination="\\127.0.0.1\TestFolder1\$DTS"
    $destination1="\\127.0.0.1\TestFolder2\$DTS"
    $destination2="\\127.0.0.1\TestFolde3\$DTS"

    $destination_array=@("$destination", "$destination1", "$destination2")

    # filter works below - need to use array

    #$files = get-childitem $source -filter "108701*" -recurse
    #foreach ($file in $files)
    #{move-item $file.fullname $destination_array[0] -force}

    $files = gci $source *.*
    if ($files -match $file_array)
    {move-item $file.fullname $destination_array[0] -force}

    echo $destination_array[0]
    echo $destination_array[1]
    echo $destination_array[2]
    echo $files

    [/quote][/quote]If someone could show how to take the value's in the hash table and move any file that matches that value to another destination? Using a -filter works on the section that is commented out; however, I would prefer to use a hashtable that is listed above on the code. /quote]

You must be logged in to reply to this topic.