The MASM Forum

General => The Campus => Topic started by: K_F on October 19, 2014, 01:32:09 AM

Title: Adding an extra extension to a filename = Renaming a file
Post by: K_F 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
Title: Re: Adding an extra extension to a filename = Renaming a file
Post by: dedndave on October 19, 2014, 01:35:47 AM
ShellExecute ?

http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153%28v=vs.85%29.aspx)

no no - see next post   :P
Title: Re: Adding an extra extension to a filename = Renaming a file
Post by: dedndave on October 19, 2014, 01:37:37 AM
my mistake.....

SHFileOperation

http://msdn.microsoft.com/en-us/library/windows/desktop/bb762164%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/bb762164%28v=vs.85%29.aspx)

SHFILEOPSTRUCT

http://msdn.microsoft.com/en-us/library/windows/desktop/bb759795%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/bb759795%28v=vs.85%29.aspx)
Title: Re: Adding an extra extension to a filename = Renaming a file
Post by: K_F 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:
Title: Re: Adding an extra extension to a filename = Renaming a file
Post by: nidud on October 19, 2014, 02:05:35 AM
deleted
Title: Re: Adding an extra extension to a filename = Renaming a file
Post by: dedndave 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
Title: Re: Adding an extra extension to a filename = Renaming a file
Post by: dedndave on October 19, 2014, 02:22:09 AM
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
Title: Re: Adding an extra extension to a filename = Renaming a file
Post by: K_F 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)
Title: Re: Adding an extra extension to a filename = Renaming a file
Post by: ragdog on October 19, 2014, 06:38:44 AM
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,