简体   繁体   English

我想使用 JavaScript 从路径中删除文件名

[英]I'd like to remove the filename from a path using JavaScript

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.这段代码的目的是打开一个目录的finder。 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.我能够提取的数据包括一个文件名 - 因为我只是试图打开 finder (mac) 到该位置,我需要去除文件名。 Here's what I ended up with:这是我的结果:

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+"\"");
var urlstr = '/this/is/a/folder/aFile.txt';
var r = /[^\/]*$/;
urlstr.replace(r, ''); // '/this/is/a/folder/'

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("/")));

You can use substring and indexOf:您可以使用子字符串和 indexOf:

var url = 'asdf/whatever/jpg.image';
url.substring(0, url.lastIndexOf('/'))

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.这应该是跨不同环境管理和解析文件路径的最稳健的方式。

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/'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM