This question already has answers here:
How to read file from an imported library
(2 answers)
Closed 6 years ago.
Suppose the following files:
lib/src/program.dart
lib/src/file.txt
I want to read file.txt contents from program.dart using a library that contains readingFunction(path):
//program.dart
String fileContents = library.readingFunction("./file.txt");
This throws an error, because the current path is the one in which execution was launched (or the package path if executing with pub run).
How could I achieve the reading with relative path instead of being forced to use "./lib/src/file.txt" path?
Use the Resource package.
It allows you to read files that are embedded in packages. Files are specified with a special URL that looks like package:package_name/path/to/file.txt (same as used in import statements).
Related
This question already has an answer here:
How to set the current working directory in Dart?
(1 answer)
Closed 5 years ago.
I want to know what the current directory is. I don't want to shell out to run pwd. Is there an easy way to do this in Dart?
and also,
Is there a way, put name of directory into a string?
Re the current directory, from the documentation from the Directory class in dart:io (emphasis added).
In addition to being used as an instance to access the file system,
Directory has a number of static properties, such as systemTemp, which
gets the system's temporary directory, and the getter and setter
current, which you can use to access or change the current directory.
To get the name of a Directory as a string just use toString(), or perhaps the path property.
So the question is actually simple, but I have no idea how to approach this issue. I know this code is generated by template based on this question:
XCode automatically generated comments?
I want to use the <name> that xcode provides on each mac machine which is unique for it's user, for some types of logs.
EDIT:
This is how the swift template file looks before it's used by Xcode to create my work file:
//
// ___FILENAME___
// ___PROJECTNAME___
//
// Created by ___FULLUSERNAME___ on ___DATE___.
//___COPYRIGHT___
//
Surely, there is no point in parsing it.
The question is: Does anyone knows how I can get this name using swift in my application?
I searched for an answer here/Google but so far no luck.
I don't know how to read the header. But you can do it otherwise.
First if you need the creation-date of a file, you can use the NSFileManager:
var path = "path/to/your/file/"
var fileAttribs:NSDictionary = NSFileManager.defaultManager().attributesOfFileSystemForPath(path, error: nil)!
var creationDate = fileAttribs.objectForKey(NSFileCreationDate)
Also if you need the full username, you can use the function NSFullUserName() or NSUserName(). It should return the same string as __FULLUSERNAME__
var fullUsername = NSFullUserName()
var username = NSUserName()
Sometimes in the iOS Simulator, this username is empty, but in a real app, it should work properly.
That text written at template instantiation time — that is, when you create a new Xcode project (or a new file in an existing project using the File > New > File... templates). You can't read the contents of the source file your code was compiled from. (Well, unless you ship that file along with your compiled binary, and read it in like any other text file.)
But that's just text substitution — it can be done anywhere in the file, not just in the comment headers. So you could create your own file or project templates, and in the template files, put those substitution macros in code instead of in comments:
let schmoeWhoCreatedThisFile = "___FULLUSERNAME___"
Here's a tutorial found in a couple seconds of web searching that has the full details on creating templates and the substitution macros you can use in them.
Remember, substitution happens when you create a new file or project — if you're looking for who made the latest change to your source file or who built the app that shipped to your customers, you're barking up the wrong tree. Some of those sorts of things you can do with source control; others are more a matter of (human-defined, human-executed) policy for you or or your organization.
This question already has an answer here:
Include all files in a folder in a single bundle
(1 answer)
Closed 8 years ago.
In the code for my bundle configuration, assume the following is declared and initialised:
BundleCollection bundles;
I have js files that I want to include in the following directories:
~/Scripts/app
~/Scripts/app/some-namespace
I've been able to include the files in the app directory but not its child directories by using this:
bundles.Add(new ScriptBundle("~/bundles/app").Include("~/Scripts/app/*.js"));
I would like to include all of the js files from both directories in a single bundle in the most maintainable way possible.
I was able to achieve this by doing this:
var appScripts = new ScriptBundle("~/bundles/app")
.IncludeDirectory("~/Scripts/app", "*.js", searchSubdirectories: true);
bundles.Add(appScripts);
I'm planning to do a program with Lua that will first of all read specific files
and get information from those files. So my first question is whats the "my documents" path name? I have searched a lot of places, but I'm unable to find anything. My second question is how can I use the first four letters of a file name to see which one is the newest made?
Finding the files in "my documents" then find the newest created file and read it.
The reading part shouldn't be a problem, but navigating to "my documents" and finding the newest created file in a folder.
For your first question, depends how robust you want your script to be. You could use Lua's builtin os.getenv() to get a variety of environment vars related to user, such as USERNAME, USERPROFILE, HOMEDRIVE, HOMEPATH. Example:
username = os.getenv('USERNAME')
dir = 'C:\\users\\' .. username .. '\\Documents'
For the second question, there is no builtin mechanism in Windows to have the file creation or modification timestamp as part of the filename. You could read the creation or modification timestamp, via a C extension you create or using an existing Lua library like lfs. Or you could read the contents of a folder and parse the filenames if they were named according to the pattern you mention. Again there is nothing built into Lua to do this, you would either use os.execute() or lfs or, again, your own C extension module, or combinations of these.
This question already has answers here:
How to create directories recursively in ruby?
(6 answers)
Closed 8 years ago.
i have built this little app that generates a excel document. i am trying to make a directory to stick it in. these documents are built differently depending on the #agency that people select. so i made this method to return the path since the path is used in a few places.
def reportsheet_dir
file_path = "#{Rails.root}/public/reportsheets/#{#agency.downcase.gsub("_","")}"
end
At the beginning of the method that creates the document i have this method that supposedly builds directories but it doest seem to be working
Dir.mkdir(reportsheet_dir) unless File.exists?(reportsheet_dir)
I keep getting. this
and i get
Errno::ENOENT at /addons/agency_report_builders
No such file or directory -/Users/fortknokx/Work/toolkit/public/reportsheets/empowerlogicbuilder
I think its because its multiple levels deep?? since public/reportsheets/agency_name/file_name has to be made. i could just go and make the folders but i would like to just make the dir each time because new agencies could be made at any time. is this possible?
Have a look at FileUtils.mkdir_p()
http://ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html#method-c-mkdir_p
It will recursively create non-existent directories. Dir.mkdir will not.