我拼凑了一个 Applescript,它返回目录(以及所有子目录)中所有文件的完整 posix 文件名列表。它在 shell 脚本中运行“find”命令。结果在运行 find 命令的目录后面有一个双斜杠。例如:
/Root_Directory/Directory_to_search//filename.txt
我读到过这是正常的,不会影响文件系统读取文件路径的方式,但我将该文件列表放入 Excel 中,将其与另一个列表进行比较,以检查是否有丢失的文件,而双斜线会破坏比较。我可以在 Excel 中搜索和替换以修复斜线,但我很好奇是否有人知道如何避免它
或者,如果有其他方法可以生成所有文件(而非目录)及其完整文件路径的列表。我只是喜欢寻找命令,所以我从这个开始。我确信还有其他方法。谢谢。这是脚本。
on writeTextToFile(theText, theFile, overwriteExistingContent)
try
-- Convert the file to a string
set theFile to theFile as string
-- Open the file for writing
set theOpenedFile to open for access file theFile with write permission
-- Clear the file if content should be overwritten
if overwriteExistingContent is true then set eof of theOpenedFile to 0
-- Write the new content to the file
write theText to theOpenedFile starting at eof
-- Close the file
close access theOpenedFile
-- Return a boolean indicating that writing was successful
return true
-- Handle a write error
on error
-- Close the file
try
close access file theFile
end try
-- Return a boolean indicating that writing failed
return false
end try
end writeTextToFile
set source_drive to choose folder with prompt "Please select hard drive."
set source_drive to the POSIX path of source_drive
set drive_name to name of (info for source_drive)
set theUser to long user name of (system info)
--set theDate to short date string of (current date) as string
--This produces a yyyymmdd timestamp
--source: https://yourscriptdoctor.com/applescript-date/
set dateObj to (current date)
set theMonth to text -1 thru -2 of ("0" & (month of dateObj as number))
set theDay to text -1 thru -2 of ("0" & day of dateObj)
set theYear to year of dateObj
set dateStr to theYear & theMonth & theDay as string
set source_drive to the quoted form of source_drive
set theText to do shell script "find " & source_drive & " -type f"
--set theText to do shell script "du -a " & source_drive
set theFile to (((path to desktop folder) as string) & drive_name & "_on_" & dateStr & ".txt")
writeTextToFile(theText, theFile, true)