How to execute a file with a different extension's associated application? - delphi

In my application, I would like to be able to execute a file with an arbitrary name and extension using the program associated with a different extension.
For example, say I have a file called file.dat, but I wanted to open it with Notepad, as if it was named file.txt or file.dat.txt. Also, I don't have permission to rename the file, so that's out of the question.
If the file were called file.txt, I could call ShellExecute. But that fails when calling file.dat; Windows complains that there’s no association for that file.
I don't want to hardcode a specific executable, because (in the example above) the end user could have associated .txt files with Wordpad instead of Notepad. In such a case, I’d want my file.dat to be opened with Wordpad.

Call ShellExecuteEx and specify the lpClass member of the SHELLEXECUTEINFO struct. Note that you must include SEE_MASK_CLASSNAME in the fMask member.
For instance, set lpClass to '.txt' to request that the file be opened with the program associated with the .txt extension.

Related

Do not replace existing file when uploading file with same name Microsoft Graph API

How can I prevent replacing the existing file with a new file which has the same name, when I upload file to one drive?
I am using PUT /me/drive/items/{parent-id}:/{filename}:/content docs end point.
I instead need to keep indexing (test.jpg, test (1).jpg) or, just like google drive does, add two files with the same name.
You can control this behavior using Instance Attributes, specifically the #microsoft.graph.conflictBehavior query parameter. There are three supported conflict behaviors; fail, replace (the default), and rename.
The conflict resolution behavior for actions that create a new item. You can use the values fail, replace, or rename. The default for PUT is replace. An item will never be returned with this annotation. Write-only.
In order to have it automatically rename the file, you add #microsoft.graph.conflictBehavior=rename as a query parameter to your URI.
PUT /me/drive/items/{parent-id}:/{filename}:/content?#microsoft.graph.conflictBehavior=rename

Erlang : exception error: no match of right hand side value {error,enoent} while reading a text file

I am currenly working on an erlang project and stuck in reading the file. I want to read a text file which is in the /src folder where all the erlang and a text file are in the same structure. Then too, I am not being able to read the file despite of specifying file paths. Any help would be appreciated.
start() ->
{ok,DataList} = file:consult("Calls.txt"),
io:format("** Calls to be made **"),
io:fwrite("~w~n",[DataList]).
The data file stores contents like : {john, [jill,joe,bob]}.
Try add folder name to the path or try set full patch to the file:
1> {ok,DataList} = file:consult("src/Calls.txt").
Notes: the error {error,enoent} mean that the file does not exist or you don't have a rights to read/write current file, for this case need set 777 rights or similar.
If you need to use src/call.txt, then this simply means that your IDE (or you) has created a src folder in which the calls.txt file has been placed. At the same time, the IDE is using a path that only includes the top level folder (i.e., the root folder for the IDE project). So src/call.txt must be used in that case. This isn’t a problem with Erlang, or even the IDE. It’s just the way your project is set up.
You can do either of two things. Move the calls.txt file up one level in the IDE file manager, so that it can be referenced as calls.txt, not src/call.txt. You can also just change the path to “calls.txt” before you run it from the command line.
enoent means "Error: No Entry/Entity". It means the file couldn't be found. When I try your code, it works correctly and outputs
[{john,[jill,joe,bob]}]

How to set download location for ftp.get with Indy Delphi component

I can successfully download a file from my ftp server using:
ftp.get(chosenFile,chosenFile);
Where chosenFile is simply a string with the name of the file.
However these are downloaded to the Debug folder of my Delphi project so...
1.) How can I specify where the files should be downloaded to.
2.) How can I make TOpenDialog automatically open to that location after downloading?
You can specify a full path in the destination file, to specify the exact location. You can specify that same path as the initial dir of the open dialog.
You could also set the working directory using the SetCurrentDir procedure.
Alternatively, you can use ftp.Get(chosenFile, Stream), where Stream can be an instance of any TStream descendant, like a TFileStream (opened to write to your desired target file), or even a TMemoryStream, if you don't need the file to be on disk at all.
In fact, the Get overload that accepts the destination filename, will just create a TIdFileStream, depending on the exact parameters, and call the other overload.
[SOLVED] I solved my own problem, can't believe it was so simple:
1.Specify the director where the file should be downloaded to as the second parameter: ftp.get(chosenFile,'C:\Temp\'+chosenFile);
2.Set the initial directory of the TOpenDialog as follows: dlg.InitialDir := 'C:\Temp';

iup.getparam in Lua -- prompting for a directory

I'm writing a Lua program that must prompt the user for a directory as one of a number of parameters for an operation (that involves copying a file to a target directory with a new name). Environment is Windows; I'm using Lua 5.1.
The relevant code currently looks like
require("iuplua")
local mediaFolder = "C:\some folder\some subfolder\"
local pPrompt = --this is a subset of the parameters
"File name: %s\n"..
"Destination: %f[DIR||"..mediaFolder.."]\n"
ret, strTargetFile, strTargetPath =
iup.GetParam("Add Media from file ", param_action, pPrompt, "Initial file name", mediaFolder)
The resultant GUI looks like:
but when the selector button (...) is pressed, the initial directory shown is not C:\some folder\some subfolder\ but whatever directory was last navigated to in the interface, and it isn't possible to select a directory, only a file.
I'm guessing I have a fundamental misunderstanding of how this should work? Is what I want to do possible with iup? Ideally, I'd also like to restrict the user to only selecting the initial directory or one of its sub-directories rather than navigating anywhere outside that directory structure, and to allow the user to create a new sub-folder.
This looks like a bug. I'll check it.
Don't know if Stack Overflow is a place for bug reports, but I monitor iup posts here.
Best

Delphi overwrite file and wrong modified date time

I'd like to get a file last modified time in Delphi.
Normally something like FileAge() would do the trick, only the problem is: if I overwrite *File A* with File B using CopyFile, File A's modified date is not updated with current overwrite time as it should(?)
I get that: CopyFile also copy file attributes, but I really need to get the modified date that also works when a file is overwritten.
Is there such function? My whole application relies on modification time to decide whether or not I should proceed with files!
EDIT Just to clarify: I'm only monitoring the files. It's not my application who's modifying them.
The documentation for CopyFile says:
File attributes for the existing file are copied to the new file.
Which means that you cannot use base your program on the last modified attribute of the file, or indeed any attribute of the file. Indeed there are all sorts of ways for the last modified attribute of the file to change. It can in fact go backwards in time.
Instead I suggest that you use ReadDirectoryChangesW to keep track of modifications. That will allow you to receive notifications whenever a file is modified. You can write your program in an event based manner based on the ReadDirectoryChangesW API.
If you can't use ReadDirectoryChangesW and the file attributes, then you'll have to base your decisions on the contents of the file.

Resources