Move files without moving subfolder files

  • I have been tasked with building a powershell file mover to move files (which could have any name and possibly no extensions) from one share to another. My problem is the source share has a subfolder that goes to a different location and needs to not be touched by the script I'm building.

    Every time I try to google this subject, google helpfully points out how easy it is to move files AND subfolders / subfolder files, not how to move files without moving subfolders or subfolder files.

    Here's my path: \\NAS\Folder1\Folder2\Folder3

    I am taking files in Folder 2 and moving them elsewhere. Files in Folder 3 need to remain where they are until a different process sweeps them. It is possible for Folder 3 to get new files in while the script for Folder 2 is running, so creating a run order (to move Folder 3 and then to move Folder 2) doesn't entirely solve my problem.

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/move-item?view=powershell-6 doesn't seem to have any comments on how to avoid subfolder file movement.

    Any thoughts or links to what I need?

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • I'f I'm understanding correctly, Brandie, this should do what you want. By default Get-ChildItem won't recurse, and using -file will mean it excludes folders. Thus you can do something like this:

    $SourceFolder = "C:\temp\Folder 1\Folder 2\"
    $TargetFolder = "C:\temp\Folder 1\"

    $TargetFiles = Get-ChildItem $SourceFolder -file

    foreach($File in $TargetFiles){
        $Target = $TargetFolder + $File.Name
        Write-Host "Moving file $File to $Target"
        Move-Item -Path $File.FullName -Destination $Target
    }

    For my little set up, it moved a bunch of (blank) text files I put in C:\temp\Folder 1\Folder 2\ to C:\temp\Folder 1\, however, left a bunch of (empty) xlsx files I put in C:\temp\Folder 1\Folder 2\Folder 3 alone, as well as the subdirectory.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Thom A - Monday, July 2, 2018 6:39 AM

    I'f I'm understanding correctly, Brandie, this should do what you want. By default Get-ChildItem won't recurse, and using -file will mean it excludes folders. Thus you can do something like this:

    $SourceFolder = "C:\temp\Folder 1\Folder 2\"
    $TargetFolder = "C:\temp\Folder 1\"

    $TargetFiles = Get-ChildItem $SourceFolder -file

    foreach($File in $TargetFiles){
        $Target = $TargetFolder + $File.Name
        Write-Host "Moving file $File to $Target"
        Move-Item -Path $File.FullName -Destination $Target
    }

    For my little set up, it moved a bunch of (blank) text files I put in C:\temp\Folder 1\Folder 2\ to C:\temp\Folder 1\, however, left a bunch of (empty) xlsx files I put in C:\temp\Folder 1\Folder 2\Folder 3 alone, as well as the subdirectory.

    AHHA! That -File switch looks like what I want indeed. Thank you so much. I'll give that a whirl.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • Brandie Tarvin - Monday, July 2, 2018 6:25 AM

    I have been tasked with building a powershell file mover to move files (which could have any name and possibly no extensions) from one share to another. My problem is the source share has a subfolder that goes to a different location and needs to not be touched by the script I'm building.

    Every time I try to google this subject, google helpfully points out how easy it is to move files AND subfolders / subfolder files, not how to move files without moving subfolders or subfolder files.

    Here's my path: \\NAS\Folder1\Folder2\Folder3

    I am taking files in Folder 2 and moving them elsewhere. Files in Folder 3 need to remain where they are until a different process sweeps them. It is possible for Folder 3 to get new files in while the script for Folder 2 is running, so creating a run order (to move Folder 3 and then to move Folder 2) doesn't entirely solve my problem.

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/move-item?view=powershell-6 doesn't seem to have any comments on how to avoid subfolder file movement.

    Any thoughts or links to what I need?

    I guess I don't understand why the equivalent of a DIR command on the \\NAS\Folder1\Folder2 path without the equivalent of the "/S" or "with recursion" option wouldn't do such a thing.  It would only list files in the Folder2 path and no subfolders.

    Also, this seems to be the reinvention of a very old, tried and true, trusted wheel.  Why not use the likes of RoboCopy or even XCopy?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden - Monday, July 2, 2018 6:49 AM

    I guess I don't understand why the equivalent of a DIR command on the \\NAS\Folder1\Folder2 path without the equivalent of the "/S" or "with recursion" option wouldn't do such a thing.  It would only list files in the Folder2 path and no subfolders.

    Also, this seems to be the reinvention of a very old, tried and true, trusted wheel.  Why not use the likes of RoboCopy or even XCopy?

    I'm replacing an FTP scripting process with this and putting it into a SQL Job. Part of our problem is that we're no longer allowed to use Windows Task Scheduler for things (it's a corporate mandate), so everything we schedule has to be done via SQL Server now. Also, the boss wanted me to learn something new as part of this process.

    Thom, your code worked beautifully except for the Write-Host. I replaced it with a Write-Output (and a log time variable) to log the file moves to a text log file that we can refer to in cases of "where did the file go". Thank you so much for your assistance.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • Brandie Tarvin - Monday, July 2, 2018 7:12 AM

    Thom, your code worked beautifully except for the Write-Host. I replaced it with a Write-Output (and a log time variable) to log the file moves to a text log file that we can refer to in cases of "where did the file go". Thank you so much for your assistance.

    You're welcome. Interesting that it didn't like Write-Host, but at least Write-Output worked.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • For anyone else who reads this thread later, it helps to put a file existence check around the file move. Otherwise (as I just found out) the job step fails on an empty folder.

    WHOOPS.

    here's my final code.

    $erroractionpreference = "Stop"

    $SourceFolder = "\\NAS\Folder1\Folder2\"

    $DestinationFolder = "\\NAS2\Test\Folder2\"

    $TargetFiles = Get-ChildItem $SourceFolder -file

    # Get date for log

    $LogTime = Get-Date -Format "MM-dd-yyyy_hh:mm:ss"

    if( (Get-ChildItem $SourceFolder -File | Measure-Object).Count -ne 0)

    {

    foreach($File in $TargetFiles){

    $Target = $DestinationFolder + $File.Name

    # Print results

    Write-Output ($LogTime + " Moving file $File to $Target`r`n" -f $File.FileName) | Out-File -FilePath \\NAS2\Test\Folder2\FTP_FilesMove_Log.txt -Encoding ASCII -Append

    # Write-Host "Moving file $File to $Target"

    Move-Item -Path $File.FullName -Destination $Target}

    }

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • Thom A - Monday, July 2, 2018 7:30 AM

    Brandie Tarvin - Monday, July 2, 2018 7:12 AM

    Thom, your code worked beautifully except for the Write-Host. I replaced it with a Write-Output (and a log time variable) to log the file moves to a text log file that we can refer to in cases of "where did the file go". Thank you so much for your assistance.

    You're welcome. Interesting that it didn't like Write-Host, but at least Write-Output worked.

    It's not that it didn't like it. It's that it didn't work for what I wanted to do, which is create a separate log file on a NAS somewhere.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • So I thought this was solved, but the code I can run in the Powershell UI doesn't want to run in a SQL Job Step.

    Any thoughts on this error?

    A job step received an error at line 6 in a PowerShell script. The corresponding line is '$TargetFiles = Get-ChildItem $SourceFolder -File'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'A parameter cannot be found that matches parameter name 'File'.  '.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • I don't know if this still holds but the Powershell included with SSMS was a subset. We got around it  by having a folder for our scripts and using CmdExec in the job step to execute the script.

    MG

    "There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies."
    Tony Hoare

    "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair.

  • MG-148046 - Tuesday, July 3, 2018 6:19 AM

    I don't know if this still holds but the Powershell included with SSMS was a subset. We got around it  by having a folder for our scripts and using CmdExec in the job step to execute the script.

    Yeah, that's what I ended up doing about 5 minutes before I got the email notification of your post. When I did that, it worked fine.

    Thanks.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply