- 1 year ago
Copy Single File to Destination Folder if Exist else Copy to Another Folder
Comment
1 / 1
You can use a simple batch file script to achieve this. Here's an example:
sql@echo off
setlocal
set "source_file=C:\path\to\source\file.txt"
set "destination_folder=C:\path\to\destination\folder"
set "alternate_folder=C:\path\to\alternate\folder"
if exist "%destination_folder%" (
copy "%source_file%" "%destination_folder%\"
) else (
copy "%source_file%" "%alternate_folder%\"
)
endlocal
echo Done.
This script sets the source_file
, destination_folder
, and alternate_folder
variables to the appropriate paths. It then checks if the destination_folder
exists, and if it does, copies the source_file
to that folder using the copy
command. If the destination_folder
does not exist, the script copies the source_file
to the alternate_folder
instead.
Note that in this example, the copy
command overwrites any existing file with the same name in the destination folder. If you want to avoid overwriting files, you can use the /Y
or /-Y
switch with the copy
command to either automatically overwrite or prompt before overwriting, respectively.