I would like to move all .txt files present in a source directory (including .txt present in subfolders) to a destination directory.
For eg:
Source directory: D:\OFFICE work\robtest\test1
Above directory also contains many subfolders.
Destination directory: D:\OFFICE work\robtest\test2
I would like to move only .txt files in source dir to the above mentioned destination directory, in such a way that only 3 .txt files must be present per sub folder (includes random folder creation) in the destination directory.
Below is the code I tried, but PowerShell says
The filename, directory name, or volume label syntax .(D:\OFFICE work\robtest\test1\testtest\testtest2\ New folder\test01112.txt\ ) is incorrect.
I am not sure why there is extra '/' after the above path in robocopy.
$fileperfolder = 3
$source = "D:\OFFICE work\robtest\test1";
$dest = "D:\OFFICE work\robtest\test2";
$folderName = [System.IO.Path]::GetRandomFileName();
$fileList = Get-ChildItem -Path $source -Filter *.txt -Recurse
Write-Host $fileList
$i = 0;
foreach ($file in $fileList) {
$destiny = $dest + "\" + $folderName
Write-Host $file.FullName;
Write-Host $destiny;
New-Item $destiny -Type Directory -Force
robocopy $file.FullName $destiny /mov
$i++;
if ($i -eq $fileperfolder) {
$folderName = [System.IO.Path]::GetRandomFileName();
$i = 0;
}
}