Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Batch – Programming .
- Scenario: Counting Files and adding them to logfiles
- How to count files in a directory and how to put them in logfiles.
- Sometimes it is helpful to know how many files and what files you have saved in a directory. You could count them manually or use a tool of some sort, but it is also realizable with a batch file. A batch for this under Windows NT could look somehting like the following:
- @echo off
- Break off
- REM ======= Part 1 ========
- echo First part of this batch (Listing and counting files in G:\Documents)
- echo.
- set dir="G:\Documents\"
- set log="G:\Logfiles-documents\log-txt-file.txt"
- set log2="G:\Logfiles-documents\log-file-count.txt"
- echo Put files (txt) into %log%
- for /R %dir% %%f in (*.txt*) do (
- dir "%%f" /b/s/a-d/w/OD/OS >> %log%
- )
- for /R %dir% %%f in (*.txt*) do (
- echo Numbers of .txt files in %dir%: >> %log2%
- dir "%%f" /b/s/a-d |find /v /c "::" >> %log2%
- )
- set log3="G:\Logfiles-documents\log-doc-odt-file.txt"
- echo Put files (odt/doc) into %log3%
- for /R %dir% %%f in (*.doc *.odt) do (
- dir "%%f" /b/s/a-d/w/OD/OS >> %log3%
- )
- for /R %dir% %%f in (*.odt *.doc) do (
- echo Numbers of .odt-files in %dir%: > %log2%
- dir G:\Documents\*.odt /b/s/a-d |find /v /c "::" > %log2%
- echo Numbers of .doc-files in %dir%: > %log2%
- dir G:\Documents\*.doc /b/s/a-d |find /v /c "::" > %log2%
- )
- echo Show all subdirectories of %dir%
- dir %dir% /a:d/s > G:\Logfiles-documents\count-directories.txt
- dir %dir% /a:d
- pause >nul
- What this batch code does?
- The batch counts all *.txt files and puts their number in one logfile. The name of every *.txt file is saved in another logfile. It also counts all .odt and .doc files and lists them in another logfile. And then last it lists all subdirectories of G:\Documents and puts them in a logfile.
- Notes for this batch:
- Make sure to use dir "%%f" and not just %%f.
- Use for /R to include all subdirectories.
- >> adds the information to the logfile instead of creating a new one. You can also use >.
- But I prefer the >> notation.
- The structure of this code can vary. The notation above is just an example but I think this is the most convenient way to write a batch like this.
- The counting of these files like the odt and doc files can of course be applied to every sort of filetype. So the batch is just a general example of how to count files and create logs.
Advertisement
Add Comment
Please, Sign In to add comment