【问题标题】:Need a script to continuous automatic file movement from one folder to another with file extension of txt需要一个脚本来连续自动将文件从一个文件夹移动到另一个文件夹,文件扩展名为 txt
【发布时间】:2020-02-23 03:16:23
【问题描述】:

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

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

@echo off
set "source=H:\Source\Send"
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

【问题讨论】:

  • > 只是问题中的错字吗?另外,我肯定会建议开始使用 powershell 编写脚本。当您需要做一些更复杂的事情时,这将有很大帮助。
  • COPY %SOURCE%\ "%%I" %target%?我想应该是COPY "%SOURCE%\%%I" "%target%"
  • 我认为您的整个想法很疯狂,我建议您将它们放到脚本而不是文件夹中,或者有一个运行脚本或其命令的上下文菜单项。或者,您可以在启动时使用一次RoboCopy,其/Mov 选项与/Mon:/Mot: 之一结合使用

标签: batch-file scheduled-tasks


【解决方案1】:

这是一个脚本,它将每秒检查一次 .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:\Source\Send"
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

【讨论】:

  • 非常感谢......脚本运行良好......你真的是一个救生员,...... :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多