All my files are in specific folders:
17\1\1\PRO
17\1\2\PRO
17\2\1\PRO
xx\xx\xx\PRO
That last 1 has a folder PRO
in it where all data resides.
We need to move these files, but the files need to stay inside their respective "PRO" folders.
For example:
17\1\1\pro\xxx\www\
needs to go to 17\1\1\pro\movies
17\2\2\pro\xxdfsdf\eeee\
needs to go to 17\2\2\pro\movies
. The movies folder should get created if there are files to move.
I need to get a part of the full name of a file and move the file there to the "movie" folder. The problem is I do not know how to split the full name, add \movies to it and move the files there.
This is my code so far:
Get-ChildItem -Path $mypath -Recurse -File -Filter $extension | select $_Fullname |
Move-Item -Force -Destination ($_Fullname.Split("pro"))
If you don't know how many directories there are, I would do something like this:
Get-ChildItem -Path $mypath -Recurse -File -Filter $extension | ForEach-Object {
if ($_.FullName.IndexOf('\PRO\') -gt 0) {
$Destination = Join-Path -Path $_.FullName.Substring(0,$_.FullName.IndexOf('\PRO\') + 5) -ChildPath 'movies';
New-Item $Destination -ItemType Directory -ea Ignore;
$_ | Move-Item -Destination $Destination;
} else {
throw ("\PRO\ path not found in '$($_.FullName)'");
}
}
This will work fine as long as your paths only have \pro\
once. If they have it more than once like customer\pro\17\pro\17\1\1\pro\xx\yy\zz\www
and you need the last index, then use $_.FullName.LastIndexOf('\pro\')
.
If you've got \pro\
directories both before and after the directory that .\pro\movies\
is in, well, you're in trouble. You'll probably have to find a different point of reference.