批处理文件如果声明

时间:2016-09-13 10:59:30

标签: batch-file

我已经收到了一份批处理文件,该文件已经放在一起,可以正常工作。但是,我认为编码有点难看,可以或者可能以更好的方式完成。

本质上,此文件每天通过一个计划任务运行,旨在查看具有学生帐户和信用信息的csv文件的特定目录。

如果发现文件被脚本处理,如果没有,则注意到没有找到文件并且脚本终止。

我正在努力做得更好',当发现一个csv IS时,我需要发现我们当前所处的那个月,然后行动依赖于此。因此,在这种情况下,如果月份变量返回01,02,03,04,05,06,07或08,那么将在文件处理中添加一个注释,以说明信用分配15/16和#34 ;

或者,如果月份是其他任何东西(IE 09,10,11,12),那么该文件将以相同的方式处理,但该注释将读取"信用分配16/17"

任何人都可以建议更好的编码结构,如果'下面的陈述?

@echo off

::Define Variables
SET server_command="c:\Program Files\PaperCut MF\server\bin\win\server-command.exe"
SET master="c:\credit_allocation\processed\master.csv"

SET month=%date:~3,-5%

SET year=%date:~8%
SET /a sum=%date:~8%+1
echo %sum% > next_year.txt
SET /p next_year=<next_year.txt
DEL next_year.txt

SET /a sum2=%date:~8%-1
echo %sum2% > last_year.txt
SET /p last_year=<last_year.txt
DEL last_year.txt


IF EXIST "c:\credit_allocation\student_credit.csv" GOTO Do_Stuff
echo. >> %master%
echo.No CSV Found at %time:~0,5% on %date% >> %master%
GOTO EOF

:Do_Stuff

::Add run date/time to master record
echo. >> %master%
echo.Process run at %time:~0,5% on %date% >> %master%


::Find out the month, IE- Jan to Aug then still previous student year, or Sept to Dec then next student year

IF /I "%month%"=="01" (
    GOTO Jan2Aug
)
IF /I "%month%"=="02" (
    GOTO Jan2Aug
)
IF /I "%month%"=="03" (
    GOTO Jan2Aug
)
IF /I "%month%"=="04" (
    GOTO Jan2Aug
)
IF /I "%month%"=="05" (
    GOTO Jan2Aug
)
IF /I "%month%"=="06" (
    GOTO Jan2Aug
)
IF /I "%month%"=="07" (
    GOTO Jan2Aug
)
IF /I "%month%"=="08" (
    GOTO Jan2Aug
)



::Sept to Dec - Loop through CSV file for username
for /f "tokens=1,2 delims=," %%a in (c:\credit_allocation\student_credit.csv) do (
%server_command% adjust-user-account-balance "%%a" "%%b" "Credit Allocation %year% /%next_year%"
)
GOTO Update_Master_Record


:Jan2Aug
::Jan to Aug - Loop through CSV file for username
for /f "tokens=1,2 delims=," %%a in (c:\credit_allocation\student_credit.csv) do (
%server_command% adjust-user-account-balance "%%a" "%%b" "Credit Allocation %last_year%/%year%"
)


:Update_Master_Record
::Take contents of CSV and append to a Master CSV record then delete original CSV file
cd c:\credit_allocation
forfiles /m *.csv /c "cmd /c echo. >> %master% && type @file >> %master% && del @file"

:EOF

1 个答案:

答案 0 :(得分:0)

这是一个快速未经测试的重写:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS

REM Define User Variables
(SET master=c:\credit_allocation\processed\master.csv)
(SET stucred=c:\credit_allocation\student_credit.csv)
(SET srv_cmd=%ProgramFiles%\PaperCut MF\server\bin\win\server-command.exe)
(SET option=adjust-user-account-balance)

IF NOT EXIST "%student%" ((ECHO=
    ECHO=No CSV Found at %TIME:~,5% on %DATE%)>>"%master%"
    GOTO :EOF)

SET "year=%DATE:~-2%"
SET "month=1%DATE:~-7,2%"
IF %month% GTR 108 (SET/A "adj_y=(2%year%-100)-1"
    CALL SET "adj_y=%%adj_y:~-2%%/%year%") ELSE (SET/A "adj_y=(1%year%-100)+1"
    CALL SET "adj_y=0%%adj_y%%"
    CALL SET "adj_y=%year%/%%adj_y:~-2%%")

REM Add run date/time to master record
(ECHO=
    ECHO=Process run at %TIME:~,5% on %DATE%)>>"%master%"

REM Loop through CSV file for username
FOR /F "TOKENS=1,2 DELIMS=," %%I IN (%stucred%) DO (
    "%srv_cmd%" %option% "%%~a" "%%~b" "Credit Allocation %adj_y%")

REM Propagate Master CSV with contents of student CSV and delete student CSV
TYPE "%stucred%">>"%master%" && DEL "%stucred%"

在确认其功能

之前,请不要对实际数据进行尝试