Click to See Complete Forum and Search --> : setting file properties?
shiroshi
March 22nd, 2006, 10:48 AM
hi,
i need to set file properties (e.g. when you do a right-click on a file, summary tab) within an application. i did a search and found how to get the info but i need to set these fields. i have an application that converts data from an file (external source) to a binary file. in the (external) file there is info i want to set in the (binary) file properties fields.
thanks,
s...
GaboonViper
March 23rd, 2006, 01:43 AM
use the SetFileAttributes function:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/setfileattributes.asp
shiroshi
March 23rd, 2006, 09:44 AM
GaboonViper,
thanks for the reply.
i'm not sure this is what i need. when you do a right click on a file and go to the "Summary" tab. i need be able to set the fields like, Title, Subject, Category, Keywords, etc. i found some code to read those fields but the problem is that file has to be a "compound file" whatever than means.
thanks.
s...
kkez
March 23rd, 2006, 01:01 PM
Did you find this KB article (http://support.microsoft.com/kb/q186898/)?
With that code you can read the properties, but at the same time you can write to it using the STGM_WRITE flag both with StgOpenStorageEx (if i read correctly in MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/stg/stg/stgopenstorage.asp), you should use this function instead of StgOpenStorage - even if the Ex version is not available under windows 98/me (in fact, IIRC these properties didn't exist in earlier version of Windows) - because MSDN states that function can be used with any file, while StgOpenStorage cannot) and IPropertySetStorage::Open.
Then you can use IPropertyStorage::WriteMultiple to store those properties through the PROPVARIANT struct.
I'm pretty familiar with interfaces and the VARIANT struct, so if you need an example..
shiroshi
March 23rd, 2006, 04:12 PM
...
I'm pretty familiar with interfaces and the VARIANT struct, so if you need an example..
thank you...
please, i need an example!!!
kkez
March 24th, 2006, 09:51 AM
This code can be used only on windows XP and superior versions (specifically, it works only on a NTFS filesystem). This article (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/stg/stg/ipropertysetstorage_ntfs_file_system_implementation.asp) explains why.
CHAR fileName = ....;
WCHAR wcFilename[1024];
IPropertySetStorage *pPropSetStg = NULL;
IPropertyStorage *pPropStg = NULL;
//Convert to unicode
mbstowcs(wcFilename, fileName, strlen(fileName ));
//Get the IPropertySetStorage interface
StgOpenStorageEx(wcFilename, STGM_READWRITE|STGM_SHARE_EXCLUSIVE, STGFMT_ANY, 0, NULL, NULL, IID_IPropertySetStorage, (void**)&pPropSetStg );
//Open summary information, getting an IpropertyStorage.
pPropSetStg->Open(FMTID_SummaryInformation, STGM_SIMPLE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &pPropStg);
//Tell Windows to set Title,Subject,Author,Keywords and Comments properties
PROPSPEC ps[5];
ps[0].ulKind = PRSPEC_PROPID;
ps[0].propid = PIDSI_TITLE;
ps[1].ulKind = PRSPEC_PROPID;
ps[1].propid = PIDSI_SUBJECT;
ps[2].ulKind = PRSPEC_PROPID;
ps[2].propid = PIDSI_AUTHOR;
ps[3].ulKind = PRSPEC_PROPID;
ps[3].propid = PIDSI_KEYWORDS;
ps[4].ulKind = PRSPEC_PROPID;
ps[4].propid = PIDSI_COMMENTS;
//Properties values
PROPVARIANT pv[5];
pv[0].vt = VT_LPSTR;
pv[0].pszVal = "The title";
pv[1].vt = VT_LPSTR;
pv[1].pszVal = "The subject";
pv[2].vt = VT_LPSTR;
pv[2].pszVal = "The author of this file";
pv[3].vt = VT_LPSTR;
pv[3].pszVal = "keywords,keywords,keywords";
pv[4].vt = VT_LPSTR;
pv[4].pszVal = "Comment";
//Write to file
pPropStg->WriteMultiple(5, ps, pv, 0)
//Release interfaces in the reverse order in which they where obtained
pPropStg->Release();
pPropSetStg->Release();
This should work, i didn't try it..
shiroshi
March 27th, 2006, 12:30 PM
thanks for the code.
i'm having a problem, the StgOpenStorageEx is complaining about an invalid file name. that's the status i get back from that function.
s...
kkez
March 28th, 2006, 06:32 AM
thanks for the code.
i'm having a problem, the StgOpenStorageEx is complaining about an invalid file name. that's the status i get back from that function.
s...
Post the ANSI string. It's null terminated? Try to output it (with MessageBoxA/MessageBoxW, for example) before and after it's converted to unicode, to see if something went wrong.
shiroshi
March 30th, 2006, 11:36 AM
CHAR fileName[] = "82";
WCHAR wcFilename[1024];
...
...
size_t status = mbstowcs(wcFilename, fileName, strlen(fileName));
...
...
hr = StgOpenStorageEx(wcFilename,...
if (FAILED(hr))
....
here is a snippet of my code that i use your example.
the status i get back from StgOpenStorage is -2147286788 which i think translates to "Invalid Filename".
thanks,
s...
shiroshi
March 30th, 2006, 11:55 AM
ok, for a test i changed the
WCHAR wc_Filename[1024] to WCHAR wc_Filename[] = L"82";
that seem to get me past the StgOpenStorageEx() call.
now i get a "File not found" message from the
pPropSetStg->Open() call.
i'm gaining ground. just little problems trying to solve.
thanks,
s...
kkez
March 30th, 2006, 02:07 PM
That's strange, mbstowcs works fine for me. Try to initialize the unicode string:
WCHAR wc_Filename[1024] = L"";
or use MultiByteToWideChar instead.
shiroshi
March 30th, 2006, 04:33 PM
i'll give that a shot. thanks...
can you tell me in your code why the open() call is not working.
i found this program WriteRead.cpp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/stg/stg/writeread_sample.asp) on MSDN site.
i got it to work but the one thing i don't like about it is it CREATES the file first. i need to fiddle with it and see if i can get to work with an existing file.
kkez
March 31st, 2006, 02:28 AM
That's because there's a STGM_CREATE flag in StgCreateStorageEx and the IPropertySetStorage::Create() method is called instead of IPropertySetStorage::Open(). My code writes to a file that exists: StgCreateStorageEx() and Open() fail if the file doesn't exist.
Take a look at this faq (http://www.codeguru.com/forum/showthread.php?t=330024) if you need to test if the file exists.
shiroshi
March 31st, 2006, 10:08 AM
That's because there's a STGM_CREATE flag in StgCreateStorageEx and the IPropertySetStorage::Create() method is called instead of IPropertySetStorage::Open(). My code writes to a file that exists: StgCreateStorageEx() and Open() fail if the file doesn't exist.
Take a look at this faq (http://www.codeguru.com/forum/showthread.php?t=330024) if you need to test if the file exists.
thanks again...
the file exists i get no error back from the GetAttributes routine.
i'm getting the error ["File not found"] on the Open() call.
your code is straight forward, i don't see what i'm doing wrong.
maybe it's the file i'm trying to open, i'll try a different type of file.
thanks,
s...
shiroshi
March 31st, 2006, 11:13 AM
i've discovered it's my file that i'm trying to open that is the problem.
i guess the file has to be a special type not some ramdom type (e.g. .doc,
.txt. .xls, .html, etc). i thought i'd be able to change the summary properties
on any file i wanted i guess i was mistaken.
thanks,
thomas...
DocBrown
July 18th, 2010, 09:37 AM
hi guys,
sorry for digging out a thread that old.
i'm currently trying to do pretty much the same as shiroshi and have exactly the same problem.
when pPropSetStg->Open() is called, it returns STG_E_FILENOTFOUND (see here: http://msdn.microsoft.com/en-us/library/aa379965%28v=VS.85%29.aspx).
which means according to MSDN that "A property set of the indicated name does not exist.". i tried it with several file types (.txt, .pdf, .doc), but none of them worked - i'm a little bit confused now.
could you please help me? :-)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.