locked
error when processing for loop (powershell) RRS feed

  • Question

  • Can some one help me with the below error, i need to increment $destin to next array same as $source.

    I want to do some thing like  foreach ($source in $TestSource -and $destin in $testdestin) {scriptblock}

    Here is the actual code:

    foreach ($source in $TestSource)
        {
            robocopy -Source $source -Destination $destin
            $destin = $destin++
          }

    Error:

    The '++' operator works only on numbers. The operand is a 'System.String'.
    At C:\Users\Dinesh\Desktop\TDHealth\Untitled18.ps1:98 char:9
    +         $destin = $destin++
    +         ~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : OperatorRequiresNumber

          
    Tuesday, January 27, 2015 11:11 PM

Answers

  • Your question is very unclear and cannot be decided with the information given.

    Perhaps you mean this:

    $txt=@'
    Source,Destination
    c:\source1,d:\dest1
    c:\source2,d:\dest2
    c:\source3,d:\dest3
    '@
    
    $steps=$txt|ConvertFrom-Csv
    
    foreach($step in $steps){
        RoboCopy $step.Source $step.Destination
    }
    


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, January 27, 2015 11:32 PM
    • Marked as answer by Dinesh Kumar C Tuesday, January 27, 2015 11:54 PM
    Tuesday, January 27, 2015 11:32 PM

All replies

  • Your question is very unclear and cannot be decided with the information given.

    Perhaps you mean this:

    $txt=@'
    Source,Destination
    c:\source1,d:\dest1
    c:\source2,d:\dest2
    c:\source3,d:\dest3
    '@
    
    $steps=$txt|ConvertFrom-Csv
    
    foreach($step in $steps){
        RoboCopy $step.Source $step.Destination
    }
    


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, January 27, 2015 11:32 PM
    • Marked as answer by Dinesh Kumar C Tuesday, January 27, 2015 11:54 PM
    Tuesday, January 27, 2015 11:32 PM
  • No,I have source and destination in different text files.

    Tuesday, January 27, 2015 11:45 PM
  • Hi Jrv,

    I tried your method and it worked, thanks for your help. However i am curious to know what if i have the source and destination on different files. Please assist.

    Tuesday, January 27, 2015 11:54 PM
  • How do you know which source matches which destination?

    ¯\_(ツ)_/¯

    Wednesday, January 28, 2015 1:05 AM