Rename a file, possible?

Started by ENAY, August 29, 2018, 11:07:18

Previous topic - Next topic

ENAY

So I'm sat in Blitz3D, haven't opened it in years.

Over a decade ago, I could have sworn I wrote a very simple script, which read a directory of all files, and then renamed them. Basically a batch renaming function. Now I'm looking at the B3D manual and nothing seems to be in there, now I know you can do LoadImage and SaveImage and have the rename in there, but what about other files that Blitz cannot load?

Is this possible or have I dreamt it?

TomToad

You probably either used a user lib which exposed the movefile function in the Windows API, or you used CopyFile() function of Blitz3D followed by DeleteFile().
------------------------------------------------
8 rabbits equals 1 rabbyte.

GrindalfGames

could copyfile do it?
looking at it you could choose a new name and place it in a new folder and then use deletefile to remove all the old files afterwards

Henri

Hi,

in Blitzmax this would be easy, as there are extensive file operation functions already builtin:

Quote
StripDir       Strip directory from a file path
StripExt       Strip extension from a file path
StripAll       Strip directory and extension from a file path
StripSlash       Strip trailing slash from a file path
ExtractDir       Extract directory from a file path
ExtractExt       Extract extension from a file path
CurrentDir       Get Current Directory
RealPath       Get real, absolute path of a file path
FileType       Get file type
FileTime       Get file time
FileSize       Get file size
FileMode       Get file mode
SetFileMode      Set file mode
CreateFile       Create a file
CreateDir       Create a directory
DeleteFile       Delete a file
RenameFile       Renames a file
CopyFile       Copy a file
CopyDir       Copy a directory
DeleteDir       Delete a directory
ChangeDir       Change current directory
ReadDir       Open a directory
NextFile       Return next file in a directory
CloseDir       Close a directory
LoadDir       Load a directory
OpenFile       Open a file for input and/or output.
ReadFile       Open a file for input.
WriteFile       Open a file for output.
CloseFile       Closes a file stream.


Although one could probably write a plain old bat file to do this.

-Henri

- Got 01100011 problems, but the bit ain't 00000001

TomToad

There is the user .decls file for kernel32 here http://www.mojolabs.nz/codearcs.php?code=1180.  Just put that into your userlibs director, rename the extension to .decls, then launch B3D.  To rename a file, just use api_MoveFile(OriginalName$, NewName$)
------------------------------------------------
8 rabbits equals 1 rabbyte.

Henri

Copying the file and then deleting the original seems a bit blunt (which I assume MoveFile does) ?

-Henri
- Got 01100011 problems, but the bit ain't 00000001

TomToad

No, it only copies the file when moving to another drive/partition.  On the same drive, it only changes the directory structure so the new directory points to the file instead of the old.  If you move to the same directory, but different name, then only the one entry is updated,  This is why it is near instantaneous whenever you move a large file or folder on the same drive, but can take several seconds or minutes when moving across drives.

Running this code for a 1.85 Gb file gives this result, showing that moving/renaming a file is not simply making a copy.
a = MilliSecs()
api_CopyFile ("C:\Users\noroa\Downloads\linuxmint-18.3-cinnamon-64bit.iso", "C:\Users\noroa\Downloads\test.iso",False)
c = MilliSecs() - a
a = MilliSecs()
api_MoveFile ("C:\Users\noroa\Downloads\test.iso","C:\Users\noroa\Downloads\testmoved.iso")
m = MilliSecs() - a
DebugLog  "Time to copy: "+c
DebugLog  "Time to move: "+m
WaitKey()

Time to copy: 8814
Time to move: 2
------------------------------------------------
8 rabbits equals 1 rabbyte.

Mikey

#7
Here's something similar that might work for you:
file$="c:\originalname"
destination$="c:\newfilename"

Print "Press any key to rename your file"

WaitKey()

CopyFile file$,destination$

Then you could use:
;
DeleteFile "c:\originalname"


To remove the old names

_PJ_

A naive approach hjacking the windows functionality to process file renaming.

Function RenameFile(Path$="",NewFile$="",Count_Renamed=False)
   ;Count_Renamed should be incremented each call for batch processing
   Local Consecutor$=""
   If (Count_Renamed) Then Consecutor$=Str(Count_Renamed)
   If (Right(Path,1)=".") Then Return
   If (Path$="") Then Path$=CurrentDir()
   If (Right$(Path,1)="\") Then Path$=Left(Path$,Len(Path$)-1)
   If (NewFile$="")
      If (FileType(Path$)=2)
         NewFile$="Renamed Folder"
         ExecuteRename(Path$,NewFile$+Consecutor$)
      Else
         If (FileType(Path$)=1)
            NewFile$="Renamed File"
         Else
            Return
         End If
      End If
   End If   
   Local Extension_separator=Instr(Path$,".")
   Local Extension$=""
   Local Path_Separators=Instr(Path$,"\",Path_Separators+1)
   Local Last_Path=0
   While (Path_Separators)
      Last_Path=Path_Separators+1
      Path_Separators=Instr(Path$,"\",Path_Separators)
   Wend
   If ((Not(Instr(NewFile$,"."))) And (Last_Path<Extension_separator))Then Extension$="."+Right(Path,(Len(Path)-Extension_separator))
   Return ExecuteRename(Path$,NewFile$+Consecutor$+Extension$)
End Function
;Requires Kernel32.dll library declarations
Function ExecuteRename%(Path$,NewFile$)
   api_MoveFileA(Path$, NewFile$)
   Local Last_Path=0
   While (Path_Separators)
      Last_Path=Path_Separators
      Path_Separators=Instr(Path$,"\",Path_Separators+1)
   Wend
   Local Parent$=Left$(Path,Last_Path)+NewFile$
   Return (FileType(Parent$))
End Function

ENAY

Thanks for all your help guys and sorry I haven't replied until now. In the end I manually renamed 100 files by hand but I will certainly be doing something in Blitzmax in the future. I tried renaming all my files to .png and using LoadFile and SaveFile, it didn't work. Haha :)

Mikey

Quote from: ENAY on September 02, 2018, 02:34:09
Thanks for all your help guys and sorry I haven't replied until now. In the end I manually renamed 100 files by hand but I will certainly be doing something in Blitzmax in the future. I tried renaming all my files to .png and using LoadFile and SaveFile, it didn't work. Haha :)

I thought you wanted to rename the first part of your files. the portion before the period (.)
Ex: filename.png being changed to othername.png

Changing the name after the period can prevent the file from being opened or being recognized by the original creating program.