以下 powershell 脚本按文件的扩展名在适当的目录中排列文件。我所做的是首先在目标文件夹中找到所有唯一的扩展名,并为所有这些扩展名调用副本,但它在 Windows 上不起作用。我为 Windows 使用了正确的路径。
当前正在创建文件夹,但未将文件从源文件夹复制到目标文件夹。
$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"