March 06, 2021, 01:32:28 AM

Author Topic: Rename a file, possible?  (Read 1947 times)

Offline ENAY

Rename a file, possible?
« on: August 29, 2018, 11:07:18 AM »
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?

Offline TomToad

Re: Rename a file, possible?
« Reply #1 on: August 29, 2018, 12:04:10 PM »
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.

Offline grindalf

Re: Rename a file, possible?
« Reply #2 on: August 29, 2018, 12:36:05 PM »
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

Offline Henri

Re: Rename a file, possible?
« Reply #3 on: August 29, 2018, 01:18:33 PM »
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

Offline TomToad

Re: Rename a file, possible?
« Reply #4 on: August 29, 2018, 02:41:14 PM »
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.

Offline Henri

Re: Rename a file, possible?
« Reply #5 on: August 29, 2018, 02:54:03 PM »
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

Offline TomToad

Re: Rename a file, possible?
« Reply #6 on: August 29, 2018, 04:56:16 PM »
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.
Code: [Select]
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()
Code: [Select]
Time to copy: 8814
Time to move: 2
------------------------------------------------
8 rabbits equals 1 rabbyte.

Offline Mikey

Rename a file, possible?
« Reply #7 on: August 29, 2018, 08:05:59 PM »
Here's something similar that might work for you:
Code: [Select]
file$="c:\originalname"
destination$="c:\newfilename"

Print "Press any key to rename your file"

WaitKey()

CopyFile file$,destination$
Then you could use:
Code: [Select]
;
DeleteFile "c:\originalname"
 

To remove the old names

Offline _PJ_

Re: Rename a file, possible?
« Reply #8 on: September 01, 2018, 11:23:03 AM »
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

Offline ENAY

Re: Rename a file, possible?
« Reply #9 on: September 02, 2018, 02:34:09 AM »
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 :)

Offline Mikey

Rename a file, possible?
« Reply #10 on: September 02, 2018, 09:38:40 PM »
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.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal