How can i get the paths of a folder and its content. Say i have folder named MyFolder as
/tmp/MyFolder/ where it has subfolders SubFolder1, SubFolder2... and some files
You may take a look at the opendir() family functions.
A more efficient way than {open,read,close}dir() is Linux' getdirentries() function. See getdirentries(3) for details.
Use dirent and dir structures. You can use opendir(), readdir() etc for manipulation.
readdir() will give one name at a time and you can keep calling it iteratively.
Related
I'm trying to use Lua's os.tmpname() on Windows (which uses tmpnam() under the hood), and I'm a bit puzzled by the filenames it is returning.
> print(os.tmpname())
\s3e8.
If I feed it directly to Lua like io.open(os.tmpname(), "w"), it will attempt to create the file in the root directory of the current drive. This seems pretty inappropriate, since we often don't have the permission to do that.
But according to this thread:
https://mingw-users.narkive.com/L7VR1gxX/temporary-file-woes
This is apparently supposed to be a path relative to the current directory. They mentioned this snippet from a Microsoft documentation:
Note than when a file name is prepended with a back slash and no path
information, such as \fname21, this indicates that the name is valid
for the current working directory.
But I can no longer find the documentation they mentioned in the thread.
I googled around, trying to find the latest documentation and found this:
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/tempnam-wtempnam-tmpnam-wtmpnam?view=msvc-160
This is the generation rules mentioned in the documentation above:
_tempnam will generate a unique file name for a directory chosen by the following rules:
If the TMP environment variable is defined and set to a valid
directory name, unique file names will be generated for the directory
specified by TMP.
If the TMP environment variable is not defined or if it is set to the
name of a directory that does not exist, _tempnam will use the dir
parameter as the path for which it will generate unique names.
If the TMP environment variable is not defined or if it is set to the
name of a directory that does not exist, and if dir is either NULL or
set to the name of a directory that does not exist, _tempnam will use
the current working directory to generate unique names. Currently, if
both TMP and dir specify names of directories that do not exist, the
_tempnam function call will fail.
I do have the TMP environment variable defined, but Lua always generates the kind of paths I mentioned at the top.
So really, I have two questions:
Is this actually supposed to be a relative path?
If so why does it always generate a relative path, instead of using the standard TMP variables?
I'm testing this with Lua 5.2 from LuaBinaries and the Lua statically built into shinchiro's mpv builds.
According to this thread on lua-users.org you can try passing null.
well i solved it using tmpname(NULL,NULL)... it considers the
temporary directory of the system.
when i use tmpnam() on windows on a non-writable root, the filename
generated (\something) is not writable...
i guess it's an implementation bug
Also note the Lua 5.2 Reference Manual
When possible, you may prefer to use io.tmpfile, which automatically removes the file when the program ends.
I need to check the files of a versioned system. To do that, I need to write a batcha program so to compare the contents of several folders containing the repositories.
So, my question is: how can I "read" the names of all the subfolders inside a folder, so to use these names later to find subfolders having the same names in a different repositories?
I suppose I may use DIR to print on the screen a list of these names but I don't know how to write it on a text file and then read it. Moreover, I should edit this kind of list, anyway.
Any suggestions or new ideas to solve this problem?
I thank gratefully who ever will answer.
it seems that you can get the subfolders using batch file from perl as follows:
system("start C:\\Temp\\mybatchfile.bat");
or you might try to pass your command suggested by #Stephan straight to system and try to handle what it is returned.
How to create a file in %temp% folder using C program. Please provide me some Ideas on it.
I am programming in windows using Mingw.
Update:
I want the file to be in .log or .txt format. How to do that?
GetTempPath + GetTempFilename
Example here
You can use Windows API GetTempPath() (see http://msdn.microsoft.com/en-us/library/windows/desktop/aa364992(v=vs.85).aspx ), build full path and then either use Win API or C functions to create file
You can use the WinAPI function GetTempPath to get the path of the temp directory.
Use getenv() to read the environment variable, then just figure out a filename and fopen() it, for example.
Note that the part where you "figure out a filename" is hard, it's better to use e.g. mkstemp() or tmpfile().
char * pTemp;
pTemp = getenv ("TEMP"); /*Get temp folder path*/
if (pTemp!=NULL)
{
printf ("Temp folder path is %s",pTemp);
}
I need to read file in my program so while providing path I want to give relative paths because all files to be opened will be in some folder within current folder.
I tried this:
FILE *f=fopen("./abc/p.txt","r")
abc is folder withing current folder, but fopen returns NULL. How to do this thing?
This comes from either one of those:
. or ./abc/ is not readable or traversable
./abc/p.txt is not readable
./abc/p.txt does not exist
./abc/p.txt is a broken link
Look at errno to know what's the real problem.
this will run:
FILE *f=fopen("...\\abc\\p.txt","r");
I am searching for a solution to delete all files from my pendrive using a C program. I don't want the code to detect the pendrive as that's not a concern right now. I would appreciate any links that could help me out on this.
Note: I am working on Windows 7 64-bit and I want to delete the entire contents from my pendrive, which contains .exes and .dlls.
Thanks and Regards,
Radix
The non-hack correct way to do this, is via the Virtual Disk Service.
If you can't use something like rm -rf F:\* and you really do need to implement it yourself in C then I think I'd probably opt for a recursive solution based on FindFirstFile and FindNextFile. These are the native Windows APIs for enumerating directories. Not remotely portable, but it doesn't seem that's a requirement.
Basically the idea is that you write a function, EmptyDir(), say, whose job it is to delete the contents of a directory. The function uses FindFirstFile and FindNextFile to walk the contents of the directory. When a file is encountered it is deleted. When a directory is encountered, EmptyDir() is called recursively. The last job of EmptyDir(), before it returns, is to delete the now empty directory.