Qt get File directory based on directory content - qt

I have few directorates and one file *.txt under one Directory A, Directory path is in QString
say c:/A/1/2/3/4
c:B/C/A/1/2/3/4/5/6
In my code I have only the full path, now I want to get the directory until A(name of A can change anytime) based on the file *.txt
inshort:- I want to parse all directory and get the directory until the place where *.txt present from right to left

Are you trying to get the string of the file path to A? Ex. If the file path was:
C:/Users/Admin/Desktop/file.txt
you want to get:
C:/Users
If so, all you have to do is find the second index location of "/". You could do something like this:
QString filePath = "C:/Users/Admin/Desktop/file.txt";
int index = filePath.indexOf("/");
index = filePath.indexOf("/",index+1);
QString shortenedPath = filePath.mid(0,index);
This should give you:
C:/Users/
If that isn't what you meant, then sorry for the long response.

Related

How to convert from File to git path?

Im using jgit, log() command, and the addPath(String path) requires me to input a git compatible path, that is, a relative path like src/java/com/foo/Test.java. But what I got is a File object, that will have an absolute path of something like: c:\hello\irrelevant\myproject\src\java\com\foo\test.java.
How to convert from this to the git path? Is there some handy function within jgit itself? I cant find it....
You can use Path::relativize to get the relative path between the file to add and the work directory.
For example:
File workDir = git.getWorkTree(); // e.g. "/path/to/workdir"
File file = new File("/path/to/workdir/foo.txt");
Path relativePath = workDir.toPath().relativize(file.toPath());
assertEquals("foo.txt", relativePath.toString());

Save files into specified directory in Qt

I'm trying to save a stringlist of files into a specified directory. All I found was saveFileDialog where I save only one file at a time. Is there any other way to save multiple files into the target folder?
The QFileDialog ist for a user to choose a location for saving, not the actual saving process. How you save a list of files depends on what exactly you have, if you mean a QStringList of file locations you simply want to copy the easiest way would be something like this:
QStringList input_file_locations;
QString output_file_location = QFileDialog::getSaveFileName(...);
for (int i = 0; i < input_file_locations.size(); i++)
{
QFile::copy(input_file_locations.at(i), output_file_location + QString::number(i));
}
(I did non add the extraction and preservation of the actual file name to keep the example as simple as possible)

Save an image to outside the directory in asp.net

I am trying to save an image to server.Server directory structure is as follows
httpdocs-
Folder-
Page.aspx
and
httpdocs-
Images-
Subimages
The Folder and Images are under the httpdocs. I need to save the image to subimages folder from pages.aspx. Saving image code is on pages.aspx.
I tried
string CroppedImagePath = Server.MapPath("~/Images/Subimages"+file)
But not get the exact result
Unless file contains a leading / character, that's going to end up saving as something like:
~/Images/Subimagesfilename.ext
Use Path.Combine() to build a path name. Something like this:
var CroppedImagePath = Path.Combine(Server.MapPath("~/Images/Subimages"), file);
try this
string CroppedImagePath = Server.MapPath("~/Images/Subimages/"+file)
/ after Subimages

How to find a file path in current java directory

I have a function that gets a file path as an input. input file is located in a folder in my project (etc/xsd/template.xsd). how can i set this path?
this ia my function:
JAXBUtilityTool tool = new JAXBUtilityTool("etc/xsd/template.xsd","src.com.classes");
and it can not find the file "etc/xsd/template.xsd"
System.getProperty("user.dir") does not help since when I add the rest of path to it:
System.getProperty("user.dir")+ "etc/xsd/template.xsd"
result is c:\eclipse\myworkingdirectory\project/etc/xsd/template.xsd
Try:
String path = System.getProperty("user.dir");
path = path.replaceAll("\\","/") + "/etc/xsd/template.xsd"
Source:
String.replaceAll single backslashes with double backslashes

pass permanent parameter to a jar file

I have 3 jars: jar1, jar2 and jar3, in the same path who can change in other pc (ex: c:\prova)
When I run jar1, it moves jar2 in the Windows Sturtup folder.
I want that jar2 simply activate jar3 at every windows startup, but of course it doesn't find jar3 who is remained in the first path.
So I want that jar1 pass a reference (in this case the path c:\prova) to the jar2, when moving it, or at least on the first call to it.
I find it difficoult because:
I can't write the path in a text file in jar2: text files in jars aren't writable.
I can't write the text file in the windows Startup folder: it will be opened at every win startup..
I can't pass the path as a parameter, it will be good for the first call but I can't store this value for the succesive calls.
Sorry for my bad english, thanks for any help!
To add the file Path.txt (with jar3's path) in jar2:
Runtime.getRuntime().exec("jar uf jar2.jar Path.txt");
To read the file in jar2 (Startup is my class name):
String s = "/Path.txt";
is = Startup.class.getResourceAsStream(s);
br = new BufferedReader(new InputStreamReader(is));
while (null != (line = br.readLine())) {
list.add(line);
}
Thank me!

Resources