【问题标题】:Listing Absolute Paths of Files in Terminal w/more than 255 Characters列出终端中包含超过 255 个字符的文件的绝对路径
【发布时间】:2016-05-06 17:00:45
【问题描述】:

我有一个两部分的问题:

第 1 部分(终端)- 我在 Mac 环境中工作,该环境存档到基于 Windows 的系统,因此我需要检测 255 的路径长度,以便在存档之前更改这些文件。我在 StackOverflow 上看到了很多关于文件名长度的解决方案,但我需要绝对路径长度。我偶然发现了这个脚本,它很接近:

sudo find . -name \* -type f | perl -ne 's/(.*)/print length($1), " $1\n" if (length($1)>254)/e' | sort -n

问题是我没有看到绝对路径长度,我只看到当前目录的路径长度。有没有一种方法可以在当前目录中递归搜索文件,同时仍然显示所有列出文件的绝对路径?

第 2 部分 (Applescript) - 一旦第 1 部分在终端中正常工作,我希望将过程自动化一点。最好我想创建一个Applescript,当文件夹在Finder中突出显示时,我可以运行命令,终端将弹出并运行命令和列表。这是我目前所拥有的:

tell application "Finder" to set theSel to selection
tell application "Terminal" 
set theFol to POSIX path of ((item 1 of theSel) as text)    
if (count of windows) is not 0 then 
    set shell to do script "cd " & quoted form of theFol in window 1        
do script "find . -name \\* -type f | perl -ne 's/(.*)/print length($1), \" $1\\n\" if (length($1)>254)/e''" in shell       
end if
activate
end tell

问题是,当我运行这个 applescript 时,命令开始执行动作,但随后卡住了,我看到的只是

>

我必须按 ctrl+c 才能继续使用终端窗口。

有谁知道我在终端命令中缺少什么标志?对于突出显示的目录,我也愿意采用完全不同的方法来检索和列出 255 个字符或更多字符的文件的字符数和绝对路径。

【问题讨论】:

  • 你需要使用终端吗?这样做的目的是什么?您需要的真实结果是什么?

标签: terminal find applescript posix


【解决方案1】:

我不确定您在这种情况下使用终端能获得什么价值,所以这是我添加的一个没有它的解决方案。

on run
    set longPaths to {}
    tell application "Finder" to set theSel to selection
    repeat with aFile in theSel
        set aFile to aFile as string
        set pathLength to count of characters in aFile
        if pathLength > 255 then
            set end of longPaths to aFile
        end if
    end repeat

    if longPaths is not equal to {} then
        -- do something with your list of long paths, write them to a text file or whatever you want
        set pathToYourTextFile to (path to desktop folder as string)&"SampleTextFile.txt"
        set tFile to open for access file (pathToYourTextFile as string) with write permission
        repeat with filePath in longPaths
            write (filePath & return as string) to tFile starting at eof
        end repeat
        close access tFile
    end if
end run

【讨论】:

  • 对于我对 Applescript 的无知深表歉意(以上内容非常有帮助),但是将列表写入桌面上的文本文件的最简单方法是什么?
  • @AlexNoble - 我更新了上面的代码以将输出反映到文本文件中
  • 所以我只是注意到这不是通过目录递归读取,它只是读取当前选择的目录。您是否知道一种方法让它也可以读取所选内容的所有子目录?真的非常感谢您为此所做的一切。
  • @AlexNoble - 您将不得不构建自己的递归处理程序。这是可以在 stackoverflow 上找到的示例 (stackoverflow.com/questions/3896175/…)。
【解决方案2】:

只需将文件夹的路径放入find 命令而不是使用cd command

像这样:

tell application "Finder"
    set theFol to item 1 of (get selection)
    if class of theFol is not folder then return -- not a folder
end tell
set f to quoted form of POSIX path of (theFol as text)

tell application "Terminal"
    do script "find " & f & " -name \\* -type f | perl -ne 'print length($_), \" $_\",  if length($_)>255'" -- (>255 characters), because the $_ variable contains a newline
    activate
end tell

AppleScript 中 do script 命令的信息:您的命令卡在终端中,因为您在 perl 命令的末尾放置了两个简单的引号字符。

【讨论】:

    猜你喜欢
    • 2013-07-15
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多