简体   繁体   English

使用批处理脚本自动将最新文件复制到文件夹

[英]Using batch script to automatically copy latest file to folder

I would like to call for help on my attempt to create a script which will automatically check last modified file and transfer it to destination.我想寻求帮助以尝试创建一个脚本,该脚本将自动检查最后修改的文件并将其传输到目的地。

What I have is this:我所拥有的是:

@Echo Off
Set _Delay=1
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
)
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 (%_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

Now, if you noticed the script will compare the content of source folder with the content of the destination folder and will copy the files from source if there is not already file in destination.现在,如果您注意到该脚本会将源文件夹的内容与目标文件夹的内容进行比较,并且如果目标文件夹中还没有文件,则会从源文件夹复制文件。 I would like to know if someone can help me figuring out how to make the script to check the last modified file in source folder and ONLY copy it to the destination AND if the said file is currently in use/being write to then script will wait until it finished before trying to copy it again.我想知道是否有人可以帮助我弄清楚如何使脚本检查源文件夹中最后修改的文件并将其仅复制到目的地并且如果该文件当前正在使用/正在写入然后脚本将等待直到它完成后再尝试复制它。

Also, right now I have to edit source.ini file to specify which folder I want to monitor and each time I have to change the subfolder path to watch for changes, is there a way to watch parent folder for any new files and take that file to the destination?另外,现在我必须编辑 source.ini 文件以指定我要监视的文件夹,每次我必须更改子文件夹路径以观察变化时,有没有办法观察父文件夹中的任何新文件并采取它文件到目的地?

I found this script on the net but hoping to check the file not based on day but instead of time.我在网上找到了这个脚本,但希望不是基于日期而是时间来检查文件。

::Copy Files Made Or Modified Today
@echo off
setlocal
set source= <source directory>
set dest= <destination directory>
pushd "%source%"
set t=%date:~4%
::for /f %%a in ('dir /b /a-d /o-d') do call :PROCESS "%%a"
goto :eof
popd
:PROCESS
for /f %%j in ('echo %~t1') do set d=%%j
if "%d%"=="%t%" copy %1 "%dest%"
goto :eof

Hope to find the solution from here.希望能从这里找到解决方案。

Many thanks非常感谢

With this you'll get the last modified file:有了这个,您将获得最后修改的文件:

for /f %%A in ('dir /b /tw /o-d not-mandatory-file-mask') do (
   set last_file=%%A
   goto :endloop
)
:endloop

With this you'll copy the file if it is possible.有了这个,如果可能的话,您将复制文件。 If not it will try again and again:如果不是,它将一次又一次地尝试:

:tryagain
set /a errorlevel=0
copy some_dir\%last_file% %destination%
if %errorlevel% NEQ 0 (
   sleep 10
   goto :tryagain
) else (
   goto :end
)
:end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM