JavaScript - 提取文件夹名称

发布于 2024-07-07 00:45:55 字数 121 浏览 7 评论 0原文

我对 JavaScript 还很陌生。

给定本地计算机的文件夹路径(Windows),我想知道如何提取当前路径中所有可能文件夹的名称,而不知道有多少个文件夹或它们的名称。

预先非常感谢您。

I'm fairly new to JavaScript.

Given a local machine's folder path (Windows), I was wondering how you can extract the names of all the possible folders in the current path, without the knowledge of how many folders there are or what they are called.

Thank you very much in advance.

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

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

发布评论

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

评论(4

放飞的风筝 2024-07-14 00:45:56

如果您在 Web 浏览器中执行 JavaScript,则不能,因为在这种情况下,出于安全原因,JavaScript 无法访问本地文件系统。

If you're executing JavaScript in a web browser then you can't, because in this scenario JavaScript has no access to the local file system for security reasons.

新一帅帅 2024-07-14 00:45:55

下面是一个小脚本,可帮助您开始将 FileSystemObject 与 JScript 结合使用:

var fso   = new ActiveXObject("Scripting.FileSystemObject");
var shell = new ActiveXObject("WScript.Shell");
var path  = "%ProgramFiles%";

var programFiles = fso.GetFolder(shell.ExpandEnvironmentStrings(path));
var subFolders   = new Enumerator(programFiles.SubFolders);

while (!subFolders.atEnd())
{
  var subFolder = subFolders.item();
  WScript.Echo(subFolder.Name);
  subFolders.moveNext();
}

在命令行上使用 csript.exe 调用该

cscript subfolders.js

脚本: Windows 脚本 5.6 文档 包含您需要的有关此主题(以及许多其他主题)的所有详细信息。 下载它并随身携带,它真的很有帮助。 在 Windows 系统上,稍微了解一下 FileSystemObject 及其亲属确实可以挽救局面。

Here is a little script to get you started with FileSystemObject in conjuction with JScript:

var fso   = new ActiveXObject("Scripting.FileSystemObject");
var shell = new ActiveXObject("WScript.Shell");
var path  = "%ProgramFiles%";

var programFiles = fso.GetFolder(shell.ExpandEnvironmentStrings(path));
var subFolders   = new Enumerator(programFiles.SubFolders);

while (!subFolders.atEnd())
{
  var subFolder = subFolders.item();
  WScript.Echo(subFolder.Name);
  subFolders.moveNext();
}

Call that with csript.exe on the command line:

cscript subfolders.js

The Windows Script 5.6 Documentation holds all the details you need on this topic (and many others). Download it and have it around, it is really helpful. On Windows systems, a little knowledge of FileSystemObject and it's relatives really can save the day.

吃素的狼 2024-07-14 00:45:55

您无法通过浏览器中的 Javascript 执行此操作,因为 JS 无法从浏览器访问文件系统。

You cannot do this via Javascript in a browser as the JS doesn't have that kind of access to the file system from a browser.

追星践月 2024-07-14 00:45:55

假设脚本将在尝试访问本地硬盘驱动器(例如在 cscript 或经典 ASP 中)有意义的上下文中执行,那么最好的选择是 FileSystemObject

Assuming the script will execute in a context where it makes sense to try and access the local hard drives (e.g. in cscript or classic ASP), your best bet is the FileSystemObject.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文