# Batch file to automatically copy newest file to destination



## astronyu (Jun 15, 2012)

Hi, I'm new here and I hope to get help from you guys.

In regards to this thread:
http://www.techsupportforum.com/forums/f128/batch-file-to-run-when-a-file-gets-modified-513919.html

Here is my version:

```
@Echo Off
Set _Delay=10
Set _Monitor=C:\Log\Editing\Data\167DATA
Set _Dest=W:\Edited\20120615\test
Set _Base=%temp%\BaselineState.dir
Set _Chck=%temp%\ChkState.dir
Set _OS=6
Ver|Findstr /I /C:"Version 5">Nul
If %Errorlevel%==0 Set _OS=5 & Set /A _Delay=_Delay*1000
:_StartMon
Call :_SetBaseline "%_Base%" "%_Monitor%"
:_MonLoop
If %_OS%==5 (Ping 1.0.0.0 -n 1 -w %_Delay%>Nul) Else Timeout %_Delay%>Nul
Call :_SetBaseline "%_Chck%" "%_Monitor%"
FC /A /L "%_Base%" "%_Chck%">Nul
If %ErrorLevel%==0 Goto _MonLoop
::
copy "%_Monitor%\*.SEC" "%_Dest%"
::
Echo.Change Detected
Goto :_StartMon
:::::::::::::::::::::::::::::::::::::::::::::::::::
:: Subroutine
:::::::::::::::::::::::::::::::::::::::::::::::::::
:_SetBaseline
If Exist "%temp%\tempfmstate.dir" Del "%temp%\tempfmstate.dir"
For /F "Tokens=* Delims=" %%I In ('Dir /S "%~2"') Do (
Set _Last=%%I
>>"%temp%\tempfmstate.dir" Echo.%%I
)
>"%~1" Findstr /V /C:"%_Last%" "%temp%\tempfmstate.dir"
Goto :EOF
```

Currently it just will copy everything from the folder even though there are already existing files on the destination. 

My goal:
1- Copy ONLY the latest file from source to destination.
2- Skip copying if file existed.
3- To make the script ask user which folder to watch before running.
e.g: Watch folder?: 167DATA


Hope someone can help.

Thank you.


----------



## astronyu (Jun 15, 2012)

4- To make the script ask for destination path.
5- When this batch script first run, it shows blank screen until the countdown finish (time delay). Can I make it so it run the first time without delay?

Hope someone can help.


----------



## astronyu (Jun 15, 2012)

Ok, I revised the code, so now it will scan the folder and will ONLY transfer file which is not already existing.


```
@Echo Off
Set _Delay=900
Set _Monitor=C:\Log\Editing\Data\167DATA
Set _Dest=W:\Edited\20120615\test
Set _Base=%temp%\BaselineState.dir
Set _Chck=%temp%\ChkState.dir
Set _OS=6
Ver|Findstr /I /C:"Version 5">Nul
If %Errorlevel%==0 Set _OS=5 & Set /A _Delay=_Delay*1000
:_StartMon
Call :_SetBaseline "%_Base%" "%_Monitor%"
:_MonLoop
If %_OS%==5 (Ping 1.0.0.0 -n 1 -w %_Delay%>Nul) Else Timeout %_Delay%>Nul
Call :_SetBaseline "%_Chck%" "%_Monitor%"
FC /A /L "%_Base%" "%_Chck%">Nul
If %ErrorLevel%==0 Goto _MonLoop
::
FOR /F %%a in ('xcopy "%_Monitor%\*.SEC" "%_Dest%" /L /Y') DO (
IF NOT EXIST "%_Dest%.\%%~nxa" xcopy "%%a" "%_Dest%" /Y
)
::
Echo.Change Detected
Goto :_StartMon
:::::::::::::::::::::::::::::::::::::::::::::::::::
:: Subroutine
:::::::::::::::::::::::::::::::::::::::::::::::::::
:_SetBaseline
If Exist "%temp%\tempfmstate.dir" Del "%temp%\tempfmstate.dir"
For /F "Tokens=* Delims=" %%I In ('Dir /S "%~2"') Do (
Set _Last=%%I
>>"%temp%\tempfmstate.dir" Echo.%%I
)
>"%~1" Findstr /V /C:"%_Last%" "%temp%\tempfmstate.dir"
Goto :EOF
```

But, let say the latest data in the folder is being write to and the script will show sharing violation. How can I get clean output in the console?


----------



## astronyu (Jun 15, 2012)

New revision.


```
@Echo Off
Set _Delay=1
::Set full path of the source folder
Set _Monitor=C:\Log\Editing\Data\167DATA
::Set full path of the destination folder
Set _Dest=W:\Edited\20120615\test
Set _Base=%temp%\BaselineState.dir
Set _Chck=%temp%\ChkState.dir
Set _OS=6
Ver|Findstr /I /C:"Version 5">Nul
If %Errorlevel%==0 Set _OS=5 & Set /A _Delay=_Delay*1000
Goto :_Xfer
:_StartMon
@echo off
SET R1=Start /w Sync1.vbs
Call :_SetBaseline "%_Base%" "%_Monitor%"
:_MonLoop
If %_OS%==5 (Ping 1.0.0.0 -n 1 -w %_Delay%>Nul) Else Timeout %_Delay%>Nul
Call :_SetBaseline "%_Chck%" "%_Monitor%"
FC /A /L "%_Base%" "%_Chck%">Nul
If %ErrorLevel%==0 Goto _MonLoop
::
for /l %%a in (1800,-1,0) do (
@Echo::::::::::::::::::::::::::::::::::::::::::::::
@Echo::        Made by Aizal 16 June 2012        ::
@Echo::::::::::::::::::::::::::::::::::::::::::::::
@Echo This script will scan SEC Data Folder for 
@Echo new files every 30 minutes and will copy 
@Echo it to the Server. Make sure you set correct 
@Echo folder before using this script.
ECHO.
ECHO.
ECHO Refreshing...
ECHO Please wait %%a seconds...
%R1%
cls
)
::
:_Xfer
FOR /F %%a in ('xcopy "%_Monitor%\*.SEC" "%_Dest%" /L /Y') DO (
IF NOT EXIST "%_Dest%.\%%~nxa" xcopy "%%a" "%_Dest%" /Y
)
::
Echo.Change Detected
ECHO.
Goto :_StartMon
:::::::::::::::::::::::::::::::::::::::::::::::::::
:: Subroutine
:::::::::::::::::::::::::::::::::::::::::::::::::::
:_SetBaseline
If Exist "%temp%\tempfmstate.dir" Del "%temp%\tempfmstate.dir"
For /F "Tokens=* Delims=" %%I In ('Dir /S "%~2"') Do (
Set _Last=%%I
>>"%temp%\tempfmstate.dir" Echo.%%I
)
>"%~1" Findstr /V /C:"%_Last%" "%temp%\tempfmstate.dir"
Goto :EOF
```
Output:

```
::::::::::::::::::::::::::::::::::::::::::::::
::        Made by Aizal 16 June 2012        ::
::::::::::::::::::::::::::::::::::::::::::::::
This script will scan SEC Data Folder for 
new files every 30 minutes and will copy 
it to the Server. Make sure you set correct 
folder before using this script.


Refreshing...
Please wait 1800 seconds...
```

Ok, I managed to get the console to show me clean output.
Now, how can I set the source and destination folder together with duration of refresh time to be user changeable without tampering with the code?

I'm thinking to use .ini file and call the .ini from the batch script but still figuring out how to do it. Anyone can help me in this?


----------

