Powershell 展平文件夹结构并创建索引
有些人喜欢使用长文件夹名和深文件夹结构。当您使用OneDrive/SharePoint Online并与Windows同步长路径时,这尤其令人痛苦 因此,我正在寻找一种缩短这些路径并保留其意义的方法,尤其是在归档文件和文件夹时 基本上是试图转变:Powershell 展平文件夹结构并创建索引,powershell,recursion,directory-structure,Powershell,Recursion,Directory Structure,有些人喜欢使用长文件夹名和深文件夹结构。当您使用OneDrive/SharePoint Online并与Windows同步长路径时,这尤其令人痛苦 因此,我正在寻找一种缩短这些路径并保留其意义的方法,尤其是在归档文件和文件夹时 基本上是试图转变: VeryLongPath\AnotherLongFolderPath\AndAnotherOne\VeryLongFilename.xlsx 进入: 并生成以下索引文件: 1 VeryLongPath 2 AnotherLongFolderP
VeryLongPath\AnotherLongFolderPath\AndAnotherOne\VeryLongFilename.xlsx
进入:
并生成以下索引文件:
1 VeryLongPath
2 AnotherLongFolderPath
3 AndAnotherOne
4 VeryLongFilename.xlsx
您可以创建一个帮助器类来跟踪各个标签:
class PathLabelIndexer
{
# This will hold the label translations, [string] -> [int]
[hashtable]$_terms = @{}
# This will keep track of how many distinct labels we've encountered
[int]$_length = 0
# Transforms a path into index values
[string] Transform([string]$Path){
return @($Path.Split('\') |%{
if($this._terms.ContainsKey($_)){
# If we already have a translation for $_, use that!
$this._terms[$_]
}
else {
# No existing translation found, add a new one
($this._terms[$_] = $this._length++)
}
}) -join '\'
}
# Produces the index needed to translate them back to labels
[string[]] GetIndex(){
# Since $_length starts at 0, a sorted array of the index values will give us the correct mapping
return [string[]]@($this._terms.GetEnumerator() |Sort Value |ForEach-Object Key)
}
}
现在您可以执行以下操作:
PS ~> $indexer = [PathLabelIndexer]::new()
PS ~> $indexer.Transform('VeryLongPath\AnotherLongFolderPath\AndAnotherOne\VeryLongFilename.xlsx')
0\1\2\3
要得到相反的结果,只需生成结果索引并再次查找各个标签:
PS ~> $index = $indexer.GetIndex()
PS ~> $index['0\1\2\3'.Split('\')] -join '\'
VeryLongPath\AnotherLongFolderPath\AndAnotherOne\VeryLongFilename.xlsx
您可以创建一个帮助器类来跟踪各个标签:
class PathLabelIndexer
{
# This will hold the label translations, [string] -> [int]
[hashtable]$_terms = @{}
# This will keep track of how many distinct labels we've encountered
[int]$_length = 0
# Transforms a path into index values
[string] Transform([string]$Path){
return @($Path.Split('\') |%{
if($this._terms.ContainsKey($_)){
# If we already have a translation for $_, use that!
$this._terms[$_]
}
else {
# No existing translation found, add a new one
($this._terms[$_] = $this._length++)
}
}) -join '\'
}
# Produces the index needed to translate them back to labels
[string[]] GetIndex(){
# Since $_length starts at 0, a sorted array of the index values will give us the correct mapping
return [string[]]@($this._terms.GetEnumerator() |Sort Value |ForEach-Object Key)
}
}
现在您可以执行以下操作:
PS ~> $indexer = [PathLabelIndexer]::new()
PS ~> $indexer.Transform('VeryLongPath\AnotherLongFolderPath\AndAnotherOne\VeryLongFilename.xlsx')
0\1\2\3
要得到相反的结果,只需生成结果索引并再次查找各个标签:
PS ~> $index = $indexer.GetIndex()
PS ~> $index['0\1\2\3'.Split('\')] -join '\'
VeryLongPath\AnotherLongFolderPath\AndAnotherOne\VeryLongFilename.xlsx
如果遇到名为
1
或2
的文件夹或文件,您想做什么?将其视为任何其他文件夹/文件,因此只需相应地重命名它,或保留相同的名称并将其添加到索引中。如果遇到名为1
或2
的文件夹或文件,您想做什么?将其视为任何其他文件夹/文件,因此,只需相应地重命名它,或者保留相同的名称并将其添加到索引中。