Developer forum

Forum » Templates » Razor question on how to get something like a substring where i Remove the filename from a path

Razor question on how to get something like a substring where i Remove the filename from a path

Hans Ravnsfjall
Hans Ravnsfjall
Reply

Sorry for my lack of Razor knowledge, but my mission is

I have a field that has a complete path to a file  like /Files/Images/Ognir/Vagar/Gardsvegur_22_Sorvagur/00.JPG

Now, i want to get the value of the path minus the filename and extension. So my output would be /Files/Images/Ognir/Vagar/Gardsvegur_22_Sorvagur

I have tried to google a solution for how to do this in Razor, but no luck - and since I am under time pressure, i´am trying a bit of a long shot, and hope some of you will grant me an early Christmas gift and indicate a solution.

Any help is really apreciated.

Thanks in advance.

 

br Hans


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

Something like this should work:

string input = "/Files/Images/Ognir/Vagar/Gardsvegur_22_Sorvagur/00.JPG";
string folder = input.Substring(0, input.LastIndexOf("/"));

LastIndexOf gets the numeric position of the last occurence of the /. Then SubString takes the original string starting at 0 up to the last index of the /, giving you /Files/Images/Ognir/Vagar/Gardsvegur_22_Sorvagur

You could use it as-is if you know that your path always has a / in it. Otherwise, you need to create a helper method that checks the string and returns an emtpy string when you can't extract a valid path from its value.

 

Votes for this answer: 1
 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Thanx a lot Imar. I will try that.

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi Again

 

I am trying to grab this inside an foreach loop, and theres proably a whole lot wrong with my code, but maybe anyone would be so kind to indicate what I am doing wrong?

 

My code is

 

@foreach (LoopItem i in GetLoop("Products")){
        

string input = i.GetValue("Ecom:Product:Field.Images.Clean");
string folder = input.Substring(0, input.LastIndexOf("/"));


   foreach(var file in GetLoop(folder.ListOfFiles)){

    GetValue(folder)/@file.GetValue(folder.FileName);
 
    }
 

      
    }

 

 

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Error message I get is:

Line 28: Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)
Line 32: 'string' does not contain a definition for 'ListOfFiles' and no extension method 'ListOfFiles' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Solved it with this


 
@foreach (LoopItem i in GetLoop("Products")){


    string input = i.GetValue("Ecom:Product:Field.Images.Clean").ToString();
if (input != "") {
string domain = "http://www.skyn.fo";
string comma = ", ";
 string folder = input.Substring(0, input.LastIndexOf("/")).ToString();
string folderOnDisk = System.Web.HttpContext.Current.Server.MapPath(folder);
  var directoryInfo = new System.IO.DirectoryInfo(folderOnDisk);
  foreach (var fileInfo in directoryInfo.GetFiles("*.jpg")){
@domain@(string.Format("{0}/{1}", folder,  fileInfo.Name))@comma

              }
        }
    }

 

 

You must be logged in to post in the forum