Batch file 如何循环通过批处理脚本上的文本文档指定的文件夹

Batch file 如何循环通过批处理脚本上的文本文档指定的文件夹,batch-file,Batch File,我有一个文件夹路径列表,我想循环通过,获得文件和创建日期,然后发送电子邮件通知这些文件已上载到ftp。我已经让一切工作,但我有麻烦,通过循环文件夹,以获得文件。我认为它在文本文件中循环,但当我进入第二个循环时,我认为它只是在文本文档中的最后一个路径中循环,因为变量被覆盖了。我尝试将第二个for语句用括号括起来,第一个for语句用括号括起来,但没有成功。这是我的密码: scanFTPCLients.bat @echo off setlocal EnableDelayedExpansion c

我有一个文件夹路径列表,我想循环通过,获得文件和创建日期,然后发送电子邮件通知这些文件已上载到ftp。我已经让一切工作,但我有麻烦,通过循环文件夹,以获得文件。我认为它在文本文件中循环,但当我进入第二个循环时,我认为它只是在文本文档中的最后一个路径中循环,因为变量被覆盖了。我尝试将第二个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

变了。不确定发送线路时要做什么(一次发送一条或完成后全部发送)。如果一次只有一个,则将其整合到该系统中。如果在结束时同时执行所有操作,则将输出发送到临时文件并发送

@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

变了。不确定发送线路时要做什么(一次发送一条或完成后全部发送)。如果一次只有一个,则将其整合到该系统中。如果在结束时同时执行所有操作,则将输出发送到临时文件并发送

@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

相比之下,在PowerShell中,它可以是:

$emailSettings=@{
From=”you@example.org"
To=”you@example.org"
主题=“上传报告”
SmtpServer=“yourmailserver”
}
$report=dir-Path@(gc pathlist.txt)|选择全名、CreationTime
发送MailMessage@emailSettings-Body“$($report | ConvertTo Html)”-BodyAsHtml

相比之下,在PowerShell中,它可以是:

$emailSettings=@{
From=”you@example.org"
To=”you@example.org"
主题=“上传报告”
SmtpServer=“yourmailserver”
}
$report=dir-Path@(gc pathlist.txt)|选择全名、CreationTime
发送MailMessage@emailSettings-Body“$($report | ConvertTo Html)”-BodyAsHtml

这显示了如何在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
出错时继续下一步
设置Fso=CreateObject(“Scripting.FileSystemObject”)
Set File=Fso.CreateTextFile(“C:\myfile.txt”,True)
如果错误号为0,则
Wscript.Echo“Error:&err.number&&err.description&&from”&err.source
清楚
wscript.exit
如果结束
直到File.AtEndOfStream
Msgbox File.readline
环

这显示了如何在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
出错时继续下一步
设置Fso=CreateObject(“Scripting.FileSystemObject”)
Set File=Fso.CreateTextFile(“C:\myfile.txt”,True)
如果错误号为0,则
Wscript.Echo“Error:&err.number&&err.description&&from”&err.source
清楚
wscript.exit
如果结束
直到File.AtEndOfStream
Msgbox File.readline
环


不幸的是,我尝试了这个方法,但它只是打开和关闭了cmd框,没有做任何事情,我应该抓到它。不要使用path,因为它是一个环境变量。将所有出现的path更改为MyPath或类似的内容。不知道你在用脚本的后半部分做什么。。。但我得到了回音。还要验证Pathlist.txt中是否包含有效数据。如果我从文本文档中复制一行并在for语句中输入变量,我得到的完整路径太长,我不知道它有多长,代码运行良好,我们不想使用名为Path的Windows环境变量,我们也不需要它。请参阅上面我更改的答案。删除路径并使用%%I在FOR循环中指定完整路径。您提供的编辑代码不起任何作用。。。当我从第一个for循环中删除第二个for循环时,我的代码可以工作,但是它只从最后一个路径获取文件,我知道第二个循环必须在第一个循环中,但是当我删除第二个for循环时,我获取的文件路径(空白)太长。我不明白它有多长,因为如果我直接从文本文件中选择路径并放置,而不是变量,它就会工作。不幸的是,我尝试了这个方法,它只是打开和关闭了cmd框,没有任何作用。我应该捕捉到它。不要使用path,因为它是一个环境变量。将所有出现的path更改为MyPath或类似的内容。不知道你在用脚本的后半部分做什么。。。但我得到了回音。还要验证Pathlist.txt中是否包含有效数据。如果我从文本文档中复制一行并在for语句中输入变量,我得到的完整路径太长,我不知道它有多长,代码运行良好,我们不想使用名为Path的Windows环境变量,我们也不需要它。请参阅上面我更改的答案。删除路径并使用%%I在FOR循环中指定完整路径。您提供的编辑代码不起任何作用。。。当我从第一个for循环中删除第二个for循环时,我的代码可以工作,但是它只从最后一个路径获取文件,我知道第二个循环必须在第一个循环中,但是当我删除第二个for循环时,我获取的文件路径(空白)太长。我不明白它有多长,因为如果我直接从文本文件中获取路径并放置,而不是它的变量。您需要将第二个
for
循环括在括号中,但您还需要使用
!路径以使延迟的扩展正常工作。您需要将
set i=i+1
更改为类似
set/ai=!我+1
。您可能需要将最后的%s列表%s更改为!名单!-但可能不是。我建议不要使用变量名
PATH
,因为这是保留的--请参阅
PATH
命令…@TessellatingHeckler,最后一个
%LIST%
未在同一代码块中设置和读取,因此
!名单
不是必需的(虽然它不会造成伤害)…我以前尝试过将第二个for循环包含在第一个for循环中,但我得到的错误是的完整路径太长。。。。。我已经编辑了这个问题,包括了我所有的文件。我已经将“I”设置为一个计数器,让我知道