将每 n 个文件移动到一个单独的文件夹中
Moving every n files into a separate folder
我有几个文件具有重复的命名约定,例如
1*Intro*
2*
3*
…
10*intro*
….
我想将每个模块移动到一个单独的文件夹中。所以,我应该从每个 *intro*
到下一个分开。
另外,我应该注意到文件是有编号和排序的。
我想,最简单的方法是:
1. Get a list of intros.
2. Separate their numbers.
3. Start moving files starting from one number till their smaller than the next one.
$i = 1
Ls *intro* | ft {$_.name -replace '\D.*', ''}
// The reason for .* is that the files are `mp4`.
Ls * | ? {$_.name -match '[^firstNumber-SecondNumber-1]'} | move-item -literalpath {$_.fullname} -destination $path + $i++ + '/' +{$_.name}
所以最后一个命令应该是这样的:
Ls *intro* | % { ls * | ? {…} | move-item … }
或者 move-item
本身可以完成过滤工作。
正则表达式不起作用,我没有足够的 Powershell 知识来编写更好的东西。你能想出任何脚本来做到这一点吗?另外,我应该如何允许 move-item
创建文件夹?
如果有人可以用更好的标题编辑此post,我将不胜感激。
这可以通过简单的 Switch
来完成。该开关将针对当前文件夹中的所有项目 运行(使用 Get-ChildItem
cmdlet 获得的项目,您使用它的别名 'LS')。它查看文件的文件名中是否包含字符串 "Intro"。如果是,它会使用该文件的名称创建一个新文件夹,并将该文件夹的信息存储在 $TargetFolder
变量中(之前创建的变量以避免范围问题)。然后它将文件移动到该文件夹,并继续到下一个文件。如果文件的文件名中没有 "Intro",它只是将文件移动到创建的最后一个指定的 $TargetFolder
。
$TargetFolder = ""
Switch(Get-ChildItem .\*){
{$_.BaseName -match "intro"} {$TargetFolder = New-Item ".$($_.BaseName)" -ItemType Directory; Move-Item $_ -Destination $TargetFolder; Continue}
default {Move-Item $TargetFolder}
}
我有几个文件具有重复的命名约定,例如
1*Intro*
2*
3*
…
10*intro*
….
我想将每个模块移动到一个单独的文件夹中。所以,我应该从每个 *intro*
到下一个分开。
另外,我应该注意到文件是有编号和排序的。
我想,最简单的方法是:
1. Get a list of intros.
2. Separate their numbers.
3. Start moving files starting from one number till their smaller than the next one.
$i = 1
Ls *intro* | ft {$_.name -replace '\D.*', ''}
// The reason for .* is that the files are `mp4`.
Ls * | ? {$_.name -match '[^firstNumber-SecondNumber-1]'} | move-item -literalpath {$_.fullname} -destination $path + $i++ + '/' +{$_.name}
所以最后一个命令应该是这样的:
Ls *intro* | % { ls * | ? {…} | move-item … }
或者 move-item
本身可以完成过滤工作。
正则表达式不起作用,我没有足够的 Powershell 知识来编写更好的东西。你能想出任何脚本来做到这一点吗?另外,我应该如何允许 move-item
创建文件夹?
如果有人可以用更好的标题编辑此post,我将不胜感激。
这可以通过简单的 Switch
来完成。该开关将针对当前文件夹中的所有项目 运行(使用 Get-ChildItem
cmdlet 获得的项目,您使用它的别名 'LS')。它查看文件的文件名中是否包含字符串 "Intro"。如果是,它会使用该文件的名称创建一个新文件夹,并将该文件夹的信息存储在 $TargetFolder
变量中(之前创建的变量以避免范围问题)。然后它将文件移动到该文件夹,并继续到下一个文件。如果文件的文件名中没有 "Intro",它只是将文件移动到创建的最后一个指定的 $TargetFolder
。
$TargetFolder = ""
Switch(Get-ChildItem .\*){
{$_.BaseName -match "intro"} {$TargetFolder = New-Item ".$($_.BaseName)" -ItemType Directory; Move-Item $_ -Destination $TargetFolder; Continue}
default {Move-Item $TargetFolder}
}