Page 1 of 1

please check this batch, work.... but have error during the code run

Posted: 09 Jan 2017 06:52
by sal21
:: need to copy \\Myserverdir\File.txt to C:\mydir\Test_(date_creation).txt

setlocal

:: the following are what I used to test - change to yours!
set folder=\\fils\FTP_Web\OUT
set file=TAB_L0928.txt
set target=\\roms\workarea$\FSRM107\Ges\DATABASE\CEC

:: get the file's date as dd/mm/yyyy

for /f "tokens=1" %%a in ('dir "%folder%\%file%" ^| find /i "%file%"') do set filedate=%%a

:: change the date to yyyymmdd

set date_created=%filedate:~-4,4%%filedate:~-7,2%%filedate:~0,2%

:: perform the copy, if the file is not present in the target

if not exist "%target%\L0928-%date_created%.txt" ^

copy "%folder%\%file%" "%target%\L0928-%date_created%.txt"

:: check on the copied file
dir "%target%\L0928-%date_created%.txt"

endlocal

exit /b 0

Re: please check this batch, work.... but have error during the code run

Posted: 09 Jan 2017 07:26
by aGerman
Rather try ROBOCOPY if you want to copy files using network paths.

Steffen

Re: please check this batch, work.... but have error during the code run

Posted: 09 Jan 2017 11:27
by Compo
If you use RoboCopy you will unfortunately need to copy and rename though.

Based on a guess at your date output, (09/01/2017), this may work:

Code: Select all

@Echo Off

Set "folder=\\fils\FTP_Web\OUT"
Set "file=TAB_L0928.txt"
Set "target=\\roms\workarea$\FSRM107\Ges\DATABASE\CEC"
Set "new=L0928-%%D%%C%%B.txt"

For /F "EOL= Delims=" %%A In ('Dir "%folder%\%file%"'
) Do For /F "Tokens=1-3 Delims=/ " %%B In ("%%A"
) Do If Not Exist "%target%\%new%" (RoboCopy "%folder%" "%target%" "%file%"
    Ren "%target%\%file%" "%new%")

Re: please check this batch, work.... but have error during the code run

Posted: 09 Feb 2017 02:35
by sal21
Compo wrote:If you use RoboCopy you will unfortunately need to copy and rename though.

Based on a guess at your date output, (09/01/2017), this may work:

Code: Select all

@Echo Off

Set "folder=\\fils\FTP_Web\OUT"
Set "file=TAB_L0928.txt"
Set "target=\\roms\workarea$\FSRM107\Ges\DATABASE\CEC"
Set "new=L0928-%%D%%C%%B.txt"

For /F "EOL= Delims=" %%A In ('Dir "%folder%\%file%"'
) Do For /F "Tokens=1-3 Delims=/ " %%B In ("%%A"
) Do If Not Exist "%target%\%new%" (RoboCopy "%folder%" "%target%" "%file%"
    Ren "%target%\%file%" "%new%")


Sorry for delay very busy...
tks for code! work great.
but...

this code overwrite the file if in destination dir just exists the file with same name?
Tks.

Re: please check this batch, work.... but have error during the code run

Posted: 09 Feb 2017 03:43
by Compo
sal21 wrote:Sorry for delay very busy...
tks for code! work great.
but...

this code overwrite the file if in destination dir just exists the file with same name?
Tks.
You can add an additional IF EXIST clause to the code. Also RoboCopy doesn't overwrite files if they are the same, if you wish to preserve one or other of those copies then there are a range of options available.

Type ROBOCOPY /? at the cmd.exe window prompt for those options.

Re: please check this batch, work.... but have error during the code run

Posted: 09 Feb 2017 04:49
by sal21
Compo wrote:
sal21 wrote:Sorry for delay very busy...
tks for code! work great.
but...

this code overwrite the file if in destination dir just exists the file with same name?
Tks.
You can add an additional IF EXIST clause to the code. Also RoboCopy doesn't overwrite files if they are the same, if you wish to preserve one or other of those copies then there are a range of options available.

Type ROBOCOPY /? at the cmd.exe window prompt for those options.


ok...
found this option /IS, but were in your code?
tks.