如何在批处理脚本

时间:2016-05-11 01:50:39

标签: batch-file

我有一个文件夹路径列表,我想循环访问并获取文件和创建日期,然后发送电子邮件通知,告知这些文件已上传到ftp。我有一切正常,但我无法循环文件夹来获取文件。我认为它循环遍历文本文件但是当我到达第二个循环时,我认为它只是循环遍历文本doc中的最后一个路径,因为变量被覆盖了。我尝试用括号括起第二个for语句,用于第一个for语句但是没有用。这是我的代码:

scanFTPCLients.bat

 @echo off
 setlocal EnableDelayedExpansion
 cls
 @pushd %~dp0
 set i=0
 for /F "tokens=*" %%i in (Pathlist.txt) do (
 set fp=%%i
 set LIST=
 for /r "%fp%" %%a in (*.*) do set i=i+1
 set LIST=!LIST! ---%deptClient%---  %%~na  ----UPLOAD TIME----  %%~ta 
 )
 set LIST=%LIST:~1% 

 IF %i% NEQ 0 (wscript "%~dp0FTPFilesUploadedNotification.vbs")
 popd

Pathlist.txt

\\vavm\CINICO\Incoming
\\vavm\CIS\Incoming
\\vavm\Forcht\Incoming
\\vavm\HPC\Incoming
\\vavm\K\Incoming
\\vavm\MWEmpCC\Incoming
\\vavm\National Labor Benefits\Incoming
\\vavm\PeriSons\Incoming
\\vavm\US\Incoming
\\vavm\K\Incoming

FTPFilesNotification.vbs

dim outputArray
dim inputText 
dim message

inputText =  CreateObject("WScript.Shell").ExpandEnvironmentStrings("%LIST%")

outputArray = split(inputText, "   ")

for each x in outputArray
   message = message & x & vbCRLF
next


Set MyEmail=CreateObject("CDO.Message")

MyEmail.Subject="Clients Imported to System"
MyEmail.From="SYSTEMFUNCTION@mrsllc.org"
MyEmail.To="rickg@gmail.com"
MyEmail.TextBody= "The Following Clients have been imported to the system: " & vbCRLF & message

MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2


MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")="mail.org"


MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25 

MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")=1

MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername")="username" 

MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword")="password"

MyEmail.Configuration.Fields.Update
MyEmail.Send

set MyEmail=nothing

3 个答案:

答案 0 :(得分:1)

改变。不确定你想要做什么来发送线路(一次一个或完成后发送所有)。如果一个一个,整合到这个。如果一次结束,则将输出发送到临时文件并发送。

@echo off
pushd %~dp0
set /A Cnt=0
for /F "tokens=*" %%i in (Pathlist.txt) do (
   echo i= %%i
   for /f "usebackq tokens=*" %%a in (`Dir /s /b %%i\*.*`) do (
      echo %%~na - %%~ta
      set /A Cnt+=1
   )
) 
echo(Cnt=%Cnt%
pause
popd

答案 1 :(得分:1)

为了进行比较,在PowerShell中,它可能是:

$emailSettings = @{
    From = "you@example.org"
    To = "you@example.org"
    Subject = "Upload report"
    SmtpServer = "yourmailserver"
}

$report = dir -Path @(gc pathlist.txt) | select FullName, CreationTime
Send-MailMessage @emailSettings -Body "$($report|ConvertTo-Html)" -BodyAsHtml

答案 2 :(得分:1)

这显示了如何遍历vbscript中的文件

'On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Dirname = InputBox("Enter Dir name")
'Searchterm = Inputbox("Enter search term")
ProcessFolder DirName

Sub ProcessFolder(FolderPath)
    On Error Resume Next
    Set fldr = fso.GetFolder(FolderPath)

    Set Fls = fldr.files

    For Each thing in Fls
'        Set contents = thing.OpenAsTextStream
'        If err.number = 0 then
'            If Instr(contents.readall, searchterm) > 1 then msgbox thing.path
'        Else
'            err.clear
'        End If
         msgbox Thing.Name & " " & Thing.DateLastModified 



    Next

    Set fldrs = fldr.subfolders
    For Each thing in fldrs
        ProcessFolder thing.path
    Next

End Sub

发送邮件

Set emailObj      = CreateObject("CDO.Message")
emailObj.From     = "dc@gail.com"

emailObj.To       = "dc@gail.com"

emailObj.Subject  = "Test CDO"
emailObj.TextBody = "Test CDO"

Set emailConfig = emailObj.Configuration
msgbox emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") 
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing")    = 2  
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1  
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl")      = true 
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername")    = "YourUserName"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword")    = "Password1"
emailConfig.Fields.Update

emailObj.Send

If err.number = 0 then Msgbox "Done"

逐行读取文件

On Error Resume Next
Set Fso = CreateObject("Scripting.FileSystemObject")
Set File = Fso.CreateTextFile("C:\myfile.txt", True)
If err.number <> 0 then
    Wscript.Echo "Error: " & err.number & " " & err.description & " from " & err.source
    err.clear
    wscript.exit
End If
Do Until File.AtEndOfStream
    Msgbox File.readline
Loop