Windows scripting question

MrScott81

Golden Member
Aug 31, 2001
1,891
0
76
Is there a similar command line function that I can use in a script file similar to the read command in *nix?

For instance, in *nix I can do this to read the number of files in a directory:
/usr/bin/ls -1 | /usr/bin/wc -l | read numfiles

Is there anything I can do in windows to do the same thing? I know almost nothing about DOS/windows scripting.


 
Aug 1, 2004
77
0
0
In XP for a batch file, you can do this:
for /f %%a in ('dir /b ^| find /c /v ""') do set numfiles=%%a

And use %numfiles% to access that value
 

MrScott81

Golden Member
Aug 31, 2001
1,891
0
76
Wow that works great.....can you explain the syntax of this?

Also, I noticed that just the dir /b | find /c /v "" echo's out 35...is there a way to store commands response in a variable without doing that?
 
Aug 1, 2004
77
0
0
That whole line is the only way I know of in a batch file to store the output of a command into a variable. Sorry. You could try VBScript instead of batch files if you need more power.
 

yinan

Golden Member
Jan 12, 2007
1,801
2
71
vbscript to do the same thing

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace("C:\temp")

fileCount = 0
For Each objFile In objFolder.Items
fileCount = fileCount+1
Next

WScript.Echo "There were " & fileCount & " files in c:\temp"