copy file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lalat06bag
Posts: 51
Joined: 10 Jan 2018 15:21

copy file

#1 Post by lalat06bag » 08 Jul 2019 16:03

Hi,

I need help in this.

i want to copy files from one directory to another. The only check is, if the file exists, it would overwrite else it would just copy. The actions should be logged in a log file with the file name.

Thank you!

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: copy file

#2 Post by penpen » 09 Jul 2019 08:29

This (untested) code sniplet might help you:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
>"logs\LogFile %date% %time::=%.txt" copy /Y "source\*.bat" "target"
Sidenote: You might want to change the used directories.

penpen

lalat06bag
Posts: 51
Joined: 10 Jan 2018 15:21

Re: copy file

#3 Post by lalat06bag » 09 Jul 2019 10:14

Thank you for helping me out. Kindly suggest.

this is coping and replacing. no issues with that. I am looking for this message

Overwrite \\xxx.xlsx? (Yes/No/All): Y
1 file(s) copied.

But this shows

XXX.xlsx
XXX.xlsx
2 file(s) copied.


Thanks,

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: copy file

#4 Post by penpen » 11 Jul 2019 03:40

I hope this "slightly" changed batch does the trick (untested):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
set "filter=*.bat"
set "workDir=."
set "sourceDir=source"
set "targetDir=target"
set "logDir=logs"

set "logFile=%logDir%\LogFile %date% %time::=%.txt"
set "answerFile=%workDir%\localYes.txt"

set "fileCount=0"
for /f %%a in ('dir /b "%sourceDir%\%filter%" ^| find /v /c ""') do set "fileCount=%%~a"

set "localYes="
>nul copy /y nul "%logFile%"
for /f "tokens=2 delims=(/)" %%a in ('^<nul copy /-y nul "%logFile%"') do (
	if not defined localYes set "localYes=%%~a"
)

>"%answerFile%" echo(%localYes%
for /l %%a in (1, 1, %fileCount%) do (
	>>"%answerFile%" echo(%localYes%
)

>"%logFile%" (<"%answerFile%" copy /-Y "%sourceDir%\%filter%" "%targetDir%")
Sidenote:
Don't run multiple instances of that skript, because then the answer file gets corrupted (overwritten) which may result in an error.

penpen

Post Reply