需要一个脚本来连续自动将文件从一个文件夹移动到另一个文件夹,文件扩展名为txt



我需要一个用于任务调度程序的脚本,它允许我将文件从一个文件夹移动到另一个文件夹,仅用于txt文件类型。这意味着当我把一个txt文件放进一个文件夹时,它需要立即移动到另一个文件夹。如果我放了除txt以外的任何文件,该文件都不应该移动。此txt文件移动需要24*7服务。

我使用了下面的脚本,但它正在移动所有类型的文件,包括BAT文件。也不连续工作。它需要从任务调度程序手动运行才能移动文件。

@echo off
set "source=H:SourceSend"
set "destination=F:Destination>"
For /F "delims=" %%I IN ('DIR %source%*.txt /A:-D /O:-D /B') DO COPY %SOURCE% "%%I" %target% & echo %%I & GOTO :END
:END

这里有一个脚本,它将每秒检查一次.txt文件并移动它们。我把它设计成一个运行连续循环的脚本。您可以使用任务调度程序启动它,也可以使用任务调度器在重复计划中运行它。它检查自己,如果有另一个实例已经在运行,它将不会再次运行。它不包含错误检查
未测试

@echo off
:: Use the parameter AuTosTaRt to start the actual loop.
:: This parameter should never be used directly/manually.
:: It should only be used by this script file to start itself.
if "%1"=="AuTosTaRt" goto :beginmove
:: Check if this script is already running
:: Note that it seems like there should be a better
:: reliable way to do this, but this is what I've
:: found over the years.
:: If the script is already running then exit.
:: Sometimes the windowtitle includes Administrator
tasklist /fi "windowtitle eq Administrator: Move text files"| find /i "cmd.exe" >nul && goto :eof
:: Sometimes the windowtitle doesn't include Administrator
tasklist /fi "windowtitle eq Move text files"| find /i "cmd.exe" >nul && goto :eof
:: Sometimes there is an odd timing issue where the script
:: is running but somehow the above lines do not find it.
:: Wait 5 seconds and check again just to make sure.
timeout /t 5 >nul
tasklist /fi "windowtitle eq Administrator: Move text files"| find /i "cmd.exe" >nul && goto :eof
tasklist /fi "windowtitle eq Move text files"| find /i "cmd.exe" >nul && goto :eof
:: If we get here, then the script isn't running.  Let's start it:
start "Move text files" /min cmd /d /c "%~dpnx0" AuTosTaRt
goto :eof

:beginmove
set "source=H:SourceSend"
set "destination=F:Destination"
:loop
move "%source%*.txt" "%destination%" >nul 2>nul
:: Pause 1 second.  We don't need the loop to go crazy.
timeout /t 1 >nul
goto :loop

最新更新