Opening Multiple files from a directory C++ - c++

I am trying to open files from a directory but everytime I display my files i have these 3 dots at the top. So for example the directory i open is called "My Documents" the output would be the 3 dots as follows
.
..
Names.txt
Jobs.txt
Names.txt and Jobs.txt is the only output i want to achieve, could anyone help out.
My code
int getDoc(string doc, vector<string> &documents){
DIR *dp;
struct dirent *dirp;
if ((dp = opendir(doc.c_str())) == NULL){
cout << "Error(" << errno << ") opening" << doc << endl;
return errno;
}
while ((dirp = readdir(dp)) != NULL){
documents.push_back(string(dirp->d_name));
}
closedir(dp);
return 0;
}
by the way i use dirent.h

. is current directory, .. is upper level directory. if you don't want them, just filter them out.

The 3 dots are actually 2 directories. The first one named '.' refers to the current directory. If you will try to open it, it will lead you back to the same directory. For example, directory C:\Users\Daniel is equal to C:\Users\Daniel\.
The second directory is '..'. It refers to the parent directory. So C:\Users directory is equivalent to C:\Users\Daniel\..
Those 2 directories are not real. They are simulated by the operating system. If you don't want to print them, just start printing the list after skipping the first 2 elements. Those 2 directories are always listed first.

The directory . is the current directory. The directory .. is the parent directory. So they will be on the list
Just like running the command ls -a

Related

How to change files names in a folder that in a directory?

I'm setting up my C++ project, the project is about rename files in any location.
I'm using filesystem library.
The project run successfully, and I didn't get any errors, but when I put a full path ( example, Documents ), it's not changing the files that in a folder. For example, I got a folder inside my downloads directory, I have a folder that is called "myfolder". Inside this folder I have 2 txt files, my program changes the name of the all files that in the downloads but not inside the "myfolder" folder.
string dirPath = "C:\\Users\\" + pcuser + "\\Downloads";
auto path = fs::path(dirPath);
auto dir = fs::directory_iterator(path);
for (auto& file : dir)
{
int Filename = rand() % 2342;
rename(file.path(), fs::path(dirPath + "\\" + to_string(Filename)).c_str());
Filename++;
}
I want to change the files that are in a folder too.
How can I do this?
There is a std::filesystem::recursive_directory_iterator so you can just use it and renamed entity when it's a file
for(auto& p: fs::recursive_directory_iterator(dirname))
{
if (fs::is_regular_file(p))
{
//do rename
}
}

Python: How can I open a folder with variable

question again... I have a list A=['AA','BB','CC'] and a folder path like ./ABC/. There are three sub folder inside called 032_AA, 0244_BB, 01_CC
format is like random number_AA(BB or CC).
now I tend to use this list to enter these folder and open a txt file which in there sub folder:
cmd1='cd ABC'
os.sys(cmd1)
for i in A:
???????? ---------------------- enter folder 032_AA according to list A
with open('xxx.txt','a') as f:
XXXXXXXX
my main question is I dont know how to enter a folder with a known file name with random number as begining.
So any idea? Thanks!
you can use glob:
import glob
regex = './ABC/*'
for i in A:
subdir = regex + i
for name in glob.glob(subdir):
U can check glob for more info on pattern matching

c++ How to check if there is a file in a directory quick way?

What I am trying to do is that when a directory has no write permission and if directory contains at least one file trying to print out "permission denied"
DIR *dir;
dir = opendir (argv[i]);
if (!(sb.st_mode & S_IWUSR) && (readdir(dir) != NULL))
{
printf("rm: cannot remove ");
printf(argv[i]);
printf(": Permission denied\n");
}
This is what I was trying to do, but it prints out the messages even when there is no files... Any suggestions?

Directory Open Issue when passing path as parameter

I having an issue with my program which is expected to either ask a user to input a directory path or to use an predefine directory path as an argument so that my program can open that directory.
Asking a user to input the directory path (C:\Users\Desktop\Test) works fine. But I am having an issue when I am passing the file path as argument.
It should be passed as this:
char location[1000] = "C:\Users\Desktop\Test";
if ((dir = opendir (location)) != NULL){
......
}
But using argument, my program can only open the directory if location is initialised and assigned as this:
char location[1000] = "C:\\Users\\Desktop\\Test";
if ((dir = opendir (location)) != NULL){
......
}
However, I need to concatenate location with a filename in another part of my program so that it becomes: C:\Users\Desktop\Test\file.txt
It won`t work with: C:\\Users\\Desktop\\Test\\file.txt.
char location[1000] can`t be modified as it works well with my code when opening or closing directory.

Python - Counting the number of files and folders in a directory

I've got a python script that deletes an entire directory and its subfolders, and I'd like to print out the number of files and folders removed. Currently, I have found some code from a different question posed 2010, but the answer I receive back is 16... If I right-click on the the folder it states that theres 152 files, 72 folders...
The code I currently have for checking the directory;
import os, getpass
user = getpass.getuser()
copyof = 'Copy of ' + user
directory = "C:/Documents and Settings/" + user
print len([item for item in os.listdir(directory)])
How can I extend this to show the same number of files and folders that there actually are?
To perform recursive search you may use os.walk.
os.walk(top, topdown=True, onerror=None, followlinks=False)
Generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at
directory top (including top itself), it yields a 3-tuple (dirpath,
dirnames, filenames).
Sample usage:
import os
dir_count = 0
file_count = 0
for _, dirs, files in os.walk(dir_to_list_recursively):
dir_count += len(dirs)
file_count += len(files)
I was able to solve this issue by using the following code by octoback (copied directly);
import os
cpt = sum([len(files) for r, d, files in os.walk("G:\CS\PYTHONPROJECTS")])