having file names and delete them - c++

I want to have access to all files of a folder and have a list of them and work with them.
For example: there is a folder named "new folder" and consist of files : 1.txt and 2.txt
I don't know what are in the folder new folder. So I want a list of files in it .
Hence the questions are :
1- How can I have such this list?
2- How can I delete a file (e.g 2.txt) whether i know there is file with this name or not.
3- Is it possible to figure out has a txt file been used or not (whether it is empty or not)
thanks;

I'd use Boost filesystem to analyze folder content, and remove to delete the file. You will find in filesystem tutorial some sample that will ease your work.
edit: remove(path) it's available in boost filesystem.

Related

How to check whether a folder exists or not in gcp cloud storage out of 1 million folders

For example I have structure like this.
bucketname/checked/folder1/some files
bucketname/checked/folder2/some files
bucketname/checked/folder3/some files
bucketname/checked/folder4/some files
bucketname/checked/folder5/some files
bucketname/checked/folder6/some files
bucketname/checked/folder7/some files
bucketname/checked/folder8/some files
bucketname/checked/folder9/some files
bucketname/checked/folder10/some files
bucketname/checked/folder11/some files
......
......
bucketname/checked/folder-1million/some files
Now,
1. If I have to check whether folder99999 exists or not. So,what would be the best way to check it (we have information of folder name - folder99999) ?
2. If we simply check path that exists or not, and if not then it means, folder don't exists. would it work fine If we have millions of folders?
3. Which data structure gcp uses to retrieve the folder data ?
The true answer is this one provided by John: folder doesn't exist. All the files are stored at the root directory (bucket level) and the file name is the full path. By human convention, the / is the folder separator and the console display fake folders.
If you haven't files in a "folder", the "folder" doesn't exist, it's not interpreted/deduced from the name fully qualified path. The folder is not a Cloud Storage resource
It's also for that reason that you search only by path prefix
However, it depends what you want to check. If you exactly know which folder you want to check and validate, and if there is at least one file in it, you can directly list the files with the folder path as prefix.

Adding a CSV file to a project in Visual Studio

I am working on a project where I have to read in serveral pre-existing CSV (dog.csv, horse.csv, etc.). I want to know how would I add these file into my project so that I may test to see if my print functions work (the code is written in c++). Would I have to copy and paste the files into the debugging folder or would I place it under the test folder of the project?
You can include the files in your project in whatever (sub)folder you wish by using Right click -> Add -> Existing Item. Then, right-click on each file and choose Properties. Set up "Copy to output directory" to "Copy if newer".
Then after build, your files will be copied into the bin/debug folder.
To read the file, you can just use:
System.IO.File.ReadAllText("dog.csv");
Another possible way is to add a file within project, right click and select properties, and then in Copy to Output Directory, select Copy always. This way, csv file will be automatically copied in your debug and release packages too.
string executableLocation = Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location);
string csvLocation= Path.Combine(executableLocation, "file.csv");
Above code will read file location from bin directory where your csv file will be stored.
This link should help guide you how to add CSV files to a project.
If you wanted to do a down and dirty way you could just save the CSV's somewhere on your local machine, and then hard code the file path to that location.
Example:
c:\test\Dog.csv and then set that as a variable for whenever you need to read in the csv file.

How to get files with a particular extension within a folder in python?

I'm working on a python code. When I change few parameters of run code every time it generates a new folder (containing files with npy extension) within main folder. I want to get access to all npy files in new folder. If I use
`os.path.listdir()`
it only lists the files in main folder.How I can approach files with npy extension?
you need to specify the path of the directory in your function
when you call:
`os.path.listdir()`
it is going to the current directory by default. If for example your new folder was called foobar then you would need to write it as something like:
path = "/foobar/"
os.listdir( path )
or equivelantly this should work also
os.listdir( "/foobar/" )
Let me know if you need any clarification
EDIT
If you want only files with the npy extension you can use the following code:
for file in os.listdir("/foobar"):
if file.endswith(".npy"):
print(os.path.join("/npy", file))
Again, let me know if you need clarification here.

Reading a folder

I'm using the following code from this website about how to read from a folder to get all the files. I'm then "pushing" them into a string vector.
My question is, when I read all the files, I get "." and ".." as my first to "files" read from the folder; what are these two "files"?
Furthermore, this code does achieve the goal. I'm just curious as to what these two "files" really are.
I'm using Xcode, and this is in C++.
Let me know if you need more info.
In linux and unix there is no difference between a file and a directory. Note that in the case of these dot files they are actually not "real files" though, that's another topic.
The "." is a special file in the folder which means "the current directory" and ".." is another special file which means "the parent directory".
Directories do not contain files. Directories contain names. The names are links to objects in the filesystem. The names . and .. are links to the directory and its parent, respectively.

How can I rename files of which I don't know the extension?

I have a directory with these files:
one.txt
two.mp3
three.bmp
Is there any way to rename files using MoveFile() but without specifying the extension of the file (all the files have different filenames)?
If not, how can I rename these files to anything I want, when I only know the
one
two
three
?
You'll have to use the underlying OS API to scan through the directory for files and compare each filename with your desired prefix. Here is another question that shows you how to list the contents of a directory in Windows.
Once you know prefix read the filelist of the directory and find a valid filename with that prefix, and use that filename with the function.