需要帮助修改脚本以使用单个文件,而不是一次性使用所有文件

时间:2013-09-18 14:06:35

标签: windows batch-file

我想知道是否有人可以帮助我对我继承的这个脚本进行非常简单的返工。我以前没有批处理文件的经验,需要以非常简单的方式修改这个已经工作的脚本。

目前,此脚本执行以下操作: - 监视文件夹并查看其中是否有文件 - 如果有,它通过命令行执行FTP传输 - 然后,它将文件移动到创建的存档文件夹中,并使用当前时间戳命名,并将某些内容写入文本日志文件 - 如果没有文件,则脚本退出并且不执行任何操作。

截至目前,脚本正在查找,处理和移动所有文件。我想看看我是否可以指向正确的方向,并提供有关如何修改脚本的帮助,以便它逐个执行每个文件,而不是在任何地方使用。最后,我认为我需要做的就是弄清楚如何正确地执行循环并读取/存储文件名以用作变量而不是使用任何帮助都将不胜感激。

@ECHO OFF

:: Set count of files = 0
SET X=0

:: Set timestamp for processed folders
set TIMESTAMP=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%-%TIME:~0,2%.%TIME:~3,2%.%TIME:~6,2%.%TIME:~9,2%

:: If more than 0 files exist begin ftp and file archival otherwise exit
FOR %%i IN (c:\Encoded_HL7_Vanderbilt\*.*) DO IF EXIST %%i (SET X=1) 
IF [%X%] gtr [0]  (cd\..\..

cd\"Program Files (x86)\Ipswitch\WS_FTP 12"
IF ERRORLEVEL 1 (echo %TIMESTAMP% Error switching to ftp program directory>E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.error.txt
exit)

wsftppro -s local:C:\Encoded_HL7_Vanderbilt\*.* -d Vandy!Vanderbilt:/incoming/ 
IF ERRORLEVEL 1 (echo %TIMESTAMP% Error transmitting file to ftp server>E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.error.txt
exit)

md "E:\Processed_HL7_Vanderbilt\%1\%TIMESTAMP%"
IF ERRORLEVEL 1 (echo %TIMESTAMP% Error creating archive directory>E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.error.txt
exit)


move "C:\Encoded_HL7_Vanderbilt\*.*" "E:\Processed_HL7_Vanderbilt%1\%TIMESTAMP%"
IF ERRORLEVEL 1 (echo %TIMESTAMP% Error moving files to archive directory>E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.error.txt
exit)) ELSE (exit)

echo %TIMESTAMP% File transfer completed successfully>E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.success.txt
exit

2 个答案:

答案 0 :(得分:1)

在公园周围散步:如果任何文件生成错误,则会停止处理更多文件。

@ECHO OFF

set "folder=%~1"

:: Set timestamp for processed folders
set TIMESTAMP=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%-%TIME:~0,2%.%TIME:~3,2%.%TIME:~6,2%.%TIME:~9,2%

:: If more than 0 files exist begin ftp and file archival otherwise exit
FOR %%a IN (c:\Encoded_HL7_Vanderbilt\*.*) DO (

cd\"Program Files (x86)\Ipswitch\WS_FTP 12"
IF ERRORLEVEL 1 (echo %TIMESTAMP% Error switching to ftp program directory>E:\HL7_Vanderbilt_log\%folder%\%TIMESTAMP%.error.txt
goto :done)

wsftppro -s local:%%a -d Vandy!Vanderbilt:/incoming/ 
IF ERRORLEVEL 1 (echo %TIMESTAMP% Error transmitting "%%a" file to ftp server>E:\HL7_Vanderbilt_log\%folder%\%TIMESTAMP%.error.txt
goto :done)

md "E:\Processed_HL7_Vanderbilt\%folder%\%TIMESTAMP%" 2>nul
IF ERRORLEVEL 1 (echo %TIMESTAMP% Error creating archive directory>E:\HL7_Vanderbilt_log\%folder%\%TIMESTAMP%.error.txt
goto :done)


move "%%a" "E:\Processed_HL7_Vanderbilt\%folder%\%TIMESTAMP%"
IF ERRORLEVEL 1 (echo %TIMESTAMP% Error moving "%%a" file to archive directory>E:\HL7_Vanderbilt_log\%folder%\%TIMESTAMP%.error.txt
goto :done)

)

echo %TIMESTAMP% File transfers completed successfully or no files were found>E:\HL7_Vanderbilt_log\%folder%\%TIMESTAMP%.success.txt
exit

:done 
goto :EOF

答案 1 :(得分:0)

试试这个,它可能适合你:

@ECHO OFF &SETLOCAL

:: Set count of files = 0
SET /a count=0

:: Set timestamp for processed folders
set "TIMESTAMP=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%-%TIME:~0,2%.%TIME:~3,2%.%TIME:~6,2%.%TIME:~9,2%"

cd "%ProgramFiles(x86)%\Ipswitch\WS_FTP 12"  || (
    echo %TIMESTAMP% Error switching to ftp program directory>"E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.error.txt"
    exit /b 1
)
md "E:\Processed_HL7_Vanderbilt\%1\%TIMESTAMP%" || (
    echo %TIMESTAMP% Error creating archive directory>"E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.error.txt"
    exit /b 1
)

:: If more than 0 files exist begin ftp and file archival otherwise exit
FOR %%i IN (c:\Encoded_HL7_Vanderbilt\*) DO (
    set /a count+=1

    REM I don't know the wsftppro command line parameters, pls. check the man page
    wsftppro -s local:"%%~i" -d Vandy!Vanderbilt:/incoming/ || (
        echo %TIMESTAMP% Error transmitting file to ftp server>"E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.error.txt"
        exit /b 1
    )
    move "%%~i" "E:\Processed_HL7_Vanderbilt\%1\%TIMESTAMP%" || (
        echo %TIMESTAMP% Error moving files to archive directory>"E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.error.txt"
        exit /b 1
    )
) 

if %count% equ 0 (
    rd "E:\Processed_HL7_Vanderbilt\%1\%TIMESTAMP%" 2>nul
    exit /b 1
)

echo %TIMESTAMP% File transfer completed successfully>"E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.success.txt"