简体   繁体   English

从全名获取目录,重命名目录

[英]get directory from fullname, rename directory

i want to find all files with more than 256 characters in fullname.我想查找全名超过 256 个字符的所有文件。

From these files i want to rename the folder.从这些文件中,我想重命名文件夹。

At the morment i'm at this point此刻我在这一点上

    $maxLength = 260
    $newPath = "a_path"

$paths = Get-ChildItem $newPath -force -Recurse | 
    Where-Object { ($_.FullName.Length -gt $maxLength) } |
    % {
     Write-Host $_.FullName
}

But now I'm lost.但现在我迷路了。

  1. I can't understand why the command write-host $paths doesn't work anymore.我不明白为什么命令 write-host $paths 不再起作用。
  2. From the fullname I only need the directory, i tried to get it with从全名我只需要目录,我试图用

    select Directory or Splith-Path select 目录或拆分路径

But this also doesn't work.但这也行不通。

  1. How can I say to "trim" the directory name to $maxLength = 260我怎么能说将目录名称“修剪”为 $maxLength = 260

Thank's for a few hints!感谢您的一些提示!

CYA_D0c CYA_D0c

  1. I can't understand why the command write-host $paths doesn't work anymore.我不明白为什么命令write-host $paths不再起作用。

Replace the Write-Host cmdlet by Select-Object :Write-Host cmdlet 替换为Select-Object

$maxLength = 260
$newPath = "a_path"
$paths = Get-ChildItem $newPath -Force -Recurse | Where-Object {$_.FullName.Length -gt $maxLength} | Select-Object -ExpandProperty FullName

$paths will then be a list of your desired full paths.然后$paths将是您所需的完整路径的列表。

  1. From the fullname I only need the directory, i tried to get it with select Directory or Splith-Path .从全名我只需要目录,我尝试使用select DirectorySplith-Path来获取它。 But this also doesn't work.但这也行不通。

Splith-Path will do it: Splith-Path会这样做:

$paths | Split-Path -Parent
  1. How can I say to "trim" the directory name to $maxLength = 260我怎么能说将目录名称“修剪”为 $maxLength = 260

That's not that easy as you might think.这并不像你想象的那么容易。 To what name do you want to trim it?你想把它修剪成什么名字? What if the trimmed name of a directory already exists?如果一个目录的修剪名称已经存在怎么办?

Of course you can rename a directory at any time, but you need a certain naming convention if you want to trim directory names to a certain length automatically.当然,您可以随时重命名目录,但是如果您想将目录名称自动修剪为特定长度,则需要一定的命名约定。

What's your original problem?你原来的问题是什么? Are you still using Windows 7 or old MS Office products that are limitted to a maximum path length of 256?您是否仍在使用 Windows 7 或限制最大路径长度为 256 的旧 MS Office 产品?

Write-Host is only for sending text characters to your terminal, be it the PowerShell Prompt or something else, and you can't catch it in a variable like you're trying to do. Write-Host仅用于将文本字符发送到您的终端,无论是 PowerShell 提示还是其他东西,您无法像您尝试做的那样在变量中捕获它。

We can simplify your code to this, to get only those paths longer than 260.我们可以将您的代码简化为此,只获得那些长度超过 260 的路径。

$paths = Get-ChildItem $newPath -force -Recurse | 
    Where-Object { ($_.FullName.Length -gt $maxLength) } | Select-Object -Expand Fullname   

Which should give you just a listing of the directories which are too long, stored inside of $paths .这应该只为您提供存储在$paths内的太长目录的列表。 You could export it to a csv file like so:您可以将其导出到 csv 文件,如下所示:

$paths | Export-Csv C:\PathTo\MyCooL.csv

And it is at this point that you should pause and decide how to proceed.正是在这一点上,您应该暂停并决定如何继续。

This sort of process is often attempted and we have, as a hive-mind, determined that it is not an ideal candidate for automation.这种过程经常被尝试,作为一个蜂巢思维,我们已经确定它不是自动化的理想候选者。 Instead, a person should review the list and then intelligently make the right decision to rename the directories.相反,一个人应该查看列表,然后明智地做出正确的决定来重命名目录。

Why?为什么? It would be very easy to break some apps if your renaming script ran amuk in C:\ProgramData\ , for instance.例如,如果您的重命名脚本在C:\ProgramData\中运行 amuk,则很容易破坏某些应用程序。 On looking at the list, you might find deeply nested installs containing Visual C++ Redistributables, or other important content.查看列表时,您可能会发现包含 Visual C++ Redistributables 或其他重要内容的深层嵌套安装。 You'll need to make the right call on how to rename it, to do so safely.您需要正确调用如何重命名它,以便安全地这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM