renaming a file

Blitz3D Forums/Blitz3D Beginners Area/renaming a file

julianbury(Posted 2003) [#1]
Hi, Y'all :-)

Good morning!

I need to rename files with my program.
There does not seem to be an obvious command to do it.
Copy and Delete seem to be the only route.
Is there a better way?


Tricky(Posted 2003) [#2]
Weird... I've looked it up in the documentation and I can't find it either... I'd normally say "RenameFile" would do the job, but that command does not seem to exist...
Really strange... Any language should include this...


Dock(Posted 2003) [#3]
- OpenFile
- ReadFile
- WriteFile
- CloseFile
- FilePos
- SeekFile
- ReadDir
- CloseDir
- NextFile
- CurrentDir
- ChangeDir
- CreateDir
- DeleteDir
- FileType
- FileSize
- CopyFile
- DeleteFile
- ExecFile

It seems that there isn't any sort of rename file command, or any file command that could achieve this other than the CopyFile and DeleteFile combo. I suppose something like this could be achieved with a userlib, but it seems odd nevertheless.


Tricky(Posted 2003) [#4]
Perhaps Mark forgot about this...

But it *is* something for the feature requests...


EOF(Posted 2003) [#5]
A bit ugly but this seems to work. It uses the DOS ren command:

REN drive:\folder\filename.ext newfilename.ext

oldfilename$="temp.txt"
newfilename$="BlitzPower!!.txt"

q$=Chr$(34)
ExecFile "cmd /c ren "+q$+CurrentDir$()+oldfilename$+q$+" "+q$+newfilename$+q$

If FileType(newfilename$) Print "Renamed!"

a$=Input$("Done ..")
End

NB: The above is from WinXP
Not tested on Win98 but I think you need
ExecFile "command /c ren "+q$+CurrentDir$()+oldfilename$+q$+" "+q$+newfilename$+q$



Tricky(Posted 2003) [#6]
I've been thinking too about Syntax Error's method...

Problem is indeed that WinXP has "Cmd", while win98 uses "Command".... If Blitz would have a way to read Evironment variables you could solve that problem...

(don't know the Blitz variant if it exists, but for the easiness I'll call it GetEnviron)

ExecFile GetEnviron("COMSPEC") & "/c ren OldFile.txt NewFile.txt"


If Blitz supports this, the problem is solved... (For the DOS-Call that is, an internal feature would be a lot better)...


EOF(Posted 2003) [#7]
Print GetEnv("comspec")


!-)


Tricky(Posted 2003) [#8]
So it DOES exist? (Didn't look it up in the documentation because I didn't need it so far, at least, not in Blitz)...

That solves the problem (for now)


soja(Posted 2003) [#9]
Just for kicks I tried to do it with a userlib. The actual rename seems to work, but it crashes immediately afterwards. (I couldn't find documentation on the function so I had to guess -- can anybody else?)

.\userlibs\msvcrt.decls:
.lib "msvcrt.dll"
Rename%(source$, dest$):"rename"


.\RenameTest.bb:
; Create a test file to rename
file = WriteFile("test.txt")
WriteLine(file, "testing...")
CloseFile(file)

; Try to rename it
notify Rename("test.txt", "renamed.txt")


(You'll find the renamed file in the .\tmp directory if you didn't save the .bb file first.)


Oldefoxx(Posted 2003) [#10]
A limitation of GetEnv(namedenv$) is that you have to know
in advance the name of the enviromental (nameenv$) that
you want the setting for. Some other languages allow you
to sequence the environmentals by a number reference,
beginning with 1. When a Nil string ("") is returned, you
have reached the end of the environmental space.

Note that you can add, delete, or alter the environmental settings by going through the Start/Settings/Control Panel/System/Advanced settings. The ones that appear are
controlled by your profile, so if you are logged in as a
different user, you may somewhat have different settings.

I wrote a version of the GetEnviron function using the
PowerBASIC PB/Win (Previously called PB/DLL) compiler, and
took advantage of the ENVIRON$() function in that language.
The code looked like this:

;note comments shown here with (;), not the {'} used in PB/WIN
;placed in a GetEnviron.BAS file and compiled with PB/Win:
#COMPILE DLL
;canned statements for LIBMAIN call inserted here to load DLL
;actual statements for DLL function below:
FUNCTION GetEnviron ALIAS "GetEnviron" (BYVAL Param1 AS LONG) AS EXPORT STRING
dim Param2 AS STRING
param2=ENVIRON$(param1)
IF LEN(PARAM2) THEN
  FUNCTION=param2
ELSE
  FUNCTION=CHR$(0)
END IF
END FUNCTION


Then I create a \PBBlitz3D\Userlibs\GetEnviron.decls file with the following in it:

.lib "<path>\GetEnviron.DLL" ;path needed if GetEnviron.DLL not in the UserLibs folder
GetEnviron$(parma1%):GetEnviron

Any and all .DECLS files found in UserLibs are automatically accessed by Pblitz when the IDE starts up.
The term after the colon (:) in the DECLS file is the actual name of the function as reported by the DLL header. Note that the ALIAS in PB/WIN and the name following the : in the .DECLS file must be exactly the same, or no match will be made. If the ALIAS was not used in the DLL source
file, the function name would probably have been rendered
into a C-compatable form, which would be a leading underscore character and the name in lower case -- in this instance, _getenviron.

Depending upon these factors, you could have made the following entry into the DECLS file:

.LIB "<path>\GetEnviron.Dll"
GetEnviron$(param1%):_getenviron

Of particular note is the fact that param1 was passed with
a BYVAL reference. This means the actual value was placed
on the stack and passed, rather than a variable reference.
This avoids the problem of different methods by different
compilers to represent variables. In some compilers, you
can "dereference" a variable by enclosing it in parens (),
meaning that the "equationa" indicated by the () is first
solved and the results (the value) is passed. You might
also use something like parm+0, whifh also forces an
eqpression evaluation before the resulting value is passed.

I would be glad to upload the GetEnviron$(param1%) DLL file that I completed, but to date no-one has explained how to put a binary file into the BlitzBASIC archives. But perhaps this explanation of how to make one and use it with Blitz will be as helpful.