Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Files count via batch file

Posted on 2009-12-21
9
Medium Priority
?
314 Views
Last Modified: 2012-05-08
I am not a coder but I think this bat file comes close to what I am trying to accomplish.  (Please see related solution)
I have a folder on a network drive named P:\Contractor Work and each contractor has folder in this main folder like so P:\Contractor Work\Brad Pitt
In each contractor folder they weekly dated folder for each Friday when files are turned in like so P:\Contractor Work 11-11-2009
I need to create a bat file to look into each contractor folder and list the total number of pdf files, xls files, and the total number of new files since the previous Friday.  Please let me know if I am correct in thinking that this bat file comes close.  Thank you in advance.

User              Total PDF Files       Total XLS Files   New Files  Total Files
Brad Pitt         2                                  5                        2               7
John Doe       5                                  7                        5              12
0
Comment
Question by:dfw_computer_geek
  • 5
  • 3
9 Comments
 
LVL 38

Accepted Solution

by:
Shift-3 earned 500 total points
ID: 26100095
Paste the script below into a text file with a .cmd extension.  Customize the value of the root variable on line 4 with the location of the folder containing the contractors' folders.  Running the script will write output to a comma-delimited text file.


@echo off
setlocal enabledelayedexpansion
 
set root=P:\Contractor Work
set report=report.csv
 
echo Total PDF Files,Total XLS Files,New Files,Total Files > "%report%"
 
for /f "tokens=*" %%G in ('dir "%root%" /a:d /b') do (
 set pdfcount=0
 set xlscount=0
 set totalcount=0
 set newcount=0
 
 for /f %%H in ('dir "%root%\%%G\*.pdf" /a:-d /b /s') do set /a pdfcount+=1
 for /f %%H in ('dir "%root%\%%G\*.xls" /a:-d /b /s') do set /a xlscount+=1
 for /f "tokens=*" %%H in ('dir "%root%\%%G" /a:d /b /o:d /t:c') do set newest=%%H
 for /f %%H in ('dir "%root%\%%G\!newest!" /a:-d /b /s') do set /a newcount+=1
 for /f %%H in ('dir "%root%\%%G" /a:-d /b /s') do set /a totalcount+=1
 
 echo "%%G",!pdfcount!,!xlscount!,!newcount!,!totalcount! >> "%report%"
)

Open in new window

0
 

Author Comment

by:dfw_computer_geek
ID: 26100505
I test this out and this works very well and gets me very close to the desired result and it works very fast and very well. The only issue I found is that the dated folders are created in advance.  So it looks to me like the code is looking at the newest folder for the files and those are considered the new files.  Is this correct?   I wonder if I should just have a prompt created for the current week to gather the new count.   Thank you in advance.
0
 

Author Comment

by:dfw_computer_geek
ID: 26100508
I test this out and this works very well and gets me very close to the desired result and it works very fast and very well. The only issue I found is that the dated folders are created in advance.  So it looks to me like the code is looking at the newest folder for the files and those are considered the new files.  Is this correct?   I wonder if I should just have a prompt created for the current week to gather the new count.   Thank you in advance.
0
Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

 
LVL 59

Expert Comment

by:Bill Prew
ID: 26100957
Before I can propose a solution I need better details.

(1) Can you confirm that the attached represents the directory and file hierarchy involved?

(2) For months and days that are less than 10, are they padded with 0 on the left to make 2 digits, or not?

(3) On any given run of the report, do you only want to count the files under the current weeks folder, or all files for that contractor?

(4) Are all week folders precreated into the future, or do they only get created week by week?

(5) Do you want the output formatted as comma delimited, or fixed width as your original sample seemed to indicate?

~bp
P:\Contractor Work
  Brad Pitt
    11-11-2009
      File11.pdf
      File12.pdf
      File13.xls
    11-18-2009
      File21.pdf
      File22.pdf
      File23.xls
    11-25-2009
      File31.pdf
      File32.pdf
      File33.xls
  John Doe
    11-11-2009
      File11.pdf
      File12.pdf
      File13.xls
    11-18-2009
      File21.pdf
      File22.pdf
      File23.xls
    11-25-2009
      File31.pdf
      File32.pdf
      File33.xls

Open in new window

0
 

Author Comment

by:dfw_computer_geek
ID: 26101274
1. this does represent the folder structure
2. the months and days are padded with zeroes
3. all files for that given contractor
4. all folders are precreated for the future (or at least I plan to all of them for next 2010)
5. csv or txt file is fine, it doesnt matter to me (which ever is easier to change in the future)
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26101860
Okay, this should be pretty close.

~bp
@echo off
setlocal EnableDelayedExpansion
setlocal EnableExtensions
 
REM Set variables
set BaseDir=P:\Contractor Work
set OutputFile=%CD%\report.csv
if exist "%OutputFile%" del "%OutputFile%"
 
REM Set today's date in YYYYMMDD format
set Today=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%
 
REM Process each user subdirectory
for /f %%U in ('dir /ad /b "%BaseDir%"') do (
 
  REM Find subfolder from last friday
  set LastFriday=00000000
  for /f %%A in ('dir /ad /b "%%~fU\*-*-*"') do (
    set DirName=%%A
    set Temp=!DirName:~6,4!!DirName:~0,2!!DirName:~3,2!
    if !Temp! GTR !LastFriday! if !Temp! LSS %Today% set LastFriday=!Temp!
  )
 
  REM Get file counts
  for /f "tokens=1 delims= " %%A in ('dir /s "%%~fU\*.pdf"^|find "File(s)"') do set CountPdf=%%A
  for /f "tokens=1 delims= " %%A in ('dir /s "%%~fU\*.xls"^|find "File(s)"') do set CountXls=%%A
  for /f "tokens=1 delims= " %%A in ('dir /s "%%~fU\*.*"^|find "File(s)"') do set CountAll=%%A
  set CountNew=0
  for /f "tokens=* delims=" %%A in ('dir /a-d /s /b "%%~fU\*.*"') do (
    set FileStamp=%%~tA
    set Temp=!FileStamp:~6,4!!FileStamp:~0,2!!FileStamp:~3,2!
    if !Temp! GTR !LastFriday! set /a CountNew+=1
  )
 
  REM Print this user data
  echo "%%U",!CountPdf!,!CountXls!,!CountNew!,!CountAll!>>"%OutputFile%"
)

Open in new window

0
 

Author Comment

by:dfw_computer_geek
ID: 26104698
billprew,

I saved this file as a .bat and changed the Contractor Work path to my test directory and ran the file and I get a message that says "The system cannot find the file specified." <- 10 times.

0
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 500 total points
ID: 26105194
Sorry about that, I was testing from the basedir and it was working okay.  Here is an adjusted version that should run okay for you.  Adjust the two paths in the SETs as needed, this was my test.

~bp
@echo off
setlocal EnableDelayedExpansion
setlocal EnableExtensions
 
REM Set variables
set BaseDir=c:\temp\EE24995267
set OutputFile=c:\temp\EE24995267\report.csv
if exist "%OutputFile%" del "%OutputFile%"
 
REM Set today's date in YYYYMMDD format
set Today=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%
 
REM Process each user subdirectory
for /f %%U in ('dir /ad /b "%BaseDir%"') do (
  REM Find subfolder from last friday
  set LastFriday=00000000
  for /f %%A in ('dir /ad /b "%BaseDir%\%%U\*-*-*"') do (
    set DirName=%%A
    set Temp=!DirName:~6,4!!DirName:~0,2!!DirName:~3,2!
    if !Temp! GTR !LastFriday! if !Temp! LSS %Today% set LastFriday=!Temp!
  )
 
  REM Get file counts
  for /f "tokens=1 delims= " %%A in ('dir /s "%BaseDir%\%%U\*.pdf"^|find "File(s)"') do set CountPdf=%%A
  for /f "tokens=1 delims= " %%A in ('dir /s "%BaseDir%\%%U\*.xls"^|find "File(s)"') do set CountXls=%%A
  for /f "tokens=1 delims= " %%A in ('dir /s "%BaseDir%\%%U\*.*"^|find "File(s)"') do set CountAll=%%A
  set CountNew=0
  for /f "tokens=* delims=" %%A in ('dir /a-d /s /b "%BaseDir%\%%U\*.*"') do (
    set FileStamp=%%~tA
    set Temp=!FileStamp:~6,4!!FileStamp:~0,2!!FileStamp:~3,2!
    if !Temp! GTR !LastFriday! set /a CountNew+=1
  )
 
  REM Print this user data
  echo "%%U",!CountPdf!,!CountXls!,!CountNew!,!CountAll!>>"%OutputFile%"
)

Open in new window

0
 

Author Comment

by:dfw_computer_geek
ID: 26106629
Shift,

How does the .cmd file decide what is considered a new file?

0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline
Suggested Courses

571 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