WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

DllCall Information

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Change Target of My Documents Folder


Question:

How do I change the target of My Documents folder? Usually I right click on My Document, select properties and change the folder name.But I would like to handle this in WinBatch.

Answer:

Take a look at the function SHSetFolderPath() in the MSDN online docs for Explorer Shell Functions.

https://msdn2.microsoft.com/en-us/library/ms647910.aspx

It's exported from SHELL32.DLL in both ANSI and Unicode versions, and is readily callable from WinBatch using DllCall(). The magic number you need for "My Documents" comes from the constant CSIDL_MYDOCUMENTS [defined in Shlobj.h & Shfolder.h in the Windows Platformo SDK], and its value is 0x000c in hex, 12 in decimal. You might need to combine this CSIDL value with CSIDL_FLAG_CREATE [value is 0x8000 in hex], which causes the function to forcibly create the target folder location if it does not already exist.

; Make sure this path already exists.
NewPathSpec = "C:\SomeNewPath\My Documents"
DllSpec = StrCat(DirWindows(1),"SHELL32.DLL")
HR = DllCall(DllSpec, long:"SHSetFolderPathA", long:12, lpnull, long:0, lpstr:NewPathSpec)
The return value is a shell HRESULT value, which is supposed to be a 32-bit unsigned long word value. A success result [S_OK] has a value of zero. Pretty much all non-zero results will indicate that the function failed. If you really want to know what most of the HRESULT values mean, and what their symbolic constant names are, you need to look at the Windows Platform SDK header file "WinError.h".
Article ID:   W17402
File Created: 2008:04:10:15:09:18
Last Updated: 2008:04:10:15:09:18