Solved

bat file to scan sub directories to check for set of files

Posted on 2014-11-20
4
223 Views
Last Modified: 2014-12-03
Hi,
I am trying to search all subdirectories in a folder C:\Stage, for a set of files.
File names differ based on the source they come form, for example, load1.csv, load 14.csv, load 21.csv.
So I need to look for files based on load*.csv, import*.csv, etc.

I got the below for QLemo, but need to modify the hardcoded file names to wldcard ones.

Thanks!


@echo off
set reqfiles=load1.csv load2.csv load3.csv
set numfiles=3
pushd .
for /D %%D in (C:\Stage) do (
  cd /D %%D
  for /F %%C in ('dir %reqfiles" /b ^| find /c /v ""') do if %%C equ %numfile% echo %%D is ok
)
popd
0
Comment
Question by:chetan1981
  • 2
4 Comments
 

Author Comment

by:chetan1981
ID: 40455047
Also just to add, there Directories are on a UNC path, dong a pushd to a UNC path errors out, any workarouund.
Really appreciate any help I can get, I have no expertise in DOS.

Thanks.
0
 
LVL 25

Accepted Solution

by:
SStory earned 250 total points
ID: 40455146
Using cd with UNC will cause errors -not supported.
However using pushd instead of cd will create a temporary virtual drive to that UNC.

Example:
rem Creates a temporary drive letter if UNC and changes to that drive
pushd %%D
  for /F %%C in ('dir %reqfiles" /b ^| find /c /v ""') do if %%C equ %numfile% echo %%D is ok
rem pops back to location before the push
popd

This will solve your UNC issue.
I don't know what you're end goal is. Just listing files?

if so why not
dir /b /s load*.csv;dir /b /s import*.csv?
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 40456532
What do you want to do when you find the files?

~bp
0
 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 40456556
Okay, not knowing exactly what you are after, here's a thought.  This approach assumes you want to have at least one file in each file group, but we can adjust if needed for different results desired.

@echo off
setlocal

REM Define folder and files to search
set BaseDir=\\server\share\dir1
set FindList="load*.csv","import*.csv"

REM Initialize count of files searched for, and files found
set FindCount=0
set FoundCount=0

REM Loop through each file group
for %%A in (%FileList%) do (
  REM Count how many groups searched
  set /a FindCount+=1
  
  REM Count the number of matching files in this group
  for /f %B in ('dir "%BaseDir%\%%~A" /a-d /b /s ^| find /v "" /c') do (
    REM If we found any files, add to count of groups with files in them
    if %%B GTR 0 (
      set /a FoundCount+=1
    )
  )
)

REM Compare count of groups searched and count of groups with files found in them
if %FoundCount% EQU %FindCount% (
  echo Found at least one file in all groups searched
) else (
  echo Missing files for at least one group searched
)

Open in new window

~bp
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question