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
This batch routine uses a file named
q24521687.txtcontaining this data (derived from yours)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
ECHOthedirquotacommand-line for checking (to thenewfile.txtfile). Delete theecho(to invoke the executable.I've assumed that
dirquotauses the standarderrorlevelsetting - 0 for success, non-zero otherwise. Without your specifying what the termination condition fordirquotais, 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 /fparses the lines of the input file and assigns each token on the line to%%a..%%d(%%ais 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 60for example would assignJto%%a,P1to%%b,OGto%%cand60to%%d. Hence, thedirquotaline would be constructed as required, assuming (for lack of data otherwise) that all directorynames are of the formBD something.With some small adjustments, this can be extended.
Suppose you wanted directorynames
BD OG,sausagesandXY Table.Since the directory name may or may not contain a space, we could fix the routine by changing it a little:
(note the changes : remove the BDSpace from the directory in the
dirquotaline and add thedelims=,phrase into thefor /f)And our data in the text file becomes
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
dirquotacommand is simplyechoed, not executed,errorlevelwill be0so each line will be reported as having succeeded (I've added%%cto the succeeded/failed report). Once you've checked that thedirquotacommand-lines being reported are correct, simply changeECHO(DirquotatoDirquotaand thedirquotalines will be executed; Assumingdirquotais an executable and it setserrorlevelto the standard0=success;otherwise failurethen theif errorlevelline directly following thedirquotaline should correctly display the result.