Solved Batch script to auto copy new file to destination

June 15, 2012 at 21:04:47
Specs: Windows
I have this script made from various tutorial, figuring out how to show clean console and have something that I need someone's help. I'm not a coder and this is just my attempt to learn how to code simple batch script based on tutorial on the net.

Basically, this script will check a source folder for any new files, and check if the destination folder have it and if not it will copy the new file to destination folder.

[code]
@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:: SEC Auto Transfer ::
@Echo:: 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
[/code]


The Output:

[code]
::::::::::::::::::::::::::::::::::::::::::::::
:: 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...
[/code]


My problem 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?
Logic of doing this would to avoid anyone from tampering the batch script and rather just make it easier to change configuration by just editing the .ini file.

Also, I want to show last copied file name just before the "Refreshing..." line, how can I do this?


See More: Batch script to auto copy new file to destination

Report •

#1
June 16, 2012 at 05:38:51
✔ Best Answer
::pulls content of dest.ini and stores it in %_Dest%
for /f "tokens=* delims=" %%I IN (c:\dest.ini)DO (Set _Dest=%%I)
::contents of dest.ini
W:\Edited\20120615\test

:: mike


Report •

#2
June 16, 2012 at 06:37:22
Thanks Mike, it works perfectly!!

@Echo Off
Set _Delay=1
for /f "tokens=* delims=" %%I IN (c:\source.ini)DO (Set _Monitor=%%I)
for /f "tokens=* delims=" %%I IN (c:\dest.ini)DO (Set _Dest=%%I)
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 /f "tokens=* delims=" %%I IN (c:\timer.ini)DO (Set _Timer=%%I)
for /l %%a in (%_Timer%,-1,0) do (
@Echo::::::::::::::::::::::::::::::::::::::::::::::
@Echo:: SEC Auto Transfer ::
@Echo:: 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 Currently recording:
FOR /F %%a in ('xcopy "%_Monitor%\*.SEC" "%_Dest%" /L /Y') DO (
IF NOT EXIST "%_Dest%.\%%~nxa" ECHO %%~nxa
)
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


Just curious, the script stopped working if I removed the "Goto :_Xfer". It was just a subsection to skip delay, script should be working even without that flag.

I managed to show next file in queue to copy by adding
ECHO Currently recording:
FOR /F %%a in ('xcopy "%_Monitor%\*.SEC" "%_Dest%" /L /Y') DO (
IF NOT EXIST "%_Dest%.\%%~nxa" ECHO %%~nxa
)

Although it keep flashing in sync with the countdown, quite annoying.
Now, how can I show last copied file?

Another question, you see I have 3 ini files, how can I just have 1 ini files with all the information?

[Source];
source=
[Destination];
dest=
[Timer];
timer=

How to get data from these? I don't quite understand the logic for code below...
>> for /f "tokens=* delims=" %%I IN (c:\source.ini)DO (Set _Monitor=%%I)


Many thanks for the help, appreciate it so much.


Report •

#3
June 16, 2012 at 07:36:10
I am sure there is some easier way to do this, but for me this made sense to cut out the complications. Glad that it worked, thanks for replying.

:: mike


Report •

Related Solutions

#4
June 16, 2012 at 07:56:42
Got it.

for /f "tokens=1,2,3 delims==" %%a in (config.ini) do (
if %%a==source set _Monitor=%%b
if %%a==destination set _Dest=%%b
if %%a==timer set _Timer=%%b
)

Last problem, how can I show last copied file??


Report •

#5
June 16, 2012 at 08:27:35
Note: There are better ways to watch for files. VBScript or PowerShell gives you access to WMI file creation events (Example), and if you have Vista or later, you can use .NET's FileSystemWatcher. Either option is designed to to what you want, without needless clock cycle burning and/or flooding your network with superfluous pings.

How To Ask Questions The Smart Way


Report •

#6
June 16, 2012 at 12:22:31
To be honest, I didn't read through the script. I just saw the comment about wanting to draw from .ini files.

:: mike


Report •

#7
June 16, 2012 at 14:41:09
Oh, don't get me wrong; I don't expect the OP to respond in any way nor do more than skim my post, I just feel someone should tell him there are better ways.

How To Ask Questions The Smart Way


Report •

#8
June 16, 2012 at 17:07:19
Thanks Razor, I'm sure there are better ways to do this but honestly, I do not know how. I tried reading articles from your given links but hardly can wrap my brain onto them.

If this script can be made simpler, better, hope you can teach me how.

Thanks for helping :)


Report •

#9
June 17, 2012 at 09:21:47
I am working towards learning vb scripting. (I know it would make things a lot easier) I just never seem to be able to pick it up. Thanks for passing on your less limited experience.

:: mike


Report •

#10
June 17, 2012 at 17:46:12
Mike, thanks for the help. Appreciate if you could show me the way of doing this?
Sorry if I give a hard time to anyone, didn't mean it.

Report •

Ask Question