|
Answer» Currently I check the disk free space on several servers and day and each of them have several drives mounted. They are all MS Windows Server 2000 and up. My goal is to create a log file for the disk checking. I only have one server and one drive in the batch file while I work on this. Here is what I currently have:
IF EXIST "Z:\*.*" ( @NET USE Z: /DELETE ) @NET USE Z: \\NACET1SQL02\C$ ECHO "%DATE%","%TIME%","NACET1SQL02","C:" >> C:\RESULTS.TXT @DIR Z: | FIND "bytes free" >> C:\RESULTS.TXT @NET USE Z: /DELETE EXIT
This is the output contained in C:\RESULTS.TXT:
"Fri 01/07/2011","10:36:55.39","NACET1SQL02","C:" 16 Dir(s) 1,625,882,624 bytes free
Question - Is there any reasonably simple way to force the results onto one LINE? I can clean up the "16 Dir(s)" and "bytes free" easily enough with Excel. I would just like to put everything for a SINGLE drive onto one line. Any ideas? This one has me stumped. Microsoft Windows XP SP3 using CMD.EXE.This should do the trick:
Code: [Select]@echo off
if exist "z:\*.*" ( net use z: /delete ) net use z: \\nacet1sql02\c$
for /f "TOKENS=* delims=" %%i in ('dir z: ^| find /i "bytes free"') do ( echo "%date%","%time%","nacet1sql02","c:","%%i">> c:\results.txt )
net use z: /delete exit
I'm not a big fan of caps and you don't need to prefix commands with @ if you use @echo off.
Good luck. [EDIT] I was fooling with this offline while Sidewinder was posting. I too am not a big fan of USING CAPS in batch scripts and I agree about @ as well.
You want to see this?
"Fri 01/07/2011","10:36:55.39","NACET1SQL02","C:" 1,625,882,624
You can form the first part of the string thus
Code: [Select]set string1="%DATE%","%TIME%,"NACET1SQL02","C:" and then use FOR to get the line of DIR output that has the "bytes free" figure into another string
Code: [Select]or /f "delims=" %%A in ( ' DIR Z: ^| FIND "bytes free" ' ) do set string2=%%A and finally echo the two strings one after the other to the results file
Code: [Select]echo %string1%%string2% >> C:\RESULTS.TXT You seem to be making a csv output file, so...
Did you want quotes around the bytes free figure?
Did you want the commas (thousands separators) to be removed from that as well?
Code: [Select]@echo off set string1="%DATE%","%TIME%","NACET1SQL02","C:" for /f "delims=" %%A in ( ' DIR Z: ^| FIND "bytes free" ' ) do set string2=%%A for /f "tokens=1-3 delims= " %%A in ("%string2%") do set bytesfree=%%C set bytesfree=%bytesfree:,=% echo [1] %string1% %string2% echo [2] %string1%,"%bytesfree%" the output...
Code: [Select][1] "07/01/2011","18:12:30.95","NACET1SQL02","C:" 0 Dir(s) 6,238,208 bytes free [2] "07/01/2011","18:12:30.95","NACET1SQL02","C:","6238208" You will see that my local %DATE% format is different from yours but that does not AFFECT the operation of the script
In my post above I missed off the first character, (F) in the code line you see below
Quote from: Salmon Trout on January 07, 2011, 11:13:10 AM and then use FOR to get the line of DIR output that has the "bytes free" figure into another string
Code: [Select]For /f "delims=" %%A in ( ' DIR Z: ^| FIND "bytes free" ' ) do set string2=%%A
|