Microsoft KB Archive/60229

Using Filename Replacement in MS-DOS Batch File PSS ID Number: Q60229 Article last modified on 01-22-1993 PSS database name: O_MSDOS

3.30 4.00 4.01

MS-DOS

Question:

I am using an MS-DOS batch file to copy files across our network. One of the files the batch file copies is a file with the same name as the batch file. For example:

GETPP.BAT contains the following:

COPY NETWORK:GETPP.BAT COPY NETWORK:other files…

The problem is that once this new GETPP.BAT file is copied, MS-DOS executes the next instruction from this new file and NOT my old batch file which (supposedly) is still running.

How can I prevent MS-DOS from starting to execute the new batch file? I want to have a batch file (for example, GETPP.BAT) that copies files from the network. However, as we have 50 users on this network, I want to be able to distribute the changed copy of the GETPP.BAT file using the example above. Can this be done?

Response:

The problem described above is expected behavior. You cannot replace the currently executing batch file with another file with the same name until the batch file has finished executing. There is no way to prevent MS-DOS from beginning to execute the new replacement file because the old batch file no longer exists, as far as the batch command processor is concerned.

You can still accomplish what you want to do by breaking up the job into two batch files. The first batch file, named GETPP.BAT, will copy the replacement copy of GETPP.BAT from your network under a different name. It then will dispatch another batch file called REN_BAT.BAT that has command line parameters specifying the new name and old name. The second batch file will delete the old name and then rename the new name to the old name.

The following are some sample batch files that perform this type of functionality. To execute these files, replace “t:” with the name/path of your network. You also must alter your updating scheme to include copying the second batch file as well.

rem @echo off rem GETPP.BAT rem Copies target file as different name and invokes rem secondary batch file as last command to rename rem replacement copy of getpp.bat (getppx.bat) to getpp.bat copy t:.bat getppX.bat ren_bat getppX.bat getpp.bat

rem @echo off rem REN_BAT.BAT rem Invoke with NEW name as 1st argument, and OLD name as 2nd argument rem Deletes old name and renames new name to old name del %2 ren %1 %2

Additional reference words: 3.30 4.00 4.01 noupd

Copyright Microsoft Corporation 1993.