Author Topic: Changing File Names using Win32 functions  (Read 2286 times)


  • Guest
Changing File Names using Win32 functions
« on: April 20, 2015, 05:34:10 pm »
My camera names its files something like this: "IMG_1023.JPG". I have in the past manually changed the "IMG_" to the file's date to something like this: "2015-04-19 1023.JPG". Sometimes I have to do this to a lot of files with multiple dates so I created a C program to do this drudgery for me. For those interested in doing a similar project I thought I'd share the major steps I used. I'll keep the fun and hard stuff for you to solve. Below I use sprintf() to do some char string handling. It is not a Win32 function nor is it considered "secure" as it could be susceptible to buffer overruns.

The project name I used was "FileDate". The program I used is Window based with scrolling. For the changing of the file names I created a routine called "FileDate()". I designed it to work in this sequence:
1. I found the first file using FindFirstFile() where the file to find is "IMG_*.*". I used a HANDLE called "hFind". This handle must stay open until all files are found.
2. I started a loop which will be used until all files beginning with "IMG_" have been renamed.
3. I opened the newly found file using CreateFile() and a temporary HANDLE I called "fptr2". According to GetFileTime() information (see step 4) the HANDLE "must have been created using the CreateFile function with the GENERIC_READ access right."
4. I read the file's date/time information using GetFileTime(). It can be used to find these three date/times: 1 when it was created, 2 when it was last accessed, and 3 when it was last modified. I used the date/time of when the file was created.
5. I closed the temporary file HANDLE "fptr2" using CloseHandle().
6. I converted the date/time of the file to "local" date/time using FileTimeToSystemTime() and then SystemTimeToTzSpecificLocalTime().
7. I copied the found file name to LastFileName[]. This will be needed later. The problem is that the found file cannot still be opened by FindNextFile() when it is renamed (it will cause an error).
8. I use sprintf() to create NewFileName[].
9. I use sprintf() to create a row message to display in the Window LastFileName[] (see step 7) and NewFileName[] (see step 8). 10. I found the next file using FindNextFile(). It is important that the HANDLE "hFind" is still opened at this time. If the next file is not opened by FindNextFile() then I set the variable DoneWithChanging to "YES" (it was initialized to "NO").
11. I make sure that NewFileName[] does not already exist. I use CreateFile() and the temporary HANDLE "fptr2".
12. I close "fptr2" using CloseHandle().
13. I rename the LastFileName[] (see step 7) to the NewFileName[] (see step 8) using MoveFile().
14. I test if DoneWithChanging==YES. If it is "YES" I "break" from the loop. If it is still "NO" I repeat the loop.
15. If I broke from the loop then I add a row message that I am done.
16. I adjust the scroll bar to the total number of row messages created.
17. I close the HANDLE "hFind" using FindClose().
18. I tell my paint handler to paint all the row messages using InvalidateRgn().
19. I return from the routine "FileDate()".
I hope this has been helpful to those wanting to rename files using Win32 functions.

Offline jj2007

  • Member
  • *
  • Posts: 516
Re: Changing File Names using Win32 functions
« Reply #1 on: April 20, 2015, 10:04:00 pm »
Very nice description!
The part with the row messages is a bit unclear. Is this a GUI or console app? If GUI, are you using a syslist32?