Author Topic: Adding an extra extension to a filename = Renaming a file  (Read 3609 times)

K_F

  • Member
  • *****
  • Posts: 1648
  • Anybody out there?
Adding an extra extension to a filename = Renaming a file
« on: October 19, 2014, 01:32:09 AM »
Cannot seem to find a 'Rename' function.

I'd like to backup a file by adding '.bak' extension to the existing file name

Something like    XYZ.txt ==> XYZ.txt.bak

I thought maybe I can do it 'inline' without having to load the file and then re-save it under a new extension ?
The general idea is to remove the file from the listbox filter, but not delete it
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

dedndave

  • Member
  • *****
  • Posts: 8829
  • Still using Abacus 2.0
    • DednDave


K_F

  • Member
  • *****
  • Posts: 1648
  • Anybody out there?
Re: Adding an extra extension to a filename = Renaming a file
« Reply #3 on: October 19, 2014, 01:49:02 AM »
Thank you sire !!
 :biggrin:

edt:Gee.. MS have made it messy !!

edt2: It's much easier to stick to CopyFile/DeleteFile for single files  :biggrin:
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

nidud

  • Member
  • *****
  • Posts: 2013
    • https://github.com/nidud/asmc
Re: Adding an extra extension to a filename = Renaming a file
« Reply #4 on: October 19, 2014, 02:05:35 AM »
or simply use MoveFile(OldName, NewName)

Quote
BOOL WINAPI MoveFile(
  _In_  LPCTSTR lpExistingFileName,
  _In_  LPCTSTR lpNewFileName
);

dedndave

  • Member
  • *****
  • Posts: 8829
  • Still using Abacus 2.0
    • DednDave
Re: Adding an extra extension to a filename = Renaming a file
« Reply #5 on: October 19, 2014, 02:17:11 AM »
SHFileOperation isn't that bad - lol
but, nidud has the right idea

someplace, Hutch had an example of SHFileOperation used to send a file to the recycle bin - very easy

dedndave

  • Member
  • *****
  • Posts: 8829
  • Still using Abacus 2.0
    • DednDave
Re: Adding an extra extension to a filename = Renaming a file
« Reply #6 on: October 19, 2014, 02:22:09 AM »
you could write a general-purpose wrapper function to simplify a variety of different operations
Code: [Select]
; recycle.asm written by Hutch

.486 ; create 32 bit code
.model flat, stdcall ; 32 bit memory model
option casemap :none ; case sensitive

include \masm32\include\windows.inc
include \masm32\include\masm32.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\shell32.inc
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\shell32.lib
include \masm32\macros\macros.asm

RecycleFile PROTO :DWORD

;


.data

fname1 db "c:\_recyclefiletest.000",0
fname2 db "c:\_recyclefiletest.001",0,0

buffer db 30 dup (0)

.code

;

start:
; create two 0 byte files
invoke CreateFile,ADDR fname1,
GENERIC_READ,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
invoke CloseHandle,eax
test eax, eax
jnz @F
print SADD("CloseHandle failed")
@@:
invoke CreateFile,ADDR fname2,
GENERIC_READ,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
invoke CloseHandle,eax
test eax, eax
jnz @F
print SADD("CloseHandle failed")
@@:

invoke RecycleFile,ADDR fname1

push eax
invoke GetLastError
invoke dw2a,eax,ADDR buffer ; what is this doing ?
print SADD(13,10,"GetLastError returned ") ; print macro
print ADDR buffer
pop eax
invoke dw2a,eax,ADDR buffer
print SADD(13,10,"RecycleFile returned ")
print ADDR buffer

print SADD(13,10,"Press enter to exit...",13,10)
invoke StdIn,ADDR buffer,1
exit


; This proc calls the SHFileOperation function to delete
; the specified file or files to the recycle bin. Each file
; must be specified with a fully qualified path (otherwise,
; the file will simply be deleted, without being placed in
; the recycle bin). Multiple files can specified by
; separating the individual paths with a null, and appending
; an additional null to the end of the final path.
;
; Returns zero for success, or nonzero for failure.
;
; Initializing the hwnd element to NULL proved to be
; necessary so the SHFileOperation function could delete
; a file that was created by the calling process.
;

RecycleFile proc pszFullPath:DWORD
LOCAL fo:SHFILEOPSTRUCT

mov fo.hwnd, NULL
mov fo.wFunc, FO_DELETE
m2m fo.pFrom, pszFullPath
mov fo.pTo, NULL
mov fo.fFlags,FOF_ALLOWUNDO
invoke SHFileOperation,ADDR fo

ret
RecycleFile endp

end start

K_F

  • Member
  • *****
  • Posts: 1648
  • Anybody out there?
Re: Adding an extra extension to a filename = Renaming a file
« Reply #7 on: October 19, 2014, 02:28:02 AM »
Thanks dudes.. looks like the MoveFile is the one..  :t

Dave.. I'll look at that function later as I'm just hankering after something simple at the mo  8)
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

ragdog

  • Member
  • ****
  • Posts: 610
Re: Adding an extra extension to a filename = Renaming a file
« Reply #8 on: October 19, 2014, 06:38:44 AM »
Or simply

This rename  XYZ.txt ==> XYZ.txt.bak

Code: [Select]
.data
szfile db " XYZ.txt",0
.data?
BackupFile db 256 dup (?)
.code
lea esi,szfile
lea edi,BackupFile

.while byte ptr [esi]!=0
lodsb
stosb
.endw

mov dword ptr [edi],'kab.'
mov byte ptr [edi+4],0
invoke CopyFile,addr szfile,addr BackupFile,FALSE

It gives many ways lstcat ,lstrcpy or what ever

regards,