Solved

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

Posted on 2014-11-20
4
220 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 53

Expert Comment

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

~bp
0
 
LVL 53

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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