我想使用 JavaScript 从路径中删除文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7601674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I'd like to remove the filename from a path using JavaScript
提问by i4n
Using Javascript I'd like to remove the filename from the end of a string (path+filename), leaving me just the directory path.
使用Javascript,我想从字符串(路径+文件名)的末尾删除文件名,只留下目录路径。
Would regular expressions be ideal? Or, is there a simpler means of doing this using the string object?
正则表达式会是理想的吗?或者,是否有使用字符串对象执行此操作的更简单方法?
Thanks for any help!
谢谢你的帮助!
----ANSWERED & EXPLAINED---
----回答和解释---
The purpose of this code was to open finder to a directory. The data I was able to extract included a filename - since I was only trying to open finder (mac) to the location, I needed to strip the filename. Here's what I ended up with:
这段代码的目的是打开一个目录的finder。我能够提取的数据包括一个文件名 - 因为我只是试图打开 finder (mac) 到该位置,我需要去除文件名。这是我的结果:
var theLayer = app.project.activeItem.selectedLayers[0];
//get the full path to the selected file
var theSpot = theLayer.source.file.fsName;
//strip filename from the path
var r = /[^\/]*$/;
var dirOnly = theSpot.replace(r, '');
//use 'system' to open via shell in finder
popen = "open"
var runit = system.callSystem(popen+" "+"\""+dirOnly+"\"");
回答by Marshall
var urlstr = '/this/is/a/folder/aFile.txt';
var r = /[^\/]*$/;
urlstr.replace(r, ''); // '/this/is/a/folder/'
回答by Gazler
You haven't specified any sample inputs.
您尚未指定任何示例输入。
Assuming you always have a directory then the following will work. It takes everything up to (but not including) the last slash.
假设您总是有一个目录,那么以下内容将起作用。它需要所有内容(但不包括)最后一个斜杠。
test = "/var/log/apache2/log.txt";
console.log(test.substring(0, test.lastIndexOf("/")));
回答by Joe
You can use substring and indexOf:
您可以使用子字符串和 indexOf:
var url = 'asdf/whatever/jpg.image';
url.substring(0, url.lastIndexOf('/'))
回答by Suleman
I know this is a very old question and has already been answered, however my requirement was to remove the file if it exists in the given path or don't do anything if its folder already.
我知道这是一个非常古老的问题并且已经得到了回答,但是我的要求是删除文件,如果它存在于给定的路径中,或者如果它的文件夹已经存在,则不执行任何操作。
The accepted answer's regex didn't work for me so I used this one:
接受的答案的正则表达式对我不起作用,所以我使用了这个:
let FilePathString = '/this/is/a/folder/aFile.txt';
let FolderPathString = '/this/is/a/folder/';
const RegexPattern = /[^\/](\w+\.\w+$)/i;
console.log(FilePathString.replace(RegexPattern, '')); // '/this/is/a/folder/'
console.log(FolderPathString.replace(RegexPattern, '')); // '/this/is/a/folder/'
回答by KyleMit
As noted in this answer, if you're using node.js(fair assumption if you're dealing with file paths) - you can use the path
module, call path.parse
, and retrieve the directory name with dir
like this:
如本答案所述,如果您使用的是node.js(如果您正在处理文件路径,这是合理的假设)-您可以使用path
模块、调用path.parse
并检索目录名称,dir
如下所示:
const path = require("path")
let myPath = "folder/path/file.txt"
let myDir = path.parse(myPath).dir
console.log(myDir) // "folder/path"
This should be the most robust way to manage and parse file paths across different environments.
这应该是跨不同环境管理和解析文件路径的最稳健的方式。