有没有办法将两个列表写入.csv两个列表以在Apple脚本中分开列?

发布于 2025-02-06 07:27:09 字数 731 浏览 1 评论 0原文

要自动编写立即在.csv du -sh的目录下方的文件/文件夹的文件列表,

这是我要寻找的内容,但所有数据都在写入.CSV时最终出现在一个列中。如果可以逗号划定数据而不是选项卡,那可能会解决问题。

我得到的近距离定义了两个变量:儿童规模和儿童路径。每个都是我想要的数据列表。

我尝试了Echo,printf,现在粘贴了,而没有得到我想要的东西,有点困难。

set RootDirectoryPosix to POSIX path of RootDirectoryAlias
set ChildSizes to do shell script ("du -sh -- " & RootDirectoryPosix & "*" & "| cut -f1")
set ChildPaths to do shell script ("du -sh -- " & RootDirectoryPosix & "*" & "| cut -f2")
set ChildPathsString to ChildPaths as string
set ChildSizesString to ChildSizes as string
tell application "Finder"
    do shell script "paste -d ," & ChildSizesString & " " & ChildPathsString & " >~/Test.csv"
end tell

Looking to automate writing the filesizes of files/folders immediately beneath a directory to a .csv

du -sh is the closes to what I'm looking for but all the data ends up in one column when writing to a .csv. If it were possible to comma deliminate the data instead of tab, that might solve the issue.

The closes I've gotten is defining two variables: ChildSizes and ChildPaths. With each being only a list of the data I want.

I've tried echo, printf, and now paste without getting exactly what I want and am a bit stumped.

set RootDirectoryPosix to POSIX path of RootDirectoryAlias
set ChildSizes to do shell script ("du -sh -- " & RootDirectoryPosix & "*" & "| cut -f1")
set ChildPaths to do shell script ("du -sh -- " & RootDirectoryPosix & "*" & "| cut -f2")
set ChildPathsString to ChildPaths as string
set ChildSizesString to ChildSizes as string
tell application "Finder"
    do shell script "paste -d ," & ChildSizesString & " " & ChildPathsString & " >~/Test.csv"
end tell

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望笑 2025-02-13 07:27:09

在这里,我从用户域中选择root文件夹脚本文件夹。以下脚本在桌面上创建CSV文件。

当我检查时,shell命令创建了用 tabs 界定的文本。脚本只是用“”;定界符

set rootFolder to path to scripts folder from user domain
set RootDirectoryPosix to quoted form of (POSIX path of rootFolder)

set csvText to do shell script "du -sh -- " & RootDirectoryPosix & "*"

set {TID, text item delimiters} to {text item delimiters, tab}
set csvText to text items of csvText
set AppleScript's text item delimiters to ";"
set csvText to csvText as text
set AppleScript's text item delimiters to TID

set fileRef to open for access file (((path to desktop) as text) & "Desktop CSV file .csv") with write permission
write csvText to fileRef as «class utf8»
close access fileRef

添加列标题:

set rootFolder to path to scripts folder from user domain
set RootDirectoryPosix to quoted form of (POSIX path of rootFolder)

set csvText to "   The Sizes   ;   The Paths    " & return
set csvText to csvText & (do shell script "du -sh -- " & RootDirectoryPosix & "*")

set {TID, text item delimiters} to {text item delimiters, tab}
set csvText to text items of csvText
set AppleScript's text item delimiters to ";"
set csvText to csvText as text
set AppleScript's text item delimiters to TID

set fileRef to open for access file (((path to desktop) as text) & "Desktop CSV file .csv") with write permission
write csvText to fileRef as «class utf8»
close access fileRef

Here, I choose for the root folder the Scripts folder from user domain. Following script creates CSV file on the desktop.

As I checked the shell command creates text delimited with TABs. The script simply replaces them with ";" delimiter.

set rootFolder to path to scripts folder from user domain
set RootDirectoryPosix to quoted form of (POSIX path of rootFolder)

set csvText to do shell script "du -sh -- " & RootDirectoryPosix & "*"

set {TID, text item delimiters} to {text item delimiters, tab}
set csvText to text items of csvText
set AppleScript's text item delimiters to ";"
set csvText to csvText as text
set AppleScript's text item delimiters to TID

set fileRef to open for access file (((path to desktop) as text) & "Desktop CSV file .csv") with write permission
write csvText to fileRef as «class utf8»
close access fileRef

With adding column titles:

set rootFolder to path to scripts folder from user domain
set RootDirectoryPosix to quoted form of (POSIX path of rootFolder)

set csvText to "   The Sizes   ;   The Paths    " & return
set csvText to csvText & (do shell script "du -sh -- " & RootDirectoryPosix & "*")

set {TID, text item delimiters} to {text item delimiters, tab}
set csvText to text items of csvText
set AppleScript's text item delimiters to ";"
set csvText to csvText as text
set AppleScript's text item delimiters to TID

set fileRef to open for access file (((path to desktop) as text) & "Desktop CSV file .csv") with write permission
write csvText to fileRef as «class utf8»
close access fileRef
内心旳酸楚 2025-02-13 07:27:09

这就是我最终得到的:

set rootFolder to choose folder with prompt "Select directory to run du -sh against
set rootFolder to choose folder with prompt "Select root directory"
set RootDirectoryPosix to quoted form of (POSIX path of rootFolder)

set csvText to "The Sizes,The Paths" & return
set csvText to csvText & (do shell script "du -sh -- " & RootDirectoryPosix & "*")

set {TID, text item delimiters} to {text item delimiters, tab}
set csvText to text items of csvText
set AppleScript's text item delimiters to ","
set csvText to csvText as text
set AppleScript's text item delimiters to TID

set fileRef to open for access file (((path to desktop) as text) & "RootFolderdu-sh.csv") with write permission
write csvText to fileRef as «class utf8»
close access fileRef

This is what I ended up with:

set rootFolder to choose folder with prompt "Select directory to run du -sh against
set rootFolder to choose folder with prompt "Select root directory"
set RootDirectoryPosix to quoted form of (POSIX path of rootFolder)

set csvText to "The Sizes,The Paths" & return
set csvText to csvText & (do shell script "du -sh -- " & RootDirectoryPosix & "*")

set {TID, text item delimiters} to {text item delimiters, tab}
set csvText to text items of csvText
set AppleScript's text item delimiters to ","
set csvText to csvText as text
set AppleScript's text item delimiters to TID

set fileRef to open for access file (((path to desktop) as text) & "RootFolderdu-sh.csv") with write permission
write csvText to fileRef as «class utf8»
close access fileRef
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文