How to convert legacy "C:\Documents and Settings" paths to "C:\Users" - winapi

I have some legacy data that was created on Windows XP. It contains absolute file names to files in users' My Documents directories. On Windows 7, these absolute references are no longer point to the correct place. For example what was "C:\Documents and Settings\Gareth\My Documents\readme.txt" should now be "C:\Users\Gareth\Documents\readme.txt".
Many Win32 functions are happy to take the file names under "C:\Documents and Settings" and to behind the scenes convert them to the new file names, however there are functions we use that don't do this. Is there a Windows API that I can use to convert these file names to the new locations?
Obviously, I could do string search and replace, but that wouldn't be guaranteed to work under all circumstances. Because there are Windows functions that seem to do the substitution it feels like there ought to be a published function I can call to do the same thing.
Just to clarify my question: I'm looking for a function that does this:
Input: "C:\Documents and Settings\Gareth\My Documents\readme.txt"
Output: "C:\Users\Gareth\Documents\readme.txt"
or
Input: "C:\Documents and Settings\Gareth\My Documents\"
Output: "C:\Users\Gareth\Documents\"

This works with symbolic links. That should work on any function that takes a path, MSDN documentation is here: msdn.microsoft.com/en-us/library/aa365680%28v=VS.85%29.aspx
I verified MapFileAndCheckSum(), it works fine. Windows7, compiled with UNICODE in effect.
#include "stdafx.h"
#include <windows.h>
#include <assert.h>
#include <imagehlp.h>
#pragma comment(lib, "imagehlp.lib")
int _tmain(int argc, _TCHAR* argv[])
{
DWORD hdrsum, chksum;
const wchar_t* path = L"c:\\documents and settings\\hpassant\\my documents\\test.exe";
DWORD retval = MapFileAndCheckSum(path, &hdrsum, &chksum);
assert(retval == 0);
return 0;
}

Is it what you need?
%USERPROFILE%\My Documents

I'm not sure where exactly you're using this (and it's been a while since I've used Windows), but I remember being able to use the %HOMEPATH% variable to get the current user's Documents & Settings directory: \Documents and Settings{username}. I think that this works with the updated Windows 7 paths, too.

Related

opening all the image files of type jpg or png in a folder with Visual Studio 2019 C++

How can I get the list of files in a directory using C or C++?
In the above answered question, there was sample code for C++ iterating through all the files of a single folder, which looked like this:
#include <string>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
std::string path = "/path/to/directory";
for (const auto & entry : fs::directory_iterator(path))
std::cout << entry.path() << std::endl;
}
but when I tried incorporating it into my program where I can already successfully open one single .jpg file or .png file at a time (yay!), I got the following 2 problem from the Visual Studio 2019 C++ compiler:
namespace "std" has no member "filesystem"
name followed by '::' must be a class or namespace name
And I guess, it's a simple syntax error in VS2019 C++, related to namespace but I don't know how to fix it. If I understand C++ (and it has been a few years), namespace declarations are a shortcut to concise code but still being sure the compiler uses the correct class when there are over(what was the term) of the same name from like different classes? In other words,
namespace fs = std::filesystem;
will replace all instances of fs with std::filesystem to allow cleaner code.
I can get rid of the first error by replace fs with std::filesystem
for (const auto & entry : std::filesystem::directory_iterator(path))
but the second error remains, so I still don't understand this error. Is it not finding filesystem from the standard library, and do you have to manually do something in VS2019 C++ to be able to #include ?

How to remove 7z word from zipped files?

As i mentioned at the title i need to remove 7z word from the zipped folder. If i will create a 7z archive it just looks like this.
If i use n++ to open it it looks like this. Any way to remove just at the start (the other is the folder name inside the zip).
7z¼¯' m® : E†D¶€ 7 z f o l d e r
42
»Ó
Here is the file: https://drive.google.com/open?id=18fpX2DzJkt6j0RqgkwWimu_QR-jT5_Qn
If it's possible from dll's can anyone do and send me(i dont know how)
Thanks
If it's possible to remove first 2 letter without messing the remaining and put it back when it's needed using batch or c i can take that.
Well just making a simple c file works fine.
Code:
#include <stdio.h>
int main(int argc, char *argv[]){
FILE *file;
file=fopen(argv[1],"r+b");
fprintf(file,"text");
}
Btw the text shouldn't be longer than 5 chars or you will corrupt the archive.
For making the archive readable.
Code:
#include <stdio.h>
int main(int argc, char *argv[]){
FILE *file;
file=fopen(argv[1],"r+b");
fprintf(file,"7z¼¯'");
}

Inno Setup Unknow Filename Prefix

I'm trying to set the output folder of the compiled installer to the desktop.
This script can be runned by multiple people across multiple computers, that's why I need to use a dynamic script.
OutputDir= "{#Desktop}"
I'm able to use the prefix userdocs: but not userdesktop:.
//Works:
#define Path "userdocs:Visual Studio 2013\Projects\"
//Throws "Unknow filename prefix userdesktop:"
#define Desktop "userdesktop:";
Here is the documentation:
{userdesktop} & {commondesktop} *
The path to the desktop folder.
{userdocs} & {commondocs}
The path to the My Documents folder.
I also tried commomdesktop: with no success.
In that case you may want to use ISSI and its Constants.
#define ISSI_IncludePath "..\Inno Setup 5\ISSI\" ;path to ISSI
#include ISSI_IncludePath+"\_issi.isi"
[Setup]
OutputDir={#ISSI_myUserDesktop}

What cmd can I use to find all (text) files whose name is of some form in a directory?

I would like to write a bash script that runs a .cpp program on a number of text files (I'll henceforth refer to those text files as input files). The .cpp program assumes that there is an input text file in the same directory level called "input.txt". However, the inputs I would like to run through the program are in a sub-directory and there are more than one input txt files that I would like to run through the program.
The input txt files are named using the following convention:
input1.txt, input2.txt, input3.txt, etc,...
They are placed in a folder called "myInputs" which contains text files with similar name e.g., input1.txt.Sol that I'm not interested in.
The commands I would like to have in my script should look in 'myInputs' for txt files of the form: input%.txt where % may be a number. For each file of that form, the script should print out the name of the file on a line-by-line basis. i.e., input1.txt should be on its own line, input2.txt should be on it own line.
Any tip is appreciated!
There is a bash command called find
http://unixhelp.ed.ac.uk/CGI/man-cgi?find
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] ) {
(void)system( "find \"MyInputs\" \"input*.txt\"" );
/* whatever comes next */
return 0;
}
Unfortunately, I don't know that much C++, but here's a snippet in C

Where do I set the Xcode IDE property to read a data file to avoid No such file or directory

This is not a code question but an Xcode IDE question.
For those who pride themselves in code analysis, that's good, but in this case, please try to focus on the question in the Title above not the code.
In the Xcode IDE I created a new project and selected a "Command Line Tool" creating ReadAFile.
I then entered some simple code into main.c to read and display the contents of "Data.txt".
I then selected File, New, File, and selected Empty (For an empty document) and named it Data.txt and then I typed out a sentence then saved the file.
In Xcode, when I choose Product, Run, I get the following error:
Error while opening the file.
: No such file or directory
Program ended with exit code: 1
Where do I set the Xcode IDE property so that it knows where my data file is to avoid getting the "No such file or directory" error?
When I enter the very same code into ReadAFile.c and I type out the very same file "Data.txt" at a terminal prompt using vim, save it, and run make ReadAFile, the code executes properly.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char * argv[]) {
char ch, file_name[25];
FILE *fp;
strcpy(file_name,"Data.txt");
fp = fopen(file_name,"r");
if( fp == NULL ) {
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are :\n", file_name);
while( ( ch = fgetc(fp) ) != EOF )
printf("%c",ch);
fclose(fp);
return 0;
}
That's a good question... I have a temporary, work around, answer...
I do respect your request to focusing on the question rather than your code.
As a quick temporary answer, the only answer I have for you at this point is to modify your code, for now, if you are only debugging your code along with your data file. I know hard coding your file locations is a no-no but for debugging purposes the following will get you over this bump in your road. You can try to hard code the directory location along with your data file so that your code can find it. For example, let's say that your Users directory on your Mac is jerryletter, you could copy your Data.txt file to your Users directory ~/jerryletter. Then could modify just one line of your code like the following:
"strcpy(file_name,"/Users/jerryletter/Data.txt");"
DougPan.com

Resources