Copy only new file from server1 to server2 remote directory?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
izy
Posts: 43
Joined: 29 May 2012 13:29

Copy only new file from server1 to server2 remote directory?

#1 Post by izy » 27 Jun 2013 04:58

I would like to copy only a new file from server1/remote_dir1 to server2/remote_dir2 each day. Each day on server1/remote_directory1 is created a new file and started with FMS_*.txt .

How to do it? Please write me example...

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy only new file from server1 to server2 remote direct

#2 Post by foxidrive » 27 Jun 2013 20:48

Code: Select all

copy "\\server1\remote_dir1\FMS_*.txt" "\\server1\remote_dir2"

izy
Posts: 43
Joined: 29 May 2012 13:29

Re: Copy only new file from server1 to server2 remote direct

#3 Post by izy » 27 Jun 2013 22:57

But how just new file from server1\remote_dir1\FMS_*.txt to \\server2\remote_dir2?
Each day on server1\remote_dir1\ is a new file FMS_*.txt which only this file should be copied to server2\remote_dir2.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy only new file from server1 to server2 remote direct

#4 Post by foxidrive » 27 Jun 2013 23:39

Try this:

Code: Select all

@echo off
for /f "delims="  %%a in ('  dir "\\server1\remote_dir1\FMS_*.txt" /b /od ') do set "file=%%a"
copy "\\server1\remote_dir1\%file%" "\\server1\remote_dir2"

izy
Posts: 43
Joined: 29 May 2012 13:29

Re: Copy only new file from server1 to server2 remote direct

#5 Post by izy » 28 Jun 2013 00:05

for /f "delims=" %%a in (' dir "\\server1\remote_dir1\FMS_*.txt" /b /od ') do ----> I receive an error: The system cannot find the file specified. I think in this part is a problem : /b /od

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy only new file from server1 to server2 remote direct

#6 Post by foxidrive » 28 Jun 2013 00:43

I had to post in haste so I've changed the code above - which should work.

If it can't find the file then A) you don't have access to the server or B) the file is not there where you expect it to be or C) it is a foreign language and the codepage is wrong.

izy
Posts: 43
Joined: 29 May 2012 13:29

Re: Copy only new file from server1 to server2 remote direct

#7 Post by izy » 28 Jun 2013 01:49

Here is complete code below. This code always copy all files from server1 to server2. I would like to copy only last file posted on server1 to server 2.

@echo off
pushd "c:\Batch\FTP_fms_froud_one\"

set "db=file_database_DO_NOT_DELETE.txt"

if not exist "%db%" type nul>"%db%"

del list.ftp 2>nul
del list2.ftp 2>nul
del filelist.txt 2>nul
del filelist2.txt 2>nul

set server_name=servername1
set username=username1
set password=username2

set remote_directory=OTHERS/out
set filespec=FMS_*.txt
for /f "delims=" %%a in ("%filespec%") do set filetype=%%~xa


echo ---------------------------------------------------------------------------
echo %date% %time% Begin Transfer

(
echo open %server_name%
echo %username%
echo %password%
echo cd %remote_directory%
echo ls %filespec%
echo quit
)>list.ftp

FTP -s:list.ftp >filelist.txt

for /f "delims=" %%a in (
'type "filelist.txt"^|find /i "%filetype%"^|find /i /v "ftp>"'
) do cmd /c >> filelist2.txt echo %%a

move /y filelist2.txt filelist.txt >nul


for /f "delims=" %%a in ('type "filelist.txt"') do (
find /i "%%a"<"%db%" >nul || >>filelist2.txt echo %%a
)

(
echo open %server_name%
echo %username%
echo %password%
echo binary
echo cd %remote_directory%
)>list2.ftp


for /f "delims=" %%a in ('type "filelist2.txt"') do (
>>list2.ftp echo mget %%a
>>list2.ftp echo y
>>"%db%" echo %%a
)

echo quit>>list2.ftp

FTP -s:list2.ftp

del list.ftp 2>nul
del list2.ftp 2>nul
del filelist.txt 2>nul
del filelist2.txt 2>nul

echo %date% %time% Done FTP Transfer
echo.
echo.

echo ---------------------------------------------------------------------------
echo Starting FTP upload
(
echo open 192.xxx.xx.xx
echo username of server2
echo password of server2
echo binary
echo mput FMS_*.txt
echo quit
)>list2.ftp

FTP -i -s:list2.ftp

del list2.ftp


echo %date% %time% Done FTP Upload
echo.
echo.


popd

Post Reply