hi i'm using following code to rename a file:

.data
OldName   db "a.txt",0
NewName  db "b.txt",0

.code
lea     dx, OldName
lea     di, NewName
mov     ah, 56h
int     21h
jc      BadRename
                         .
                         .
                         .

It compiles nice but produce an hard error when running. I think it has something to do with that DS is pointing the wrong place because of the .code/.data words  :?:. Can anyone help how i do this call correct ?  thanks.
Posted on 2007-06-13 04:30:34 by maxleif
I tried to set the es to ds

  mov      ax,ds
  mov      es,ax   

Still error during execution  :sad:. Btw i use masm to compile and haven't found any samles different from what i'm doing. Well i also tried with "mov ax,7156h" still without any succes. hmmm - i though renaming a file was a triviel task but obviusly it isn't.     
Posted on 2007-06-13 15:24:20 by maxleif
Try
    mov ax,seg OldName
    mov ds,ax
    mov es,ax

and make sure that "a.txt" exists.

Most DOS functions on error will return carry set and an error code in AX.
Posted on 2007-06-13 18:18:11 by sinsi
Should be a doddle, the name should change even if the original named file is open in a windows box.

I assume those zeros on the end of old + newname are null terminators.
Newname cannot already exist in the folder, or you will get a carry.
My own nulls are inside the "  "


ZONE
org cs:100
RENAME
mov dx,N1
mov di,N2
mov ax,5600
int 21
N1 db "ee.txt_"
N2 db "ff.txt_"
END
____________


Try your one as a micro comfile to see if it works as a standalone com executable.

If it does, then try cs segment overrides with dx and di in the main procedure.
(ES and/or DS may be messed up)
Posted on 2007-06-13 21:08:41 by eek
Hey maxleif,

This will give you an idea of how to finish. The rename function is for Win 95+,
but the principal is still the same. Since it handles long names, you might want to use it.

.model small
.stack 200h

.data
             
      day        db  '00'      ; storage for date
      month      db  '00'
      year        db  '00'
      orig_name  db    140 dup(?)
      new_name    db    128 dup(?)
      name_length db    ?
      ext_length  dw    ?
      extension  db    10 dup(?)
      storage    db    150 dup(?)
      fail        db    13,10,'File not found.',13,10,'$'
      nofile      db    13,10,'File not found.',13,10,'$'
     
      prompt      db    13,10,9,'                      (c) Elvis  2/1/04',13,10
                  db    13,10,'Filename to Rename ->  $'


.code
begin:
      mov      ax,@data
      mov      ds,ax
      mov      es,ax              ; need this for function 7156h
                   
      mov      dx, offset prompt
      mov      ah,9
      int      21h
             
      mov      orig_name,128      ; max characters in input
      lea      dx,orig_name      ; get filename
      mov      ah,0ch            ; flush keyboard buffer
      mov      al,0ah            ; buffered keyboard input
      int      21h
      xor      ax,ax
      mov      al, ; how many chars entered
      add      al,2              ; find position after last char
      mov      si,ax              ; move count to index register
      mov      orig_name,00  ; make it into ascii string
       
      mov      dx,offset orig_name + 2
      mov      ax,7143h          ; See if file exists
      int      21h                ; carry flag set if problem
                                         
      jc      short not_present
         
      mov      cl, ; how many bytes read in
      mov      name_length,cl    ; store filename length

      mov      ch,00
      mov      si,cx              ; move count to index register
                         
      mov      cl,  ; cx = total number of characters
                                  ; excluding the terminating <ENTER>

; Look for a '\' which would indicate a path was specified

chk_path:
      lea      si,
      xor      cx,cx
      mov      di,si
      mov      cl,
     
      mov      al,'\'
                                    ; c:\work.1\temp\test.asm
      add      di,cx
      std                          ; scan from right to left  <------
      dec      di                 
      repne    scasb              ; zero flag set if "\"
      pushf
      push    cx
     
      xor      di,di
      xor      si,si
      lea      si,
      pop      cx
      add      si,cx
      xor      cx,cx
      cld                          ; scan from left to right
      mov      di,si
      mov      cl,
     
      mov      al,'.'              ;
                                    ;
                                    ; c:\work.1\temp\test.asm
      repne    scasb              ; zero flag set if "."
      jnz      short scan

; Store the file extension

extens:

      mov      si,di              ; the scanned input now is the source
      mov      di,offset extension ;
      mov      ,cx
      rep      movsb              ; move remaining chars into extension
      jmp      short scan

not_present:
      mov      dx,offset nofile
      mov      ah,9
      int      21h
      mov      ax,4c01h
      int      21h


; Store the path

scan:
      lea      si,
      xor      cx,cx
      mov      di,si
      mov      cl,
     

      mov      al,'\'              ; c:\work.1\
      add      di,cx
      std                          ; scan from right to left
      dec      di
      repne    scasb              ; zero flag set if "\"
      popf
      jnz      short no_path
      add      cx,1                ; value is total # chars in path

no_path:
      push    cx
      mov      di,offset storage
      cld                          ; scan from left to right
      rep      movsb              ; store path to directory in storage
      mov      al,0
      stosb                        ; make path ASCIZ
      pop      cx
      mov      name_length,cl      ; store length of path

time:
      mov      ah,4                ; get time
      int      1ah

      mov      al,cl              ; store time
      xor      ah,ah              ; 
      mov      bl,10h
      div      bl                 
      add      ax,3030h
      mov      ,al
      mov      ,ah
      mov      al,dh
      xor      ah,ah
      div      bl
      add      ax,3030h
      mov      ,al
      mov      ,ah
      mov      al,dl
      xor      ah,ah
      div      bl
      add      ax,3030h
      mov      ,al
      mov      ,ah
                 
      mov      al,         ; build time-based new filename
      mov      byte ptr new_name, al

      mov      al,
      mov      byte ptr new_name + 1, al

      mov      al,
      mov      byte ptr new_name + 2, al
             
      mov      al,
      mov      byte ptr new_name + 3, al

      mov      al,
      mov      byte ptr new_name + 4, al

      mov      al,
      mov      byte ptr new_name + 5, al
         
      mov      di,offset new_name + 6   
      mov      al, '.'                  ; add the period (6 + 1)
      stosb
      mov      si,offset extension      ; add on file extension
      mov      cx,            
      rep      movsb
                   
      xor      cx,cx
      mov      di,offset storage        ;
      mov      cl,
      add      di, cx                  ; move to end of storage

      xor      cx,cx
      mov      si,offset new_name      ; source of extension
      mov      cx,         ; # of chars in the extension
      add      cx,7                    ; chars in new name plus the period
      rep      movsb                    ; add new_name to end of storage

is_a_path:
      xor      ax,ax
      xor      dx,dx
      mov      ax,7156h                ; LFN rename file  Win 95 +
      lea      dx, orig_name + 2        ; old name
      lea      di, storage              ; sets the new name
      int      21h
       
is_there:
      cmp      al,02h
      jz      not_there
      jmp      short exit

not_there:
      mov      dx,offset fail
      mov      ah,9
      int      21h
      mov      ax,4c01h
      int      21h

exit:
      mov      ax,4c00h
      int      21h

end begin
Posted on 2007-06-14 00:01:51 by skywalker
fail        db    13,10,'File not found.',13,10,'$'  <---String terminator '$' is in quotes.
Should your null terminator be in quotes?

No null terminator means it cant read the filename.
Posted on 2007-06-14 04:32:08 by eek

fail        db    13,10,'File not found.',13,10,'$'  <---String terminator '$' is in quotes.
Should your null terminator be in quotes?

No null terminator means it cant read the filename.



This is tasm code. What compiler are you using ?

If it's masm, you may have to do it like this.    "file_la_de_dah$"

I don't work much with 16 bit code all that much and when I do, I convert it
to tasm.

Sometimes there are several ways of handling strings.

Posted on 2007-06-14 04:54:06 by skywalker
Thaks for all replies  :D. I tried many things but can't get it to work. I use XP and a MASM32 compiler:

Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.




Inside the original programs there is lines like:
lea esi,
push esi
call GetTimeFormat
xor eax, eax
.
.
etc




I compile the code with succes using this .bat file:

SET MP=C:\masm32
%MP%\tools\inc2l\inc2l msvcrt.inc
%MP%\bin\ml /c /coff winbol.asm
%MP%\bin\polink /release /subsystem:windows /MAPINFO:EXPORTS /MAPINFO:FIXUPS
/MAPINFO:LINES /VERBOSE /MACHINE:IX86 /libpath:%MASMPATH%\lib winbol.obj

I have a file a.txt in the same folder as the program file :). When running the program with the modification suggested in this thread I alway get the error:

winbol.EXE has encountered a problem and needs to close.  We are sorry for the inconvenience.





Here is the program:

.386
.model flat, stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\advapi32.inc
include msvcrt.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\advapi32.lib
includelib msvcrt.lib

.data
OldName         db    "a.txt", 0
NewName         db    "b.txt", 0

.code
main:
  mov ax,ds
  mov es,ax   
  lea   dx, OldName
  lea   di, NewName
  mov  ah, 56h
  int    21h
  jc     BadRename
BadRename:

end main
Posted on 2007-06-14 05:47:09 by maxleif

Try
    mov ax,seg OldName
    mov ds,ax
    mov es,ax

and make sure that "a.txt" exists.

Most DOS functions on error will return carry set and an error code in AX.


This line:

mov ax,seg OldName

Gives me compiler error:

error A2004: symbol type conflict
Posted on 2007-06-14 05:51:30 by maxleif

Should be a doddle, the name should change even if the original named file is open in a windows box.

I assume those zeros on the end of old + newname are null terminators.
Newname cannot already exist in the folder, or you will get a carry.

Try your one as a micro comfile to see if it works as a standalone com executable.

If it does, then try cs segment overrides with dx and di in the main procedure.
(ES and/or DS may be messed up)


I'm sure it works with:

OldName        db    "a.txt", 0

because this is also what is doing in the original program which is working fine.

What do you mean by micro compile?
Posted on 2007-06-14 05:57:40 by maxleif

Hey maxleif,

This will give you an idea of how to finish. The rename function is for Win 95+,
but the principal is still the same. Since it handles long names, you might want to use it.


is_a_path:
       xor      ax,ax
       xor      dx,dx
       mov      ax,7156h                 ; LFN rename file  Win 95 +
       lea      dx, orig_name + 2        ; old name
       lea      di, storage              ; sets the new name
       int      21h


Doing this gives me error at programrun.

  mov ax,ds
  mov es,ax   
  mov      ax,7156h             
  lea      dx, OldName + 2       
  lea      di, NewName           
  ; lea  dx, OldName
  ; lea  di, NewName
  mov  ah, 56h
  int    21h
  jc    BadRename
Posted on 2007-06-14 06:01:42 by maxleif

fail        db    13,10,'File not found.',13,10,'$'  <---String terminator '$' is in quotes.
Should your null terminator be in quotes?

No null terminator means it cant read the filename.



It's not the problem because it is ended with ,0 in the original program.
Posted on 2007-06-14 06:02:52 by maxleif


fail        db    13,10,'File not found.',13,10,'$'  <---String terminator '$' is in quotes.
Should your null terminator be in quotes?

No null terminator means it cant read the filename.



This is tasm code. What compiler are you using ?

If it's masm, you may have to do it like this.    "file_la_de_dah$"

I don't work much with 16 bit code all that much and when I do, I convert it
to tasm.

Sometimes there are several ways of handling strings.





Tried that but gives me error at run. String end with ,0 because original program works fine with that.
Posted on 2007-06-14 06:06:05 by maxleif


Hey maxleif,

This will give you an idea of how to finish. The rename function is for Win 95+,
but the principal is still the same. Since it handles long names, you might want to use it.


is_a_path:
      xor      ax,ax
      xor      dx,dx
      mov      ax,7156h                ; LFN rename file  Win 95 +
      lea      dx, orig_name + 2        ; old name
      lea      di, storage              ; sets the new name
      int      21h


Doing this gives me error at programrun.

  mov ax,ds
  mov es,ax   
  mov      ax,7156h             
  lea      dx, OldName + 2       
  lea      di, NewName           
  ; lea  dx, OldName
  ; lea  di, NewName
-> mov  ah, 56h
-> int    21h
  jc    BadRename


It is because you don't need the 2 marked statements.

Try an altavista advanced search and put this in the
box marked "All these words"

Google is OK, but a sloppy second place. :-)

rename 56h source code

or

16 bit assembly code

You probably get the idea.

Get a copy of Ralf Brown's Interrupt List, it lists all the DOS interuppts.


Posted on 2007-06-14 06:16:16 by skywalker
Trust me i have now used about 5-6 hours totally just for doing lots of search in google and altavista too :)....but still i have no working solution. Pretty hard to get file rename to work  :sad:

What 2 marks do you think i should leave out? i have tried with ah=56h and ax=7156h and with ax=5600 .... no idea what to do now  :sad:
Posted on 2007-06-14 09:00:19 by maxleif
I just found this. Look for LFN_Code.zip if you want more source code.

; The program renames "TEXT.TXT" to "RENAME.TXT"
; Rename takes place in the current directory.


org 100h
start:

mov ah,56h ; open file read/write
lea dx,file ; points to file name
lea di,new ; renames to
int 21h

ret

file db 'TEST.TXT',0 ; File name
new db 'Rename.txt',0 ; New name

end
Posted on 2007-06-14 09:21:45 by skywalker
Yes i found that sample

http://www.programmersheaven.com/download/22037/ZipFileList.aspx

and that's also what i do. I think it has something to do how i compile it.

Perhaps i should try another compiler tasm or something. hmm. I used to program assembler on motorola many years ago but if any knows a link to masm for dummies i will start look at that :).
Posted on 2007-06-14 12:28:11 by maxleif
Have you looked on alt.lang.asm ?

There are posts there dealing with file renaming.

Posted on 2007-06-14 14:36:15 by skywalker
DOS INT 21h won't work from a Win32 program - you need to use the Win32 API.
Try using MoveFile

BOOL MoveFile(
  LPCTSTR lpExistingFileName,
  LPCTSTR lpNewFileName
);

Posted on 2007-06-14 18:32:20 by sinsi
From what I can remember way back in my old days of MASM DOS programming:

1) The DS register does not point to the data segment (for .EXE files) when the program is loaded. You must initialize it to the data segment yourself in order that the SI register can be used to point to the proper area of memory. You must also initialize the ES register to the data segment for the DI register to point to the proper area of memory (unless you use a segment override such as DS:DI).

2) Strings to be handled by the DOS interrupts all had to be terminated by the $ character).

Raymond
Posted on 2007-06-14 21:03:03 by Raymond