I need to write a program that takes a root directory and a minimum file size from the user and then counts all files that are greater than the size the user enters in all sub directories.
For some reason my recursion only counts files that are in the current working directory and does not count those in the sub directories. Any clues on how to fix this? It is correctly counting the number of files in the working directory though.
You need to specify path correctly when you call the function recursively. dentry->d_name refers the file name only; it does not contain the directory name it is belong.
Otherwise, it will try to recurse into possibly non-existing directory. (Or,will try to recurse into same directory infinitely if there's a directory whose name is same as the parent directory).
char path[PATH_MAX];
....
snprintf(path, sizeof path, "%s/%s", fileName, dentry->d_name);
countFiles(path, count, size);
Related
I have a directory called scratch. Within that directory are subdirectories with random names. Within each of those randomly named subdir's there is a folder called 'deagg' and also additional folders. Within those additional folders are more folders called 'deagg'. I ONLY want to print the 'File' aka summary.txt that is in the highest level 'deagg' folder.
Example of the directory:
C:\scratch\PGA\deagg\randomfolder\summary.txt
C:\scratch\PGA\asdf\randomfolder\deagg\summary.txt
C:\scratch\LMK\deagg\randomfolder\summary.txt
C:\scratch\LMK\asdf\randomfolder\deagg\summary.txt
So I want to os.walk through the scratch directory, look for all highest level 'deagg' folders to print out their summary.txt (File variable I defined). I do NOT want to have it find those other 'deagg' folders that are deeper within the path.
Normally I'd just use the [2] to get the 'deagg' folders that are in place 2, but the Start_Path changes and it might not be called scratch, maybe there are more directories in the way. So I can't define the highest level 'deagg' using a list index like that. Any ideas on how to specifically target the highest level 'deagg' folder if the path changes length?
Maybe the path turns to:
C:\scratch\projects\PGA\deagg\randomfolder\summary.txt
How would I adjust for the different directory length, since using [2] wouldn't work since 'deagg' isn't in [2] anymore? I do not want to be changing that index number every time this code is run.
Start_Path = r'C:\scratch\\'
File = 'summary.txt'
def extract_data():
for root, dirs, files in os.walk(Start_Path):
if File in files and root.split(os.path.sep)[2] == 'deagg':
print(File)
EDIT
The existed directory is not necessarily a sub-directory of the home directory. It can be a sub-directory of a sub-directory of the home directory.
End of EDIT
I am reading a user input (e.g., cd existedDirectory) to change the current directory to the "existedDirectory". I know that
chdir(getenv("HOME"));
can change the current directory to home directory, so I tried the following methods:
chdir(getenv("/existedDirectory"));
chdir(getenv("existedDirectory"));
chdir(getenv("~/existedDirectory"));
chdir("/existedDirectory");
chdir("existedDirectory");
chdir("~/existedDirectory");
Nothing worked. Any help will be greatly appreciated.
chdir takes a string argument with a path to change to. It does no other special handling (environment vars or home or anything else), so you if you want any of those things, you need to build a string to pass into it.
chdir("/existingDirectory");
which change to and existing directory in the root directory -- it has an absolute path. So it has to be one of the directories you see when you run ls /.
chdir("exisitingDirectory");
will change to an existing subdirectory of the current director.
If you want to change to a subdirectory of your home directory, you need something like:
char path[PATH_MAX];
sprintf(path, "%s/%s", getenv("HOME"), "subdir");
chdir(path);
chdir() in C only affects the process, which calls it. Your shell and your program are two different processes, which run separately. So, the environmental variable $PWD, which indicates the working directory, will only be changed for your application. Check this resource, too.
Note:chdir() doesn’t change the working directory of current shell. Because when the program is executed in the shell, the shell follows fork on exec mechanism. So, it doesn’t affect the current shell.
I need to create a directory for a file. The easiest path would be to create a directory with the same name as the file, and put the file there.
Is there any case where a valid file name would be an invalid directory name?
The directory will of course be created on the same system as where the file name is valid.
So basically I'm asking if there is any system where there are different limitations on directory names and file names.
As the number of characters in a path is limited on some filesystems, any file that uses more than half the limit of path characters would cause problems if put into a directory of the same name.
I'm looking to list and store the contents of a directory in a struct using C on Windows.
I got a problem with stat(), I don't really understand this line
if (statut.st_mode & S_IFDIR)
So I want to understand how it works for checking if it's a directory or a file?
stat() retrieves a block of information describing the specified file. Directories are also files. A directory can be thought of as a file that contains other files.
So, in the file's st_mode, you can see whether the current file is actually a directory by checking for the presence of the S_IFDIR bit.
If in fat16 system i am much confused about distinguishing a sub-directory and a file. As sub directory and a file have similar structure, how do we distinguish a directory and a file and how do we traverse through directories to reach the file?
I initially thought all the directory entries will start with '0x2e' as 1st byte of 8.3 naming convention in fat16. But practically i couldn't find all the directories starting with 0x2e but directly the name of the directory.
So, how can i distinguish directory and a file in fat16?
Directories have the 0x10 bit set in the attribute field; files do not. The first byte is "." (0x2E) for self and parent directories, if present (which they aren't in the root).
The content of the directory "file" is an array of directory entries.
Ignoring long file names, the way you search for a file is:
SearchDirectory = root
Parse off 8.3 name from remaining path
DirectoryEntry = Look up 8.3 name in SearchDirectory
If remaining path is empty, then DirectoryEntry is the requested entity
If Directory.Attribute & 0x10 == 0 then ERROR (expecting a directory)
SearchDirectory = DirectoryEntry.contents
Goto 2