locked
Script to only move files one at a time. RRS feed

  • Question

  • Hi, I'm trying to build a script that will only move 3 files at a time from a directory that may contain dozens of files.  I'll use the Windows scheduler to run the PowerShell script every 5 minutes.  So every 5 minutes 3 files will be moved.

    I built the following PowerShell script that loops 3 times, but it moves all files at once.  What do I need to do to only copy one file at time during each of the 3 loops?  I can use either a PowerShell script or a batch file.

    for($i = 0; $i -le 3; $i++)
    {
    get-childitem -path E:\test\TEST1\ -filter *.* | move-item -destination E:\test\TEST2
    }

    Monday, October 8, 2018 1:30 PM

Answers

  • get-childitem -path E:\test\TEST1\* |
         Select -First 3 |
         move-item -destination E:\test\TEST2
     

    \_(ツ)_/

    • Marked as answer by JG-EDI Monday, October 8, 2018 2:54 PM
    Monday, October 8, 2018 2:35 PM

All replies

  • get-childitem -path E:\test\TEST1\* |
         Select -First 3 |
         move-item -destination E:\test\TEST2
     

    \_(ツ)_/

    • Marked as answer by JG-EDI Monday, October 8, 2018 2:54 PM
    Monday, October 8, 2018 2:35 PM
  • You've got to be kidding me.  There is actually a "First" option which specifies how many files to pick-up.  I missed that in the "get-help".  Don't even need the "for" loop.

    Thank you!  That works perfectly!




    Monday, October 8, 2018 2:54 PM
  • No kidding needed:

    PS D:\scripts> help select -par first
    
    -First <Int32>
        Gets only the specified number of objects. Enter the number of objects to get.
    
        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?       False
        Accept wildcard characters?  false
    


    \_(ツ)_/

    Monday, October 8, 2018 3:20 PM