Rella
Rella

Reputation: 66935

batch file for copying files from one place to another

How can I program a bat file that would be capable of putting file.dll that is in the same folder that a .bat file is in into the windowsMainDir/system32 folder but only if the file does not already exist?

Upvotes: 1

Views: 5580

Answers (1)

indiv
indiv

Reputation: 17846

Create your batch file like "copy_file.cmd" and put in these contents:

@echo off
SET SRC="%~dp0file.dll"
SET DEST="%WINDIR%\system32\file.dll"
if not exist %DEST% copy /V %SRC% %DEST%

If the destination file does not exist, it will copy the source file to the destination. The /V switch makes copy verify that the file was copied correctly, and is optional.

The %~dp0 in SRC takes the drive d and path p from the variable %0 (the batch file's path) and uses that path as a prefix to file.dll. You want do do this to guarantee that the script always takes the file from the same directory as the batch file, and not the current directory. E.g., if your batch file was on a network drive mapped to H:, you could still run it from C:.

c:\> h:\shared_scripts\copy_file.cmd

Upvotes: 4

Related Questions