The following powershell script is arranging the files by their extension in appropriate directory. What I did is first find all the unique extensions in the target folder and called copy for all those extensions but it doesn't work on Windows .I have used correct path for Windows.
Currently folders are being created but the file is not being copied from source folder to destination folder.
$source = "/home/shubham/ps/" #location of starting directory
$destination = "/home/shubham/ps/check"; #location where files will be `copied to`
$extensionArray = @(Get-ChildItem ($source) |
Select-Object Extension |
Sort-Object Extension |
Get-Unique -AsString)
echo ($extensionArray[3])
[string[]]$l_array = ($extensionArray | Out-String -Stream) -notmatch '^$' |
select -Skip 2
for ($i=0; $i -lt $extensionArray.Length; $i++) {
$x = $l_array[$i]
$ext = "*" + "$x"
#foreach ($x in $extension_array){
echo ("*" + ($x))
$newsrc = ($source) + "/" + ($ext)
$files = @("", "*" + ($x)) #edit .xlsx to your desired extension
$outputPath = ($destination) + "_" + ($x)
New-Item -ItemType Directory -Force -Path ($outputpath);
Copy-Item -Path $newsrc -Filter ($ext) -Destination $outputPath -Container
}
echo "end"
-Forceand-Debugparameters withMove-Itemwhen on Windows to see what's happening? Also, have you tried stepping through your code in the PS ISE on Windows to view variables values? - trebleCode