Help with Dos For/F command and sub folders

D

Dazza

Hi Guys

Its one of those days when you say yes that can be done. Only to find
out you start wishing you had said no!

I need to inspect each .txt file in each subfolder and count the
number of commas are in it and then list them

i know i am missing something and must be my age

@echo off
setlocal enabledelayedexpansion

for /F "usebackq" %%a IN ('*.txt') Do find /c /i "," %%a >>c:
\commareport.txt

This works fine for the folder is is in but i need to do all
subfolders

I have tried calling this bat from another bat but with no results.

Can anyone give me a hand,

Cheers
Darren
 
P

Pegasus [MVP]

Dazza said:
Hi Guys

Its one of those days when you say yes that can be done. Only to find
out you start wishing you had said no!

I need to inspect each .txt file in each subfolder and count the
number of commas are in it and then list them

i know i am missing something and must be my age

@echo off
setlocal enabledelayedexpansion

for /F "usebackq" %%a IN ('*.txt') Do find /c /i "," %%a >>c:
\commareport.txt

This works fine for the folder is is in but i need to do all
subfolders

I have tried calling this bat from another bat but with no results.

Can anyone give me a hand,

Cheers
Darren

Here you go:
@echo off
for /F %%a in ('dir *.txt /b /s') do (
for /F %%c in ('type "%%a" ^| find /c ","') do echo %%c - %%a
)
 
D

Dazza

Here you go:
@echo off
for /F %%a in ('dir *.txt /b /s') do (
  for /F %%c in ('type "%%a" ^| find /c ","') do echo %%c - %%a
)- Hide quoted text -

- Show quoted text -

Many Thanks for the quick reply, the only problem i have now is that
the are spaces in the folder names, legacy dont you love it....

Darren
 
D

Dazza

Many Thanks for the quick reply, the only problem i have now is that
the are spaces in the folder names, legacy dont you love it....

Darren- Hide quoted text -

- Show quoted text -



Any Body got any ideas?????
 
J

John John - MVP

Dazza said:
Any Body got any ideas?????

Hang tight... you're being helped by one of the best and he won't
abandon you halfway through the job. His last post was less than 27
minutes ago, he is active in several groups, give him time to come back
and read your other post!
 
P

Pegasus [MVP]

John John - MVP said:
Hang tight... you're being helped by one of the best and he won't abandon
you halfway through the job. His last post was less than 27 minutes ago,
he is active in several groups, give him time to come back and read your
other post!

To John: Thanks for the compliment, which is not really deserved. And yes, I
sometimes do have some other commitments.

To the OP: Sorry for the oversight. Here you go again:
@echo off
for /F "delims=" %%a in ('dir *.txt /b /s') do (
for /F %%c in ('type "%%a" ^| find /c ","') do echo %%c - %%a
)
 
D

Dazza

To John: Thanks for the compliment, which is not really deserved. And yes, I
sometimes do have some other commitments.

To the OP: Sorry for the oversight. Here you go again:
@echo off
for /F "delims=" %%a in ('dir *.txt /b /s') do (
  for /F %%c in ('type "%%a" ^| find /c ","') do echo %%c - %%a
)- Hide quoted text -

- Show quoted text -

Hi Thanks for the quick update.

Sorry I did not get to you but i have been on a few days vacation.

This now works a treat, but like all requests they change the goal
posts.

I have now been asked to add the number of lines in each text file
also

I am on the catch up with being off

Regards
Darren
 
P

Pegasus [MVP]

Dazza said:
Hi Thanks for the quick update.

Sorry I did not get to you but i have been on a few days vacation.

This now works a treat, but like all requests they change the goal
posts.

I have now been asked to add the number of lines in each text file
also

I am on the catch up with being off

Regards
Darren

Using a batch file to count the number of lines in a text file is
notoriously hard because batch files have the unfortunate habit of ignoring
blank lines when scanning text files. There is also the perennial issue of
poison characters such as ()"`& that have a tendency to make the batch file
unstable. The solution below uses a hybrid batch/VBS solution and is far
more robust than a pure batch file. Since your specification is rather
vague, you will probably have to tune it to deliver the results you expect.
Or, in other words, you must nail down your requirements *before* you start
coding.

@echo off
set sFolder=d:\User Data
set Scr="%temp%\TempVBS.vbs"
set VB=echo^>^>%Scr%
cd 1>nul 2>%Scr%
%VB% '------------------------------------------
%VB% 'Recursively count the number of .txt files
%VB% 'and report the number of lines and commas
%VB% '29.3.2010 FNL
%VB% '------------------------------------------
%VB% Set oFSO = CreateObject("Scripting.FileSystemObject")
%VB% Set oFolder = oFSO.GetFolder("%sFolder%")
%VB% iFileCount = 0
%VB% iLineCount = 0
%VB% iCommaCount = 0
%VB% ProcessFolder(oFolder)
%VB% WScript.Echo iFileCount, iLineCount, iCommaCount
%VB% '----------------------------
%VB% 'Process a folder recursively
%VB% '----------------------------
%VB% Sub ProcessFolder(oFldr)
%VB% For Each oFile In oFldr.Files
%VB% If lcase(Right(oFile.Name, 4)) = ".txt" then Count oFile
%VB% Next
%VB% For Each oSubFolder In oFldr.Subfolders
%VB% ProcessFolder oSubFolder
%VB% Next
%VB% End Sub
%VB% '---------------------
%VB% 'Process a single file
%VB% '---------------------
%VB% Sub Count(oFile)
%VB% WScript.Echo "Processing", oFile.path
%VB% iFileCount = iFileCount + 1
%VB% if oFile.size = 0 then Exit Sub
%VB% Set oData = oFSO.OpenTextFile(oFile.path)
%VB% sString = oData.ReadAll
%VB% For i = 1 To Len(sString)
%VB% char = Mid(sString, i, 1)
%VB% if char = "," Then iCommaCount = iCommaCount + 1
%VB% If char = vbLf then iLineCount = iLineCount + 1
%VB% Next
%VB% oData.close
%VB% End Sub
for /F "tokens=1-10" %%a in ('cscript //nologo %Scr%') do set Files=%%a& set
Lines=%%b& set Commas=%%c
del %Scr%
echo %Files% files, %Lines% lines, %Commas% commas
 
D

Dazza

Using a batch file to count the number of lines in a text file is
notoriously hard because batch files have the unfortunate habit of ignoring
blank lines when scanning text files. There is also the perennial issue of
poison characters such as ()"`& that have a tendency to make the batch file
unstable. The solution below uses a hybrid batch/VBS solution and is far
more robust than a pure batch file. Since your specification is rather
vague, you will probably have to tune it to deliver the results you expect.
Or, in other words, you must nail down your requirements *before* you start
coding.

@echo off
set sFolder=d:\User Data
set Scr="%temp%\TempVBS.vbs"
set VB=echo^>^>%Scr%
cd 1>nul 2>%Scr%
%VB% '------------------------------------------
%VB% 'Recursively count the number of .txt files
%VB% 'and report the number of lines and commas
%VB% '29.3.2010 FNL
%VB% '------------------------------------------
%VB% Set oFSO = CreateObject("Scripting.FileSystemObject")
%VB% Set oFolder = oFSO.GetFolder("%sFolder%")
%VB% iFileCount  = 0
%VB% iLineCount  = 0
%VB% iCommaCount = 0
%VB% ProcessFolder(oFolder)
%VB% WScript.Echo iFileCount, iLineCount, iCommaCount
%VB% '----------------------------
%VB% 'Process a folder recursively
%VB% '----------------------------
%VB% Sub ProcessFolder(oFldr)
%VB%   For Each oFile In oFldr.Files
%VB%     If lcase(Right(oFile.Name, 4)) = ".txt" then Count oFile
%VB%   Next
%VB%   For Each oSubFolder In oFldr.Subfolders
%VB%     ProcessFolder oSubFolder
%VB%   Next
%VB% End Sub
%VB% '---------------------
%VB% 'Process a single file
%VB% '---------------------
%VB% Sub Count(oFile)
%VB%   WScript.Echo "Processing", oFile.path
%VB%   iFileCount = iFileCount + 1
%VB%   if oFile.size = 0 then Exit Sub
%VB%   Set oData = oFSO.OpenTextFile(oFile.path)
%VB%   sString = oData.ReadAll
%VB%   For i = 1 To Len(sString)
%VB%     char = Mid(sString, i, 1)
%VB%     if char = ","  Then iCommaCount = iCommaCount + 1
%VB%     If char = vbLf then iLineCount = iLineCount + 1
%VB%   Next
%VB%   oData.close
%VB% End Sub
for /F "tokens=1-10" %%a in ('cscript //nologo %Scr%') do set Files=%%a& set
Lines=%%b& set Commas=%%c
del %Scr%
echo %Files% files, %Lines% lines, %Commas% commas- Hide quoted text -

- Show quoted text -

Hi Thanks again

I am sorry if i am not descripbing what i am needing to do very
clearly. I will try to explain

I have a folder with multiple subfolders. That contain a number
of .txt files. These file are text files of production serial numbers.
They are in a number of legacy formats.

However each serial number is either on its own line, or on its own
line followed by a commas or multiple numbers on one line but each
followed by a comma.

I need to be able to document the number of serial numbers is each
file. Your first bat file worked for 75% of the files but does not
work for the ones with no commas.

What i would like is to display the following

Lines Commas Path
10000 10000 /abc/def/ghi.txt
500 1500 /qwe/rty/uio.txt
50000 0 /qwe/rty/lmk/hij.txt

With these results i can identify the files that need a manual check.

I hope this is a little clearer

and i do appreciate your help

Darren
 
D

Dazza

Hi Thanks again

I am sorry if i am not descripbing what i am needing to do very
clearly. I will try to explain

I have a folder with multiple subfolders. That contain a number
of .txt files. These file are text files of production serial numbers.
They are in a number of legacy formats.

However each serial number is either on its own line, or on its own
line followed by a commas or multiple numbers on one line but each
followed by a comma.

I need to be able to document the number of serial numbers is each
file. Your first bat file worked for 75% of the files but does not
work for the ones with no commas.

What i would like is to display the following

Lines            Commas              Path
10000           10000                  /abc/def/ghi.txt
500                1500                  /qwe/rty/uio.txt
50000                  0                  /qwe/rty/lmk/hij.txt

With these results i can identify the files that need a manual check.

I hope this is a little clearer

and i do appreciate your help

Darren- Hide quoted text -

- Show quoted text -

I must be doing something wrong, i am trying the above script and it
does not seem to be echoing anything to screen.

After it has finished in i manually echo the end variable i get a
result. but these seem to be a full total
 
D

Dazza

I must be doing something wrong, i am trying the above script and it
does not seem to be echoing anything to screen.

After it has finished in i manually echo the end variable i get a
result. but these seem to be a full total- Hide quoted text -

- Show quoted text -

HI Pegasus

I have had no joy so far, but i have a solution if i can get it
together and could you help

For the commas this works

@echo off
for /F "delims=" %%a in ('dir *.txt /b /s') do (
for /F %%c in ('type "%%a" ^| find /c ","') do echo %%c # %%a
)

If i modify it i can get the same to count the lines

@echo off
for /F "delims=" %%a in ('dir *.txt /b /s') do (
for /F %%c in ('type "%%a" ^| find /v /c "%junk%"') do echo %%c # %
%a
)

I am struggling to get both find commands into the same for/f loop. i
am trying

@echo off
for /F "delims=" %%a in ('dir *.txt /b /s') do (
for /F %%c in ('type "%%a" ^| find /c ","') do (
for /F %%l in ('type "%%a" ^| find /v /c "%junk%"') do echo %%l # %
%c # %%a
)


Anybody got any ideas
 
P

Pegasus [MVP]

Dazza said:
On 29 Mar, 12:45, "Pegasus [MVP]" <[email protected]> wrote:
Hi Thanks again

I am sorry if i am not descripbing what i am needing to do very
clearly. I will try to explain

I have a folder with multiple subfolders. That contain a number
of .txt files. These file are text files of production serial numbers.
They are in a number of legacy formats.

However each serial number is either on its own line, or on its own
line followed by a commas or multiple numbers on one line but each
followed by a comma.

I need to be able to document the number of serial numbers is each
file. Your first bat file worked for 75% of the files but does not
work for the ones with no commas.

What i would like is to display the following

Lines Commas Path
10000 10000 /abc/def/ghi.txt
500 1500 /qwe/rty/uio.txt
50000 0 /qwe/rty/lmk/hij.txt

With these results i can identify the files that need a manual check.

I hope this is a little clearer

and i do appreciate your help

Darren

Try this batch file. It generates the results you expect but I'm not fond of
it for two reasons:
- It will be slow for large files and also for large numbers of files
- It is likely to get derailed by poison characters either in the file names
or in the file contents.
@echo off
SetLocal EnableDelayedExpansion
set Folder=d:\Test
echo Lines Commas Path
for /F "delims=" %%a in ('dir "%Folder%\*.txt" /b /s') do (
set C=0
set L=0
for /F %%c in ('type "%%a" ^| find /c ","') do set C=%%c
for /F %%L in ('type "%%a"') do set /a L=!L!+1
set C= !C!
set L= !L!
echo !L:~-6! !C:~-5! %%a
)
 
P

Pegasus [MVP]

Dazza said:
On 30 Mar, 13:12, Dazza <[email protected]> wrote:
HI Pegasus

I have had no joy so far, but i have a solution if i can get it
together and could you help

For the commas this works

@echo off
for /F "delims=" %%a in ('dir *.txt /b /s') do (
for /F %%c in ('type "%%a" ^| find /c ","') do echo %%c # %%a
)

If i modify it i can get the same to count the lines

@echo off
for /F "delims=" %%a in ('dir *.txt /b /s') do (
for /F %%c in ('type "%%a" ^| find /v /c "%junk%"') do echo %%c # %%a
)

I am struggling to get both find commands into the same for/f loop. i
am trying

@echo off
for /F "delims=" %%a in ('dir *.txt /b /s') do (
for /F %%c in ('type "%%a" ^| find /c ","') do (
for /F %%l in ('type "%%a" ^| find /v /c "%junk%"') do echo %%l # %%c #
%%a
)


Anybody got any ideas

You need to scan each file twice: Once to count the commas and once to count
the lines. Your code uses a nested loop which causes the files to be scanned
L squared times (L = number of lines in each file).
 
P

Pegasus [MVP]

Dazza said:
I must be doing something wrong, i am trying the above script and it
does not seem to be echoing anything to screen.

After it has finished in i manually echo the end variable i get a
result. but these seem to be a full total

This is the way I designed it, based on your somewhat vague specification.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top