News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Adding an extra extension to a filename = Renaming a file

Started by K_F, October 19, 2014, 01:32:09 AM

Previous topic - Next topic

K_F

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....'



K_F

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

deleted

dedndave

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

you could write a general-purpose wrapper function to simplify a variety of different operations
; 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

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

Or simply

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


.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,