Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can i make a path short in a string?

This is just a example the string can something else.

C#
string test = "xcopy c:\test\test\test\name.bat d:\test\test\test\hi.bat"


and i want to have

C#
string test = "xcopy c:\..\name.bat d:\..\hi.bat"



other example

string test = "whatever c:\test\test\test\name.bat whatever"
to
string test = "whatever c:\..\name.bat whatever"

other example

regedit.exe /e:a "D:\data\backup\laptop\CCleaner\CCleaner.reg" "HKEY_CURRENT_USER\Software\Piriform\CCleaner"

to
regedit.exe /e:a "D:\..\..\..\..\CCleaner.reg" "HKEY_CURRENT_USER\..\..\CCleaner"

at autoit i can do this with

Func _shortstringpath($data)
Return StringRegExpReplace($data, '("[^\\]+\\)(?:[^\\"]+\\)*([^"]+")', "$1...\\$2")
EndFunc

i want to do this in c# 2.0

this is the link with the question for autoit code but now i need it in c#
https://www.autoitscript.com/forum/topic/166301-short-path-anywhere-at-the-string/[^]
Posted
Updated 4-Jun-15 11:08am
v7
Comments
Andreas Gieriet 4-Jun-15 16:14pm    
Why do you need a relative ("short"?) path?
How do you know what the reference (current?) directory is?
For relative paths, you need a reference: relative to what?
E.g. if you intend to write that xcopy command to a batch file that will later be executed somewhere, you need to set the current directory (cd ...) before that relative call.
And: as others noted: there is no way to combine relative with absolute paths. You might use wildcards instead, but this is very uncommon and error prone.
Cheers
Andi
[no name] 4-Jun-15 17:01pm    
i do not expect to run the rule its for showing in a notitfication

You can use relative paths - which is what the ".." bit in a path means: "the directory above the current", "." means "the current directory".
But you can't combine a relative path with any part of an absolute path specification.
So if you start your path with an absolute specification like "C:\" you can't then use a relative path specifier in the path and expect it to work!

Why not just use File.Move[^]?
 
Share this answer
 
try this method :
C#
public static string GetShortPath(string fullPath)
       {
            var ellipsesPath = "";

            if (string.IsNullOrEmpty(fullPath)) return ellipsesPath;
            var dirName = fullPath.Split('\\');
            var fileName = Path.GetFileName(fullPath); 
            if (fileName.Length > 25)
            {
                fileName = fileName.Substring(0, 15) + "..."+ fileName.Substring(fileName.Length - 5, 5);
            } 
            ellipsesPath = Path.GetPathRoot(fullPath) + (dirName.Length > 2 ?@"..\" : "") + fileName; 
            return ellipsesPath;
       }

and use it like this :
C#
string shortPath= GetShortPath(@"d:\tester\test\test\hi.bat");
 
Share this answer
 
v2
C#
string pattern = @"(""[^\\]+\\)(?:[^\\""]+\\)*([^""]+"")";
string data = regedit.exe /e:a "D:\data\backup\laptop\CCleaner\CCleaner.reg" "HKEY_CURRENT_USER\Software\Piriform\CCleaner"
string replacement = "$1...\\$2";
Regex rgx = new Regex(pattern);
data = rgx.Replace("\"" + data + "\"", replacement);
data = data.TrimStart('"');
data = data.TrimEnd('"');
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900