I want to move all files of a particular file type to a subfolder within their existing structures. All the examples I can find have hard-coded subfolders. I have many redundant folder structures so hard-coding isn't an option.
I have tried this:
$Dir = "\\root\*\REPORTS\*"
Get-ChildItem $Dir | Where-Object {$_.Name -match "csv"} | Move-Item -dest .\Queue\
It yields this error:
- Get-ChildItem $Dir | Where-Object {$_.Name -match "csv"} | Move-Item <<<< -dest .\Queue\
- CategoryInfo : WriteError: (\root...ity20170823.csv:FileInfo) [Move-Item], IOException
- FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand
I have read that the period denotes the current folder, and so I am trying to use that for destination but it appears to only apply to path. It finds the files ok but is just unable to move them. Help!
EDIT I tried this based on the below suggestion:
$Dir = "\\root\*\REPORTS\*"
Get-ChildItem $Dir | Where-Object {$_.Name -match "xml"} | %{
#Move-Item -path $_.FullName -dest $_.FullName.Replace('REPORTS', 'REPORTS\Queue')
write-output $_.FullName
write-output $_.FullName.Replace('REPORTS', 'REPORTS\Queue')
}
The write-output values look good but the move-item line yields this error:
Move-Item : Could not find a part of the path. At C:\Users[me]\Desktop[filename].ps1:18 char:14 + Move-Item <<<< -path $.FullName -dest $.FullName.Replace('REPORTS', 'REPORTS\Queue') + CategoryInfo : WriteError: (\root\d$...ity20170823.csv:FileInfo) [Move-Item], DirectoryNotFoundException + FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand
Ah, because the path doesn't exist. This will work if I put in a if-folder-exists-else-create check, I believe.
EDIT 2 This works, except that it puts all of the files into directories named for the files.
$Dir = "\\root\*\REPORTS\*"
Get-ChildItem $Dir | Where-Object {$_.Name -match "csv"} | %{
New-Item -ItemType Directory -Force -Path $_.FullName.Replace('REPORTS', 'REPORTS\Queue')
Move-Item -path $_.FullName -dest $_.FullName.Replace('REPORTS', 'REPORTS\Queue')
#write-output $_.FullName
#write-output $_.FullName.Replace('REPORTS', 'REPORTS\Queue')
}
Results in a move to:
\\root\abc\REPORTS\Queue\20170707-resub.csv\20170707-resub.csv
It would be nice if the folder created was just the path and not the file.
.
refers to your current working directory. – Maximilian Burszley\\root\abc\REPORTS\thisFile1.csv
moved to folder\\root\abc\REPORTS\Queue\thisFile1.csv
. I'm playing with Replace but failing. – n8.