简体   繁体   English

如何使用JavaScript从路径获取最后的文件夹名称和文件名?

[英]How to get the last folder name and file name from a path using JavaScript?

how can i get the last folder name and file name from a full path,I need a solution for this. 如何从完整路径中获取最后的文件夹名称和文件名,我需要一个解决方案。


Example: 例:

d:\folder1\folder2\assets\images\62f9a0f4-98b9-4dd0-8047-ed1a3cc306cf.png

In my case,I just need \\images\\62f9a0f4-98b9-4dd0-8047-ed1a3cc306cf.png from the path in Javascript. 就我而言,我只需要从Javascript路径中\\ images \\ 62f9a0f4-98b9-4dd0-8047-ed1a3cc306cf.png。

Not the ideal solution, but gets the work done. 不是理想的解决方案,但可以完成工作。 I had to escape the \\ while creating the string 创建字符串时,我必须转义\\

var arr = "d:\\folder1\\folder2\\assets\\images\\62f9a0f4-98b9-4dd0-8047-ed1a3cc306cf.png".split("\\");

    var length = arr.length, path = "\\" + arr[length-2] + "\\" + arr[length-1];

    console.log(path);

This should do it: 应该这样做:

 var str = "d:\\\\folder1\\\\folder2\\\\assets\\\\images\\\\62f9a0f4-98b9-4dd0-8047-ed1a3cc306cf.png"; var ret = str.split("\\\\").reduce((p,c,i,arr) => {if(i >= arr.length - 2){return (p?("\\\\"+p+"\\\\"):"")+c}}); console.log(ret); 

var url = window.location.pathname;
var filename = url.substring(url.lastIndexOf('/')+1);
var indxend = url.lastIndexOf('/')-1
var folderName =url.substring(0,indxend).substring(url.substring(0,indxend).lastIndexOf('/')+1);

Give this a go: 试试看:

 var path = 'd:\\\\folder1\\\\folder2\\\\assets\\\\images\\\\62f9a0f4-98b9-4dd0-8047-ed1a3cc306cf.png'; var parts = path.split('\\\\'); var file = parts.pop(); var fileAndPar = parts.pop() + '\\\\' + file; console.log(fileAndPar); 

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

相关问题 如何从javascript中的文件夹路径获取最后一个文件夹名称? - How to get last folder name from folder path in javascript? 在javascript中从URL路径获取最后的文件夹名称getJSON Api - Get last folder name from URL path , getJSON Api , in javascript 如何使用 JavaScript 从完整路径获取文件名? - How to get the file name from a full path using JavaScript? 当仅知道文件夹路径时,使用javascript / jquery从url获取当前文件名 - Get current file name from a url using javascript/jquery when only folder path is known 如何使用Ruby on Rails中的文件上传控件仅获取文件夹路径名而不获取文件名? - how to get only folder path name without file name using file upload control in Ruby on rails? 如何使用 javascript 从不同的文件夹文件和 html 文件名中获取数据 - how to Get data from different folder file and that html file name using javascript 使用JavaScript从完整路径获取不带GET参数的文件名? - Get the file name without GET parameters from a full path using JavaScript? 如何使用php和javascript从目录获取文件名 - How to get a file name from a directory using php and javascript Javascript从路径中获取文件名和类型作为字符串 - Javascript get file name and type from path as string 使用php或javascript从文件夹中获取文件名到页面中 - getting a file name from a folder into page using php or javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM