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

Functions

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

How FileRename Works

Keywords:  filerename  

Question:

When I use the FileRename function, it doesn't rename the file in place, but copies the renamed file to the WinBatch directory. Am I missing something, or has there been a patch for this error?

I also think FileRename has a quirky habit of moving files rather than simply renaming them, if you fail to specify the path in the second parameter, ought to be considered a bug, and an annoying one, given the purpose of the FileRename() function.

The path should not be necessary if you've specified it in the first parameter. Normally I specify the path in both the first and second parameters, but if you forget to include the path in the second parameter the results can be both mystifying and irritating.

To me it seems counter-intuitive that the path in the second parameter is necessary. If you leave it out, you've told the function where the file is and what to call it, right? But if you leave out the path, you can easily have a large number of files end up a long way from where they're supposed to be. And then good luck in finding them! In my case, I eventually discovered the renamed files in the same directory as the WBT that "renamed" them. I guess my little program thought it needed company. :-)

According to the WIL help file, "...you cannot make a file change its resident disk drive with FileRename". That isn't true! My files were moved from O: to C:, no problem.

There's no telling where your files will end up with FileRename(). Heck, ones more adventurous files may even decide to move to Seattle! (Joke!) :-)

Any chance you guys could fiddle with the code a bit to ensure that in the next version of WIL files will stay put, rather than potentially going a-wandering when they're renamed? It should be a simple fix: if the path is absent from the second parameter, then use the path defined in the first parameter.

Answer:

  1. If you try to rename a file to a filename that already exists, FileRename will complain (as would the DOS ren command). You first need to test to see if the file with the new name exists, and if so, delete it, before you rename your old filename to your new filename. It's not considered an error, just how WinBatch (and DOS) works.

  2. If you do not specify a full path on the target filename, it assumes you want to rename it to the current directory. When WinBatch starts up its default current directory is usually the WInBatch directory. (Depends) So either specify a full path on the rename target or do a DirChange so that the default directory is what you want it to be.
The general idea is that the script has a default working directory (Seen with DirGet() and changed with DirChange() ). In any case, where path information is not provided, then the current path is substituted.

You can do a DirChange("target directory") to set the default directory wherever you want.

It's not likely that the FileRename function will be changed, as many scripts now depend on this mode of action, and changing how it works will break running scripts.


Article ID:   W13076
Filename:   FileRename Function General Info.txt
File Created: 1999:04:15:16:51:36
Last Updated: 1999:04:15:16:51:36