Page 1 of 1

copy file

Posted: 08 Jul 2019 16:03
by lalat06bag
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!

Re: copy file

Posted: 09 Jul 2019 08:29
by penpen
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

Re: copy file

Posted: 09 Jul 2019 10:14
by lalat06bag
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,

Re: copy file

Posted: 11 Jul 2019 03:40
by penpen
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