DosTips.com

A Forum all about DOS Batch
It is currently 27 Mar 2017 16:28

All times are UTC-06:00




Post new topic  Reply to topic  [ 6 posts ] 
Author Message
PostPosted: 09 Jan 2017 06:52 
Offline

Joined: 12 Jul 2016 12:58
Posts: 4
:: 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


Top
   
PostPosted: 09 Jan 2017 07:26 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 2555
Location: Germany
Rather try ROBOCOPY if you want to copy files using network paths.

Steffen


Top
   
PostPosted: 09 Jan 2017 11:27 
Offline

Joined: 21 Mar 2014 08:50
Posts: 381
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%")


Top
   
PostPosted: 09 Feb 2017 02:35 
Offline

Joined: 12 Jul 2016 12:58
Posts: 4
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.


Top
   
PostPosted: 09 Feb 2017 03:43 
Offline

Joined: 21 Mar 2014 08:50
Posts: 381
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.


Top
   
PostPosted: 09 Feb 2017 04:49 
Offline

Joined: 12 Jul 2016 12:58
Posts: 4
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.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 6 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Baidu [Spider] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited