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.txt
containing 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
ECHO
thedirquota
command-line for checking (to thenewfile.txt
file). Delete theecho(
to invoke the executable.I've assumed that
dirquota
uses the standarderrorlevel
setting - 0 for success, non-zero otherwise. Without your specifying what the termination condition fordirquota
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 assignJ
to%%a
,P1
to%%b
,OG
to%%c
and60
to%%d
. Hence, thedirquota
line 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
,sausages
andXY 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
dirquota
line 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
dirquota
command is simplyecho
ed, not executed,errorlevel
will be0
so each line will be reported as having succeeded (I've added%%c
to the succeeded/failed report). Once you've checked that thedirquota
command-lines being reported are correct, simply changeECHO(Dirquota
toDirquota
and thedirquota
lines will be executed; Assumingdirquota
is an executable and it setserrorlevel
to the standard0=success;otherwise failure
then theif errorlevel
line directly following thedirquota
line should correctly display the result.