C++/VB - Next "available" File/Folder Name,...
Asked By Kerem Gümrükcü
27-Jan-10 04:00 AM
Hi,
i am looking for some API that will give me the next
available file/folder name if the file/folder aleady
exists. Example: you have a file named "New Text.txt",
o the operating system suggests here "New Text(1).txt"
if you create a new file within the shell. Is there any API
that could give me the next available name for a file if
i provide a filename as input? I guess its some shell interface
or function, but i could not find anything so far,...
Thanks in advance,...
Regards
Kerem
--
-----------------------
Beste Gr?sse / Best regards / Votre bien devoue
Kerem G?mr?kc?
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
PathYetAnotherMakeUniqueName
(1)
XP
(1)
PathMakeUniqueName
(1)
GetUniqueFilePath
(1)
FindFirstFile
(1)
FindNextFile
(1)
GetNextCount
(1)
FilePathIN
(1)
Jeff Gaines replied to Kerem Gümrükcü
I use the following for files:
internal static string GetUniqueFilePath(string filePathIN)
{
int count = 1;
string result = filePathIN;
FileInfo fInfo = new FileInfo(result);
if (fInfo.Exists)
{
do
{
result = filePathIN + "." + count.ToString("0000");
count++;
fInfo = new FileInfo(result);
}
while (fInfo.Exists);
}
return result;
}
It adds the number at the end so you would need to tweak it a bit to put
the number in a different place.
--
Jeff Gaines Dorset UK
There are 3 types of people in this world. Those who can count, and those
who cannot.
Stefan Kuhr replied to Kerem Gümrükcü
Hi Kerem,
Even if there is such a thing, what value would it give to you? Once you
had that "next available file/folder name" and just before you can
create it, your program can be preempted and someone else can calculate
this file/folder and create it, so what value will your prior call to
this imaginary function have?. Having a function that does this as an
API is subject to race conditions, so it is per se useless, unless
tightly coupled with a file/foldeer creation call. The only sure thing
is a function that tries to *create* the next possible file/folder.
You mentioned PathMakeUniqueName, I never used that, does it only
does not create the file name, you will have to use that in a loop until
a combination of the result of both PathMakeUniqueName and CreateFile
succeed. BTW: MSDN online says that PathMakeUniqueName is suppported on
W2K as well.
--
S
Kerem Gümrükcü replied to Stefan Kuhr

Hi Stefan,
Yes, you are right on that. But it is quite sure that there will be no
race condition/deadlock, because the access to the folder is only restricted
to
a special process that only runs single instance, system and sessionwide
in a special account and the folder (where the subfolders/files) will be
created) has special NTFS restrictions only for the process user context.
AFAIK (i didnt check it and will use it!) it only suggest the file name,
like
many shell and helper functions do with strings and URLs/Paths.
I implemented my own version that does a for-loop on the names until it gets
a "available" name, then it does its job.
I am always very cautious on functions i never used or know, so i always
check
first whether there is a (implemented) export of the function on the named
library
and this time i was right on checking it, because my up2date W2k system does
NOT have such a export in the library! Yes, MSDN says that, but in fact it
seems to
be wrong since i do have a shell32.dll (5.00.3900.7155) on my W2k and it
does
NOT export that function at least it has no named export! But MSDN says:
Minimum DLL Version shell32.dll version 5.0 or later
Custom Implementation No
Header shlobj.h
Import library shell32.lib
Minimum operating systems Windows 2000
Thats simply wrong!
And what export name is that "PathYetAnotherMakeUniqueName", available
since >=XP *g*
Regards
K.
--
-----------------------
Beste Gr?sse / Best regards / Votre bien devoue
Kerem G?mr?kc?
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
Hector Santos replied to Kerem Gümrükcü

Stefan's suggestion still applies. However, where (count) do you
begin? If its a single process/thread, you might as well use a
FindFirstFile()/FindNextFile() to get the highest count using a "New
File*.txt" specification, then +1 for the new file. If the applet is
24x7, then you can get that high count at startup.
Off hand:
int GetNextCount()
{
const char *pszSpec = "new file(*).txt";
int nHigh = 0;
WIN32_FIND_DATA fd;
HANDLE ff = FindFirstFile(pszSpec, &fd);
if (ff != INVALID_HANDLE_VALUE) {
do {
int n = atoi(fd.cFileName+9);
if (n > nHigh) nHigh = n;
} while(FindNextFile(ff,&fd));
FindClose(ff);
}
return nHigh+1;
}
Usage:
CString nfn;
nfn.Format("New File(%d)",GetNextCount())
Fine tune this to your specific paths/names. But has Stefan
highlighted, if you going to be having multiple access points,
threads, processes, etc, then doing a exclusive file open with
CreateFile() will work (after getting the highest count), then use a
loop to increment again if the file was created after the
findfirst/next search.
--
HLS
Kerem Gümrükcü replied to Jeff Gaines
Hi Jeff,
thanks a lot, thats nice, but i already have something
like that, but i will "follow" the Microsoft Windows
naming scheme to make sure that evrything is fine.
I dont want to reinvent the wheel if there is such a
function already build into windows itself,...if not,
i have to go the hard way,...
Regards
Kerem
--
-----------------------
Beste Gr?sse / Best regards / Votre bien devoue
Kerem G?mr?kc?
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
Kerem Gümrükcü replied to Kerem Gümrükcü
No need to reinvent the wheel:
[PathMakeUniqueName]
http://msdn.microsoft.com/en-us/library/bb776476%28VS.85%29.aspx
But only works >= XP
Regards
Kerem
--
-----------------------
Beste Gr?sse / Best regards / Votre bien devoue
Kerem G?mr?kc?
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
Alexander Grigoriev replied to Kerem Gümrükcü
Kerem Gümrükcü replied to Alexander Grigoriev
Non, its shell32.dll, check exports. At first i was
expecting it in the helper library too,..
--
-----------------------
Beste Gr?sse / Best regards / Votre bien devoue
Kerem G?mr?kc?
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
Alexander Grigoriev replied to Kerem Gümrükcü
I remember that shell32.dll was replaced with IE update. Maybe the function
is only present after one such update.
rogero replied to Kerem Gümrükcü
d
oes
t
Ah - they are exported by ordinal not by name.
PathMakeUniqueName is ordinal #47 in shell32.dll on W2K
Regards,
Roger.

0f, &mesh, NULL); Win32 DirectX Graphics Discussions D3DXMatrixLookAtLH (1) D3DXMatrixRotationY (1) D3DXCreateCylinder (1) OnD3D9FrameRender (1) XP (1) IDirect3DDevice9 (1) DrawIndexedPrimitive (1) UnlockVertexBuffer (1) [Please do not mail me a copy of M, T1836%T<FEX5')A;G-L871I;VXH)FUA = % = O<FQD+#$P+C!F+# N, &8L, "XP M9BD[#0H)<&0S9$1E = FEC92T^4V5T5')A;G-F;W)M*"!$, T144U]73U), 1"PF C P, # P, #LL"B @.38N, S Y.38W.RTW, 2XR, 3<W, 3([ M, "XP, # P, # [+ H@("TY-BXS, #DY-C<[-S$N, C$W-S$R.S N, # P Y, 3$W.RP*(" M M.38N, S Y.38W.RTW, 2XR, 3<W, 3([, "XP, # P, # [+ H@(#DV+C, P.3DV-SLM M-S$N, C$W-S$R.S CLP+C P, # P, #LL"B @+3DV+C, P.3DV-SLW, 2XR, 3<W, 3([, "XP, # P M, # [+ H@("TY-BXS, #DY-C<[-S$N, C$W-S$R.S$Q C0S.3$Q-SLL"B @+3DV+C, P.3DV-SLW M, 2XR, 3<W, 3([, "XP, # P, # [+ H@("TY-BXS, #DY-C<[+3<Q+C(Q-S<Q, CLQ M, 3 3@L, "PQ.3LL"B @, SLQ.2PV+#$X.SL*"B @ M365S:$YO<FUA;', @('L*(" @-CL*(" @, "XP, # P, # [, "XP, # P, # [+3$N M, # P, # P.RP*(" @, "XP, # P, # [, "XP, # P, # [, 2XP, # P, # [+ H@(" P
Refresh network connections? C++ / VB I maintain an XP-XP VPN connection between home and work. It's set to "redial if line is dropped I don't mean open the window and simulate F5.) Thanks. - - - Vince Win32 Networks Discussions XP (1) VPN (1) Vince (1) Disfunctional (1) Window (1) Redial (1) Icon (1) may be ipconfig' options. c: \ > ipconfig / renew YourConnectionName ali keywords: Refresh, network, connections? description: I maintain an XP-XP VPN connection between home and work. It's set to redial if line is dropped
happens? And if I click, it does not do anything also. Win32 DirectX Graphics Discussions XP (1) Ghz (1) E871E169F96 (1) Notebook (1) 38P.S (1) Dxut (1) Bits (1) TME C!F M+" P+C!F+"TS, "XP9B I.PT*(" @($0S1%A614-43U(S('95<%9E8R@@, "XP M9BP@, 2XP9BP@, "XP9B I.PT*(" @($0S1%A-051225A!, 38@;6%T5FEE = SL- M"B @("!$, T1836%T<FEX3&]O P+C P, # P, #LP+C P, # P M, #LP+C P, # P, #LL"B @, "XP, # P, # [, "XP, # P, # [, "XP, # P, # [+ H@ M(# N, # P, # P.S N, # P, # P.S N, # P, # P.RP P+C P, # P, #LP+C P M, # P, #LP+C P, # P, #LL"B @, "XP, # P, # [, "XP, # P, # [, "XP, # P, # [ M+ H@(# N, # P, # P.S N, # P, # P.S N, # P, # P.RP
Vista C++ / VB I have a program that is 32-bit and runs fine on XP, XP Pro and 2000. It has mainly VB 6 and C code. The application installs on compatible with abomination known as Vista? Thanks. VB Vista Compatibility Discussions Vista (1) VB (1) XP (1) Database (1) Adminstrator (1) Windows (1) ActiveX (1) Faral (1) Have you tried right The application installs on Vista but will not run. It installs and runs fine on XP, XP Pro and Windows 2000. It is a 32-bit app in VB6 with some C 3D ire = 3D I am using SEAU as the install program. It installs fine on XP and 2000. I wonder if I can give the permission to read / write to the for it? - Kev I am using SEAU as the install program. It installs fine on XP and 2000. I wonder if I can give the permission to read / write to the on, Vista description: I have a program that is 32-bit and runs fine on XP, XP Pro and 2000. It has mainly VB 6 and C code. The application installs