0

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
7
  • >>logfile.txt after each command will work. It appends to the file if it exists already. >logfile.txt (only one >) is the version that overwrites the file if it exists. Commented Jul 2, 2014 at 1:52
  • Thank you , but the thing is that I have more than 200 commands [I just put 4 on this post] , I don't want to have a log file for each command... I want to have 1 log file and within that file check if each command has been successfully applied or not.... Commented Jul 2, 2014 at 1:57
  • You will have one log file if you use >>logfile.txt. It will not create one file per command. If you don't want the entire output of all commands in that file, then it depends how dirquota signals that it failed. You'll have to check that somehow, and then log the result with echo Failed >> logfile.txt or echo Success >> logfile.txt. Commented Jul 2, 2014 at 2:01
  • Great tips! You are right... I will need the entire output. Where do I put >>logfile.txt on the script ? at the end of the first command? Commented Jul 2, 2014 at 2:22
  • 3
    Just execute the Batch file this way: theBatchFile.bat > logfile.txt Commented Jul 2, 2014 at 2:37

2 Answers 2

2
@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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Magoo , so... my understanding is that I need to put the path of the folders on q24521687.txt? Also I haven't put any error level setting. Could you please let me know where to put that ? Also , BD is part of the name of the folder , so will \BD %%c" be necessary ? or just \%%c"/Limit:%%dGB will do the trick ? Thanks in advance
2

As Aacini says in his comment, you can simply redirect output when you invoke your batch file. If your batch script is called "myScript.bat", then you can use myScript >myLog.txt 2>&1. If you want to append to an existing log file, then use myScript >>myLog.txt 2>&1.

The funny looking redirection at the end causes error messages on stderr to be redirected to the same location as stdin, which is the log file in this case. This is needed so that you can capture any error messages that DIRQUOTA may produce.

There is one problem with this approach - Your PAUSE output will also appear in the log file, and you will not see the prompt to press a key on the screen. You could solve this by obtaining a Windows port of the TEE utility that sends output to both the screen and a file.

Alternatively, you could modify your script as follows, and call it normally:

@echo off
call :main >myLog.txt 2>&1
pause
exit /b

:main
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.
exit /b

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.