Deleting content of the folder using VC++ - file

I am using the following code to create a directory using the following code.
TCHAR dir_path[] = TEXT("C:\Users\Temp\abc");
if (!CreateDirectory(dir_path,NULL)) {
}
else
{
//directory already exists
}
I want to add the logic to delete all the content of the folder(Files and folders recursively if any are there) if the folder C:\Users\Temp\abc already exists.
Any idea how I can delete the content of the folder recursively?
Thanks in Advance,
Azeem
I am new to this site. Apologies in advance if I am mistaken anywhere.

Firstly find the entries in the directory:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364418%28v=vs.85%29.aspx
See if they are directory or not:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365740%28v=vs.85%29.aspx
and
http://msdn.microsoft.com/en-us/library/windows/desktop/gg258117%28v=vs.85%29.aspx
If yes, change to it and call the function again http://msdn.microsoft.com/en-us/library/windows/desktop/aa365530%28v=vs.85%29.aspx.
Then delete it: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363915%28v=vs.85%29.aspx

Related

How to read a text file from resources without javaClass

I need to read a text file with readLines() and I've already found this question, but the code in the answers always uses some variation of javaClass; it seems to work only inside a class, while I'm using just a simple Kotlin file with no declared classes. Writing it like this is correct syntax-wise but it looks really ugly and it always returns null, so it must be wrong:
val lines = object {}.javaClass.getResource("file.txt")?.toURI()?.toPath()?.readLines()
Of course I could just specify the raw path like this, but I wonder if there's a better way:
val lines = File("src/main/resources/file.txt").readLines()
Thanks to this answer for providing the correct way to read the file. Currently, reading files from resources without using javaClass or similar constructs doesn't seem to be possible.
// use this if you're inside a class
val lines = this::class.java.getResourceAsStream("file.txt")?.bufferedReader()?.readLines()
// use this otherwise
val lines = object {}.javaClass.getResourceAsStream("file.txt")?.bufferedReader()?.readLines()
According to other similar questions I've found, the second way might also work within a lambda but I haven't tested it. Notice the need for the ?. operator and the lines?.let {} syntax needed from this point onward, because getResourceAsStream() returns null if no resource is found with the given name.
Kotlin doesn't have its own means of getting a resource, so you have to use Java's method Class.getResource. You should not assume that the resource is a file (i.e. don't use toPath) as it could well be an entry in a jar, and not a file on the file system. To read a resource, it is easier to get the resource as an InputStream and then read lines from it:
val lines = this::class.java.getResourceAsStream("file.txt").bufferedReader().readLines()
I'm not sure if my response attempts to answer your exact question, but perhaps you could do something like this:
I'm guessing in the final use case, the file names would be dynamic - Not statically declared. In which case, if you have access to or know the path to the folder, you could do something like this:
// Create an extension function on the String class to retrieve a list of
// files available within a folder. Though I have not added a check here
// to validate this, a condition can be added to assert if the extension
// called is executed on a folder or not
fun String.getFilesInFolder(): Array<out File>? = with(File(this)) { return listFiles() }
// Call the extension function on the String folder path wherever required
fun retrieveFiles(): Array<out File>? = [PATH TO FOLDER].getFilesInFolder()
Once you have a reference to the List<out File> object, you could do something like this:
// Create an extension function to read
fun File.retrieveContent() = readLines()
// You can can further expand this use case to conditionally return
// readLines() or entire file data using a buffered reader or convert file
// content to a Data class through GSON/whatever.
// You can use Generic Constraints
// Refer this article for possibilities
// https://kotlinlang.org/docs/generics.html#generic-constraints
// Then simply call this extension function after retrieving files in the folder.
listOfFiles?.forEach { singleFile -> println(singleFile.retrieveContent()) }
In order to have the same url that work for both Jar or in local, the url (or path) needs to be a relative path from the repository root.
..meaning, the location of your file or folder from your src folder.
could be "/main/resources/your-folder/" or "/client/notes/somefile.md"
The url must be a relative path from the repository root.
it must be "src/main/resources/your-folder/" or "src/client/notes/somefile.md"
Now you get the drill, and luckily for Intellij Idea users, you can get the correct path with a right-click on the folder or file -> copy Path/Reference.. -> Path From Repository Root (this is it)
Last, paste it and do your thing.

Create folder in explicit directory AIR

Like the title says I want to create a folder in a specific directory with Adobe Air.
If I use static methods of File like File.userDirectory works fine but I need to give the choice to select the directory.
I am trying this:
file.addEventListener(Event.SELECT, dirSelected);
file.browseForDirectory("Select a directory");
function dirSelected(e:Event):void {
trace(file.nativePath);
file.resolvePath("new_folder");
file.createDirectory();
}
Nothing happens
"resolvePath: Creates a new File object with a path relative to this File object's path, based on the path parameter (a string)."
So:
var newDir:File = file.resolvePath("new_folder");
newDir.createDirectory();

Deleting images from a folder programmatically

i want to delete images from public:// sub folder pro grammatically. i have paths like this below.
$path = 'http://localhost/Drupal/dbquery/sites/default/files/take_snap/20140713110549.jpg';
i want to delete like this,
$path = 'http://localhost/Drupal/dbquery/sites/default/files/take_snap/20140713110549.jpg';
$del = unlink($path);
but it doesn't delete image. i try to do this from menu call back function. how do i do that?.
In Drupal 7 you can use the file_delete to delete the file and its DB records as well.
these codes work. i just changed the path to relative path like below.
$path = 'sites/default/files/take_snap/20140713110549.jpg';
$del = unlink($path);
Now it deletes file from public sub folder.

How to get the latest file from a directory using groovy?

I have a directory that contains a list of files. I wanted to get the latest file out of all the contents of the said directory. How will I do that?
I am using this code, but I am not getting the latest file out of it. Please help.
def fileDir = new File("A/B").listFiles().first()
Thanks.
As simple as:
new File( 'A/B' ).listFiles()?.sort { -it.lastModified() }?.head()
(taking the negative lastModified, as we want the newest file first)

Gradle - Add single file to JAR

I have the file "license.txt" in the root directory of my project. In the jar-task, I want to add this file to the (root folder of the) JAR file.
I tried
jar {
from '.' include 'license.txt'
}
but this replaces the other content (.class files) instead of adding a file.
And I do not want to add the license.txt to the resources folder, because I do not want to change my project structure just because of the build tool.
Who can help? Thank you!
To add a single file, you can simply do:
jar {
from "license.txt"
}
Your solution should also work if you scoped your include to your from by enclosing it in curly braces.
If you would like to add multiple files, you can do:
jar{
from{
["aaa.txt","bbb.txt"]
}
}
You would add multiple files to output jar as under:
jar {
// Update jar name according to Ascertia conventions
/**
* archiveFileName
* The archive name. If the name has not been explicitly set, the pattern for the name is:
* [archiveBaseName]-[archiveAppendix]-[archiveVersion]-[archiveClassifier].[archiveExtension]
*/
archiveFileName = 'database_postgresql.jar'
from(['build/classes/java/main','mappings/postgresql'])
}
All contents in below directories will be added:
build/classes/java
/main/mappings/postgresql

Resources