如何让任务调度程序运行代码以自动移动文件

问题描述:

你好,我希望你能提供帮助。

我有一个要求,即CADCAM软件产生4个文件。为了便于管家,我希望将这4个文件放入自己的目录中。所以我做了一些挖掘并写了一个小的vb.net程序来为我做这个。



这是一项正在进行的工作所以它有点凌乱,但最初的想法是让它在后台运行并经常检查4个文件的存在然后移动它们。



但是我认为这可能会更好使用任务调度程序作为任务运行,然后我可以去除所有杂乱的计时器位并进行一些错误检查。



理想情况下我想要任务调度程序仅在CADCAM软件实际运行时每隔一分钟左右运行此代码。但是我不知道如何在软件运行时间隔地运行任务(代码)。



任何想法都会被感激地接受。这是我在VB.net的第一次尝试,我唯一的另一个体验就是vba for office。



Hello I hope you can help.
I have a requirement where-by a CADCAM software produces 4 files. For the sake of housekeeping I wanted these 4 files to be placed into their own directory. so I did some digging and wrote a small vb.net program to do this for me.

Its a work in progress so its a bit messy but the initial idea was to have it running in the background and check very so often for the existence of the 4 files an then move them.

But then i thought that this might be better served to be run as a task using the task scheduler, then i can strip out all the messy timer bits and do some error checking as well.

Ideally I would like the task scheduler to only run this code every min or so whilst the CADCAM software is actually running. But I cant find out how to run the task (code) at interval only if the software is running.

Any Thoughts would be gratefully accepted. This is my first stab at VB.net my only other experience is vba for office.

Imports System.Timers
Imports System.Threading
Imports System
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions

Public Class Test
    Public Shared Sub Main()
        Dim myTimer As New System.Timers.Timer()
        AddHandler myTimer.Elapsed, New ElapsedEventHandler(AddressOf OnTimer)
        myTimer.Interval = 5000
        myTimer.Enabled = True
        myTimer.AutoReset = False
        While Console.Read()
            ' Thread.Sleep(1000)
        End While
    End Sub

    Shared Function ChckFlNm(FlNm As String) As Boolean
        Dim ChkFlNm As Boolean

        If Len(FlNm) = 10 Then
            If Left(FlNm, 1) = "S" Then
                If Regex.IsMatch(Mid(FlNm, 2, 5), "^[0-9 ]+$") Then
                    ChkFlNm = True
                End If
            End If
        End If

        Return ChkFlNm
    End Function

    Public Shared Sub OnTimer(ByVal source As [Object], ByVal e As ElapsedEventArgs)
        Dim Score As Integer
        Dim DirNm As String = ""
        Dim newDirNm As String
        Dim path As String
        Dim path2 As String
        'creating a DirectoryInfo object
        Dim mydir As DirectoryInfo = New DirectoryInfo("C:\Users\dave.MARLIN-LTD\Desktop\S20001")
        ' getting the files in the directory, their names and size
        Dim f As FileInfo() = mydir.GetFiles()
        Dim file1 As FileInfo

        If f.Length > 0 Then 'check to see if there are any files in the directory
            For Each file1 In f
                Console.WriteLine("File Found: {0} Size: {1}  ", file1.Name, file1.Length)
                Select Case file1.Extension
                    Case ".dtx"
                        If ChckFlNm(file1.Name) = True Then Score += 4
                    Case ".pjx"
                        If ChckFlNm(file1.Name) = True Then
                            Score += 8
                            DirNm = Left(file1.Name, 6)
                        End If
                    Case ".pgx"
                        If file1.Name = "start.pgx" Then
                            Score += 1
                        End If
                        If file1.Name = "stop.pgx" Then
                            Score += 2
                        End If
                End Select
            Next file1

            If f.Length = 4 Then
                If Score = 15 Then
                    newDirNm = mydir.FullName & "\" & DirNm
                    If (Not System.IO.Directory.Exists(newDirNm)) Then
                        System.IO.Directory.CreateDirectory(newDirNm)
                    End If
                    ' Move the files.
                    path = mydir.FullName & "\" & DirNm & ".dtx"
                    path2 = newDirNm & "\" & DirNm & ".dtx"
                    Console.WriteLine("DateTime: " & DateTime.Now)
                    File.Move(mydir.FullName & "\" & DirNm & ".dtx", newDirNm & "\" & DirNm & ".dtx")
                    Console.WriteLine("File moved {0} ---> {1}", DirNm & ".dtx", DirNm & "\" & DirNm & ".dtx")
                    File.Move(mydir.FullName & "\" & DirNm & ".pjx", newDirNm & "\" & DirNm & ".pjx")
                    Console.WriteLine("File moved {0} ---> {1}", DirNm & ".pjx", DirNm & "\" & DirNm & ".pjx")
                    File.Move(mydir.FullName & "\" & "start.pgx", newDirNm & "\" & "start.pgx")
                    Console.WriteLine("File moved {0} ---> {1}", "start.pgx", DirNm & "\" & "start.pgx")
                    File.Move(mydir.FullName & "\" & "stop.pgx", newDirNm & "\" & "stop.pgx")
                    Console.WriteLine("File moved {0} ---> {1}", "stop.pgx", DirNm & "\" & "stop.pgx")
                End If
            End If
        End If


        Dim theTimer As System.Timers.Timer = DirectCast(source, System.Timers.Timer)
        theTimer.Interval += 5000
        theTimer.Enabled = True

    End Sub


End Class

然后
ChkFlNm = True
结束 如果
结束 如果
结束 如果

返回 ChkFlNm
结束 功能

公开 共享 Sub OnTimer( ByVal source As [ Object ], ByVal e As ElapsedEventArgs)
Dim 得分作为 整数
Dim DirNm 作为 字符串 =
Dim newDirNm As String
Dim 路径作为 字符串
Dim path2 As String
' 创建DirectoryInfo对象
Dim mydir 作为 DirectoryInfo = DirectoryInfo( C:\ Users \dave.MARLIN-LTD \Desktop\S20001
' 获取目录中的文件,名称和大小
Dim f As FileInfo()= mydir.GetFiles()
Dim file1 As FileInfo

如果 f.Length> 0 然后 ' 检查目录中是否有文件
对于 每个 file1 f
Console.WriteLine( 找到文件:{0}大小:{1},file1.Name,file1.Length)
选择 案例 file1.Extension
案例 。dtx
如果 ChckFlNm(file1.Name)= True 然后得分+ = 4
案例 。pjx
如果 ChckFlNm(file1.Name)= True 那么
得分+ = 8
DirNm =左(file1.Name, 6
结束 如果
案例 。pgx
如果 file1.Name = start.pgx Then
分数+ = 1
结束 如果
如果 file1.Name = stop.pgx 然后
得分+ = 2
结束 如果
结束 选择
下一步 file1

如果 f.Length = 4 然后
如果得分= 15 然后
newDirNm = mydir.FullName& \& DirNm
如果 System.IO.Directory.Exists(newDirNm))然后
System.IO.Directory.CreateDirectory(newDirNm)
结束 如果
' 移动文件。
path = mydir.FullName& \& DirNm& 。dtx
path2 = newDirNm& \& DirNm& 。dtx
Console.WriteLine( DateTime:& DateTime.Now)
File.Move(mydir.FullName& \& DirNm& .dtx,newDirNm& \& DirNm& ; 。dtx
Console.WriteLine( 文件移动{0} ---> {1},DirNm& 。dtx,DirNm& \& DirNm& .dtx
File.Move(mydir.FullName& \& DirNm& 。pjx,newDirNm& \& DirNm& 。pjx
Console.WriteLine( 文件移动{0} ---> {1},DirNm& 。pjx,DirNm& \\ \\& DirNm& 。pjx
文件。移动(mydir.FullName& \& start.pgx,newDirNm& \& start.pgx
Console.WriteLine( 文件移动{0} ---> {1} start.pgx,DirNm& \& start.pgx
File.Move(mydir.FullName& \& stop.pgx,newDirNm& \ & stop.pgx
Console.WriteLine( 文件移动{0} ---> {1} stop.pgx,DirNm& \& stop.pgx
结束 如果
结束 如果
结束 如果


Dim theTimer As System.Timers.Timer = DirectCast (source,System.Timers.Timer)
theTimer.Interval + = 5000
theTimer.Enabled = True

结束 Sub


结束 Class
") Then ChkFlNm = True End If End If End If Return ChkFlNm End Function Public Shared Sub OnTimer(ByVal source As [Object], ByVal e As ElapsedEventArgs) Dim Score As Integer Dim DirNm As String = "" Dim newDirNm As String Dim path As String Dim path2 As String 'creating a DirectoryInfo object Dim mydir As DirectoryInfo = New DirectoryInfo("C:\Users\dave.MARLIN-LTD\Desktop\S20001") ' getting the files in the directory, their names and size Dim f As FileInfo() = mydir.GetFiles() Dim file1 As FileInfo If f.Length > 0 Then 'check to see if there are any files in the directory For Each file1 In f Console.WriteLine("File Found: {0} Size: {1} ", file1.Name, file1.Length) Select Case file1.Extension Case ".dtx" If ChckFlNm(file1.Name) = True Then Score += 4 Case ".pjx" If ChckFlNm(file1.Name) = True Then Score += 8 DirNm = Left(file1.Name, 6) End If Case ".pgx" If file1.Name = "start.pgx" Then Score += 1 End If If file1.Name = "stop.pgx" Then Score += 2 End If End Select Next file1 If f.Length = 4 Then If Score = 15 Then newDirNm = mydir.FullName & "\" & DirNm If (Not System.IO.Directory.Exists(newDirNm)) Then System.IO.Directory.CreateDirectory(newDirNm) End If ' Move the files. path = mydir.FullName & "\" & DirNm & ".dtx" path2 = newDirNm & "\" & DirNm & ".dtx" Console.WriteLine("DateTime: " & DateTime.Now) File.Move(mydir.FullName & "\" & DirNm & ".dtx", newDirNm & "\" & DirNm & ".dtx") Console.WriteLine("File moved {0} ---> {1}", DirNm & ".dtx", DirNm & "\" & DirNm & ".dtx") File.Move(mydir.FullName & "\" & DirNm & ".pjx", newDirNm & "\" & DirNm & ".pjx") Console.WriteLine("File moved {0} ---> {1}", DirNm & ".pjx", DirNm & "\" & DirNm & ".pjx") File.Move(mydir.FullName & "\" & "start.pgx", newDirNm & "\" & "start.pgx") Console.WriteLine("File moved {0} ---> {1}", "start.pgx", DirNm & "\" & "start.pgx") File.Move(mydir.FullName & "\" & "stop.pgx", newDirNm & "\" & "stop.pgx") Console.WriteLine("File moved {0} ---> {1}", "stop.pgx", DirNm & "\" & "stop.pgx") End If End If End If Dim theTimer As System.Timers.Timer = DirectCast(source, System.Timers.Timer) theTimer.Interval += 5000 theTimer.Enabled = True End Sub End Class


你只需创建一个简单的应用程序,它只能直接从main方法执行ontimer方法中发生的任何事情(你不需要使用一段时间了。然后简单地将其安排为您提到的任务。只需尝试任务调度程序,让它启动一些应用程序,如记事本,只是为了掌握它。



祝你好运!
You just make a simple application that only executes whatever is happening in the ontimer method once directly from the main method (you don't need to use a time anymore). Then simply schedule it as a task as you mentioned. Just try the task scheduler and have it start some application like notepad, just to get the hang of it.

Good luck!