Search code examples
windowsbatch-filewindows-server-2003-r2

Log file for batch script


I'm a beginner in scripting, I just made a batch script to create some quotas on a server Win2003. I would like to have a log file as an output , but the only command I know is >>logfile.txt which will make a log file for each command.

I would like to have ONE logfile.txt that will log if all commands were successfully applied or not...

Can someone help please with the coding please?

@echo off
echo.
Dirquota quota add "J:\P1\BD OG" /Limit:60GB /Type:Hard /Status:Enable /Add-Threshold:80 /Add-Notification:80,M,e-mail-warning.txt
Dirquota quota add "J:\P1\BD Chair" /Limit:60GB /Type:Hard /Status:Enable /Add-Threshold:80 /Add-Notification:80,M,e-mail-warning.txt
Dirquota quota add "M:\P2\BD Arena" /Limit:50GB /Type:Hard /Status:Enable /Add-Threshold:80 /Add-Notification:80,M,e-mail-warning.txt
Dirquota quota add "K:\P3\BD Home" /Limit:30GB /Type:Hard /Status:Enable /Add-Threshold:80 /Add-Notification:80,M,e-mail-warning.txt
echo.
pause

Solution

  • @ECHO OFF
    SETLOCAL
    (
     FOR /f "tokens=1-4" %%a IN (q24521687.txt) DO (
      ECHO(Dirquota quota add "%%a:\%%b\BD %%c" /Limit:%%dGB /Type:Hard /Status:Enable /Add-Threshold:80 /Add-Notification:80,M,e-mail-warning.txt
      IF ERRORLEVEL 1 (ECHO failed) ELSE (ECHO succeeded)
     )
    )>newfile.txt
    
    GOTO :EOF
    

    This batch routine uses a file named q24521687.txt containing this data (derived from yours)

    J P1 OG 60
    J P1 Chair 60
    M P2 Arena 50
    K P3 Home 30
    

    Which may be easier to maintain.

    Produces newfile.txt - this could be any filename you choose. Use >> in place of > to append to an existing file (or create it if it doesn't exist) > will replace any existing file.

    Note that this will simply ECHO the dirquota command-line for checking (to the newfile.txt file). Delete the echo( to invoke the executable.

    I've assumed that dirquota uses the standard errorlevel setting - 0 for success, non-zero otherwise. Without your specifying what the termination condition for dirquota is, I would be guessing.


    Response to comments:

    There's no indication about BD - is it part of every name to be constructed in that position, or only some?

    The for /f parses the lines of the input file and assigns each token on the line to %%a..%%d (%%a is nominated, tokens 1-4 are selected, so each token on the input line is assigned to the next-alphabetical metavariable).

    The default separators are Space and comma, amongst others. Consequently, J P1 OG 60 for example would assign J to %%a, P1 to %%b, OG to %%c and 60 to %%d. Hence, the dirquota line would be constructed as required, assuming (for lack of data otherwise) that all directorynames are of the form BD something.

    With some small adjustments, this can be extended.

    Suppose you wanted directorynames BD OG, sausages and XY Table.

    Since the directory name may or may not contain a space, we could fix the routine by changing it a little:

    @ECHO OFF
    SETLOCAL
    (
     FOR /f "tokens=1-4DELIMS=," %%a IN (q24521687.txt) DO (
      ECHO(Dirquota quota add "%%a:\%%b\%%c" /Limit:%%dGB /Type:Hard /Status:Enable /Add-Threshold:80 /Add-Notification:80,M,e-mail-warning.txt
      IF ERRORLEVEL 1 (ECHO %%c failed) ELSE (ECHO %%c succeeded)
     )
    )>newfile.txt
    
    GOTO :EOF
    

    (note the changes : remove the BDSpace from the directory in the dirquota line and add the delims=, phrase into the for /f)

    And our data in the text file becomes

    J,P1,BD OG,60
    J,P1,sausages,60
    M,P2,XY Table,50
    K,P3,BD Home,30
    

    Simply each variable part separated by commas (the delims= character selected).

    Note the positioning of the quotes. These are designed to ensure that separators are correctly processed.

    As presented, since the dirquota command is simply echoed, not executed, errorlevel will be 0 so each line will be reported as having succeeded (I've added %%c to the succeeded/failed report). Once you've checked that the dirquota command-lines being reported are correct, simply change ECHO(Dirquota to Dirquota and the dirquota lines will be executed; Assuming dirquota is an executable and it sets errorlevel to the standard 0=success;otherwise failure then the if errorlevel line directly following the dirquota line should correctly display the result.