I will get environment 'APPDATA' and get back to previous directory from APPDATA\Roaming to APPDATA, then I want to concantenate with another directory APPDATA\Local\ finally replace '\' into '\'. Currently I had problem with it. How can I go back directory ?
Codes suppose to be
#include <string>
#include <iostream>
#include <algorithm>
char* path;
path= getenv("APPDATA"); <!--GetEnv APPDATA-->
??? <!-- Go back directory -->
strncpy(path,"Local\\stuff,12); <!-- add front directory-->
<!-- Replace slash to double slash -->
std::string s = path;
std::replace(s.begin(),s.end(), '\','\\');
Are you trying to remove the last directory in a string?
If so, you can use std::string::find_last_of( "\\" ) to find the last slash then use the return value to create a substring. The following example will do that.
std::string path = getenv("APPDATA"); //<!--GetEnv APPDATA-->
//??? <!-- Go back directory -->
std::size_t slashPosition = path.find_last_of( "\\" );
// Remove slash at the end if found easier to handle if trailing slash is/not found)
path = path.substr( 0, slashPosition );
path += "\\Local\\stuff"; //<!-- add front directory-->
I removed the code for replacing the single back with double since it would not work as written and I don't think it was necessary. I also used a std::string for the path variable to take advantage of methods in std::string.
Related
I am trying to go through a predefined dir path set but I only have the prefix of the path
I have tried use include? or find a method that does that.
I can only think about the trivial solution of define a regexp and go for each in the set, but that seems to be not so ruby-like style
require 'set'
legal_paths = Set['A/B/C', 'A/D/E', 'A/F/G']
Dir.glob('**/system.log').each do |fd|
if failed_tests_path.include?(fd) #fd for example = A/F/G/E/system.log, A/B/C/K/system.log etc...
puts fd
end
end
I want fd to be only system.log files that the paths are including inside the set (the set holds the prefix to the path)
If these are only the prefix, try String#start_with?:
require 'set'
legal_paths = Set['A/B/C', 'A/D/E', 'A/F/G']
files = Dir.glob('**/system.log').select do |fd|
fd.start_with?(*legal_paths)
end
Why not make use of the fact that you can specify these prefixes in the glob?
legal_paths = ['A/B/C', 'A/D/E', 'A/F/G']
files = Dir.glob("{#{legal_paths.join(',')}}/**/system.log")
Note though that if the legal_paths are input by the user, the above might not be secure as the user could traverse to parent directories using ...
I am trying to develop a small search engine application to search for my local files in my C:// directory by typing their names as input.
I received this suggestion as an implementation to the search function. However, it only allows me to search with the exact name of the file e.g "1234_0506AB.pdf"
I want my search function to take "1234" as an input and still be able to fetch "1234_0506AB.pdf".
Another suggestion was: instead of simply testing for equality via dir_entry.path().filename() == file_name, just get the filename as string dir_entry.path().filename().string() and do a .find(). For even more general searching, you could use a regex and match that with the filename.
I have very little experience in that and require some help and guidance to use .find() or regex in my code.
#include <filesystem>
#include <algorithm>
namespace fs = std::filesystem;
void search(const fs::path& directory, const fs::path& file_name)
{
auto d = fs::directory_iterator(directory);
auto found = std::find_if(d, end(d), [&file_name](const auto& dir_entry)
{
return dir_entry.path().filename() == file_name;
});
if (found != end(d))
{
// we have found what we were looking for
}
// ...
}
To use find, you should check the documentation of the method, which also contains some examples on most sites.
In your code, you accept a filename if its exactly the same as the one you are looking for:
return dir_entry.path().filename() == file_name;
To accept substring matches, you'll have to modify this check to use find instead of ==. As mentioned in the linked doc, find returns npos if it can't find a match.
return dir_entry.path().filename().string().find(file_name.string()) != std::string::npos;
If you are looking for matches only at the beginning of the string, you could use == 0instead of != npos.
But in this case, you have other options too, for example using substr to cut the filename to the desired length:
return dir_entry.path().filename().string().substr(0, file_name.size()) == file_name.string();
For a solution using regex, check the regex examples on the same site.
Is there any way to remove part of URL ?
I have a path of file with specific extension , and I want remove file name and extension.
Here is my code:
QString path;
if (path.right(3) == "jpg")
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
else
?
for example :
I want
C:\Users\me\Desktop\
instead of
C:\Users\me\Desktop\file.exe
You can use the QFileInfo class and the absolutePath method:
QString filePath = QFileInfo(path).absolutePath();
Search backwards through the string for the first occurrence of '/' or '\' and stop.
I am trying to write an XML file using MSXML4. It works fine except when I have a data element with a trailing space which must be preserved.
Given the following code to insert a new element:
const _bstr_t k_Parent (ToBSTR("ParentNode"));
const _bstr_t k_Child (ToBSTR("ChildNode"));
const _bstr_t k_Data (ToBSTR("DataWithTrailingSpace "));
const _bstr_t k_Namespace (ToBSTR("TheNameSpace"));
MSXML2::IXMLDOMDocument2Ptr m_pXmlDoc;
m_pXmlDoc->async = VARIANT_FALSE;
m_pXmlDoc->validateOnParse = VARIANT_FALSE;
m_pXmlDoc->resolveExternals = VARIANT_FALSE;
m_pXmlDoc->preserveWhiteSpace = VARIANT_TRUE;
MSXML2::IXMLDOMNodePtr pElement = m_pXmlDoc->createNode(NODE_ELEMENT, k_Child, k_Namespace);
MSXML2::IXMLDOMNodePtr pParent = m_pXmlDoc->selectSingleNode(k_Parent);
pElement->put_text (k_Data);
MSXML2::IXMLDOMNodePtr pNewChild = pParent->appendChild(pElement);
If I check "pNewChild->text", the text still contains the trailing space. When I try writing it to a file:
std::string xml (static_cast<std::string>(m_pXmlDoc->xml));
std::ofstream file("output.xml");
file << xml << std::endl;
file.flush();
file.close();
The output is:
<ParentNode>
<ChildNode>DataWithTrailingSpace</ChildNode>
</ParentNode>
Instead of (note the extra space behind "DataWithTrailingSpace"):
<ParentNode>
<ChildNode>DataWithTrailingSpace </ChildNode>
</ParentNode>
I cannot figure out at what point the trailing space is getting stripped.
Can someone please provide some insights in to where this may be occurring and how I can correct it?
If you need to preserve whitespace then you should be using a CDATA section via createCDATASection() or the like.
Mystery solved. Don't preview your XML in Internet Explorer. It hides trailing spaces. Use notepad instead.
You should replace the whitespace(s) with . That way your whitespaces should persist.
EDIT
Appearantly it didn't solve your problem. Then maybe you want to have a look at these sites:
http://msdn.microsoft.com/en-us/library/ms757008(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms757885(VS.85).aspx
I am writing a small windows script in javascript/jscript for finding a match for a regexp with a string that i got by manipulating a file.
The file path can be provided relative or absolute. How to find whether a given path is absolute/relative and convert it to absolute for file manipulation?
How to find whether a given path is absolute/relative ...
From the MSDN article Naming Files, Paths, and Namespaces:
A file name is relative to the current directory if it does not begin with one of the following:
A UNC name of any format, which always start with two backslash characters ("\\"). For more information, see the next section.
A disk designator with a backslash, for example "C:\" or "d:\".
A single backslash, for example, "\directory" or "\file.txt". This is also referred to as an absolute path.
So, strictly speaking, an absolute path is the one that begins with a single backslash (\). You can check this condition as follows:
if (/^\\(?!\\)/.test(path)) {
// path is absolute
}
else {
// path isn't absolute
}
But often by an absolute path we actually mean a fully qualified path. In this is the case, you need to check all three conditions in order to distinguish between full and relative paths. For example, your code could look like this:
function pathIsAbsolute(path)
{
if ( /^[A-Za-z]:\\/.test(path) ) return true;
if ( path.indexOf("\\") == 0 ) return true;
return false;
}
or (using a single regex and a bit less readable):
function pathIsAbsolute(path)
{
return /^(?:[A-Za-z]:)?\\/.test(path);
}
... and convert it to absolute for file manipulation?
Use the FileSystemObject.GetAbsolutePathName method:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var full_path = fso.GetAbsolutePathName(path);
To check whether the path is relative or absolute, look for a leading /.
If it doesn't have one, you need to concatenate the path to a base path. Some programming environments have a "current working directory", but Javascript that lives in the browser doesn't, so you just need to pick a base path and stick to it.
function full_path(my_path) {
var base_path = "/home/Sriram/htdocs/media";
var path_regex = /^\/.*$/;
if(path_regex.test(my_path)) {
return my_path;
} else {
return base_path + my_path;
}
}
Paths can contain newlines, which the javascript regex . won't match, so you might want to develop a more sophisticated regex to make sure all paths will work properly. However, I'd consider that outside the scope of this answer, and of my knowledge. :-)