0

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

目前,此脚本执行以下操作: - 监视文件夹并查看其中是否有文件 - 如果有,则通过命令行执行 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
4

2 回答 2

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
于 2013-09-19T03:21:54.157 回答
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"
于 2013-09-18T15:39:47.987 回答