July 2, 2018 at 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?
July 2, 2018 at 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.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
July 2, 2018 at 6:43 am
Thom A - Monday, July 2, 2018 6:39 AMI'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.
July 2, 2018 at 6:49 am
Brandie Tarvin - Monday, July 2, 2018 6:25 AMI 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
Change is inevitable... Change for the better is not.
July 2, 2018 at 7:12 am
Jeff Moden - Monday, July 2, 2018 6:49 AMI 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.
July 2, 2018 at 7:30 am
Brandie Tarvin - Monday, July 2, 2018 7:12 AMThom, 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
July 2, 2018 at 7:31 am
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}
}
July 2, 2018 at 7:36 am
Thom A - Monday, July 2, 2018 7:30 AMBrandie Tarvin - Monday, July 2, 2018 7:12 AMThom, 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.
July 2, 2018 at 11:36 am
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'. '.
July 3, 2018 at 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.
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.
July 3, 2018 at 7:16 am
MG-148046 - Tuesday, July 3, 2018 6:19 AMI 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.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply