【问题标题】:Powershell : Sort files by first characer of namePowershell:按名称的第一个字符对文件进行排序
【发布时间】:2017-07-04 17:55:28
【问题描述】:

谁能帮忙完成这个家庭项目?

我正在尝试对一个 500gb 以上的随机文件目录进行排序。

我想将这些文件分类到名为“A”、“B”、“C”...、“1”、“2”、“3”...的子目录中。

###################################################
# I've created a test directory to try my script against
###################################################
<# Start 7/4/2017 #>

Set-Location 'D:\IT Notes\psroot'


<# Make directories A..Z#>

65..90 | % {md ("{0}" -f [char]$_)}


<# Make a list of 0-9, A-Z, & a-z #>
$chars = [char[]] ([char]'0'..[char]'9' + [char]'A'..[char]'Z' + [char]'a'..[char]'z')


<# Use chars list to create random.txt file#>
(1..100).ForEach({-join (Get-Random $chars -Count 10) | Add-Content random.txt })


<# Save random.txt list to a variable#>
$random = (Get-Content .\random.txt)


<# Create files & add .txt extension #>
New-Item $random -ItemType file | Rename-Item -NewName {$_.name + ".txt"}

###################################################

现在我如何将所有内容分类到他们尊重的目录中?

提前感谢您的帮助!

【问题讨论】:

标签: powershell filesystems powershell-4.0


【解决方案1】:

这花了我整个上午,但我终于让它工作了!

对于那些有兴趣使用powershell清理那些大而不守规矩的目录的人,欢迎您:]

##########################
# Create directory A-Z
65..90 | % {md ("{0}" -f [char]$_)}


# Starting value = A
$upcaseN = 65


# Run loop while $upcase less than or equall to 90
while ($upcaseN -le 90) {

# Convert $upcase from int to char
$upcase =  [char]$upcaseN

# Move items to respected directories
Move-Item $upcase* $upcase -ErrorAction SilentlyContinue

# Increase starting value
$upcaseN++

}
###########################



###########################
# Create directory 0-9
0..9 | % {md ("{0}" -f $_)}

# Starting value = 0
$notaletter = 0

while ($notaletter -le 9){
Move-Item $notaletter* $notaletter -ErrorAction SilentlyContinue
$notaletter++

} #############################

【讨论】:

    猜你喜欢
    • 2018-08-19
    • 2019-01-04
    • 1970-01-01
    • 2020-11-18
    • 2011-07-12
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 2011-11-22
    相关资源
    最近更新 更多