Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Software Development
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory modem monitor motherboard network printer problem ram registry router security slow software sound toshiba trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
Batch file --> text list production help

Reply  
Thread Tools
thermal's Avatar
Junior Member with 5 posts.
 
Join Date: Nov 2009
Experience: Intermediate
13-Nov-2009, 09:23 AM #1
Batch file --> text list production help
Dear all,

I am currently running a batch file that examines a backup folder that is added to at the end of each previous day. Currently the batch file uses 'findstr' to examine the original text file (created using the 'dir' command) and filters out the previous days dates to a new text file.
However, at present, the final text file starts with the names of all folders within the backup folder, then the names of folders within these folders, then files within each of these folders:

Backup_folder --> folder_1 --> folder_A --> file_1A
Backup_folder --> folder_2 --> folder_B --> file_2B

Producing a text list =
folder_1
folder_2 etc
folder_A
folder_B etc
file_1A
file_2B etc

However, ideally what I would like is to have the folder names of folders within the backup folder and then the number of folders and number of files within each of these folders directly after each folder name . . .

Thanks in advance for any help!
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
13-Nov-2009, 09:52 AM #2
Welcome to TSG!

It would help if you can post the code you are using, so we can see exactly what files are being generated, and exactly how you are filtering the output.
Be sure to remove any personal info such as user names if they are revealing (replace them with Username for example, or username1, username2, etc).

From the description, I would expect the "new text file" created by findstr to contain no folder names at all, and you haven't described how the "final text file" is created.
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

thermal's Avatar
Junior Member with 5 posts.
 
Join Date: Nov 2009
Experience: Intermediate
13-Nov-2009, 10:03 AM #3
Hi, please find the current code as requested. At present all is being produced is a final list containing a list of folders followed by filename.doc list that is within each folder. What I ideally would like, but dont know how to do, is to have the name of the folder within the backup folder, but then a count of the docs inside each of these folders and so that each number comes after each folder name rather than all the folder names followed by all files names which is currently what is happening . . . ____________________________________________________
@echo off
dir "E:\BACKUP\data" /O /s > E:\BACKUP\del1.txt

echo wscript.echo ^(Date^(^)- 1^)>yesterday.vbs
for /f %%a in ('cscript //nologo yesterday.vbs') do set ydate1=%%a
del yesterday.vbs
set ydate1=%ydate1:/=%
set d=%ydate1:~0,2%
set m=%ydate1:~2,2%
set y=%ydate1:~4,4%
set ydate2=%d%/%m%/%y%

echo Good morning, here is backup 2 yesterdays (%ydate2%) list. . . > E:\BACKUP\list.txt
findstr "%ydate2%" E:\BACKUP\del1.txt > E:\BACKUP\list.txt
echo y | del E:\BACKUP\del1.txt
_____________________________________________________________

Thanks
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
13-Nov-2009, 11:01 AM #4
OK, this will list the folders and the count of files. It will not list folders that do not have changed files though it's easy to have it do so, i.e. E:\Backup\Data\Folder 5 -- Number of files 0
It will not list files in the E:\Backup\Data folder, let me know if you need those listed as well.
Would be easy to have it just list the file names, rather than the date, time, and size, or any combo of that info
Try this out and see how it works.
Code:
@Echo Off
Setlocal EnableDelayedExpansion
Set _Src=E:\Backup\Data
Set _OutPut=%_Src%\list.txt
Set _tmp=%_Src%\del1.txt
Echo Wscript.Echo ^(Date^(^)- 1^)>yesterday.vbs
For /F %%a In ('cscript //nologo yesterday.vbs') Do Set ydate1=%%a
Del yesterday.vbs
Set ydate1=%ydate1:/=%
Set d=%ydate1:~0,2%
Set m=%ydate1:~2,2%
Set y=%ydate1:~4,4%
Set ydate2=%d%/%m%/%y%
>"%_OutPut%" Echo Good morning, here is backup 2 yesterdays (%ydate2%) list. . .
For /F "Tokens=*" %%I In ('Dir "%_Src%" /AD /B /ON /S') Do (
>>"%_OutPut%" Echo.
If Exist "%_tmp%" Del "%_tmp%"
PushD "%%I"
Set _Count=0
For /F "tokens=*" %%A In ('Dir /A-D /OD^|Findstr "%ydate2%"') Do (
>>"%_tmp%" Echo %%A
Set /A _Count+=1
)
PopD
If !_Count! GTR 0 (
>>"%_OutPut%" Echo %%I -- Number of Files !_Count!
>>"%_OutPut%" Type "%_tmp%"
))
If Exist "%_tmp%" Del "%_tmp%"
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

thermal's Avatar
Junior Member with 5 posts.
 
Join Date: Nov 2009
Experience: Intermediate
13-Nov-2009, 02:39 PM #5
Thanks TheOutcaste, that works really well. I do have another question though . . . if there were folders in the second level, such as folder_2 (E:\backup\folder_1\folder_2) how can I get the program to count the number of folders at this level, (or any level for that matter, which I could change in the code should I need to) so that it produces something like below, and then includes only number of files within folder_2 . . .

E:\backup\folder1 -- Number of folders = 1
13/11/2009 ??:?? folder_2 -- number of files = ??

Thanks for this!

Also, out of interest, what do the PushD and PopD commands do?
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
13-Nov-2009, 06:09 PM #6
So you only want to list the number of 2nd level folders, but not 1st or 3rd? And list number of files for all folders?
Would E:\Backup be the 1st level, or E:\Backup\Folder_X?

You can list Number of folders for each folder easily, but trying to determine the Level of the current folder would complicate things. And sounds like you want to list them in "Tree Order", rather than "Level Order"

The Dir command lists folders in "Level order"; it first lists all the top level folders, then all the 2nd level folders, then all the 3rd level folders, etc, like this:
Code:
    Parent  1st Level 2nd Level 3rd Level
C:\Test Dir\Folder 1
C:\Test Dir\Folder 2
C:\Test Dir\Folder 3
C:\Test Dir\Folder 4
C:\Test Dir\Folder 1\Fldr1Sub1
C:\Test Dir\Folder 1\Fldr1Sub2
C:\Test Dir\Folder 1\Fldr1Sub1\Fldr1SubSub1
C:\Test Dir\Folder 2\Fldr2Sub1
C:\Test Dir\Folder 2\Fldr2Sub1\Fldr2SubSub1
If you want to list them in "Tree order", that gets even more complicated.
By "Tree order" I mean listing them in this order:
Code:
C:\TEST DIR
├───Folder 1
│   ├───Fldr1Sub1
│   │   └───Fldr1SubSub1
│   └───Fldr1Sub2
├───Folder 2
│   └───Fldr2Sub1
│       └───Fldr2SubSub1
├───Folder 3
└───Folder 4
Or in Text:
C:\Test Dir\Folder 1
C:\Test Dir\Folder 1\Fldr1Sub1
C:\Test Dir\Folder 1\Fldr1Sub1\Fldr1SubSub1
C:\Test Dir\Folder 1\Fldr1Sub2
C:\Test Dir\Folder 2
C:\Test Dir\Folder 2\Fldr2Sub1
C:\Test Dir\Folder 2\Fldr2Sub1\Fldr2SubSub1
C:\Test Dir\Folder 3
C:\Test Dir\Folder 4
Might be a whole lot easier to let the backup program create a list of files that it copied. What are you using to do the backup?

If you are using Robocopy to do the actual backup, it can easily create a log of the files it copies, which will show the Source path, rather than the Backup path.
And it will generate the list in Tree Order, rather than Level Order. The output will show the total files in each folder, so you would have to do some counting to count only the files that were copied, but not to hard to do, as you would only be parsing the log file, instead of actually doing Dir commands.
Another option is to use Robocopy to generate a list of files that are not more than 1 day old, this will show the path in the Backup tree rather than the Source tree.

This will list the number of subfolders in each folder, along with the number of files:
Code:
@Echo Off
Setlocal EnableDelayedExpansion
Set _Src=E:\Backup\Data
Set _OutPut=%_Src%\list.txt
Set _tmp=%_Src%\del1.txt
Echo Wscript.Echo ^(Date^(^)- 1^)>yesterday.vbs
For /F %%a In ('cscript //nologo yesterday.vbs') Do Set ydate1=%%a
Del yesterday.vbs
Set ydate1=%ydate1:/=%
Set d=%ydate1:~0,2%
Set m=%ydate1:~2,2%
Set y=%ydate1:~4,4%
Set ydate2=%d%/%m%/%y%
>"%_OutPut%" Echo Good morning, here is backup 2 yesterdays (%ydate2%) list. . .
>>"%_OutPut%" Echo.
For /F "Tokens=*" %%I In ('Dir "%_Src%" /AD /B /ON /S') Do (
If Exist "%_tmp%" Del "%_tmp%"
PushD "%%I"
Set _Count=0
For /F "tokens=1" %%A In ('Dir /AD') Do Set /A _Fldrs=%%A-2
For /F "tokens=*" %%A In ('Dir /A-D /OD^|Findstr "%ydate2%"') Do (
>>"%_tmp%" Echo %%A
Set /A _Count+=1
)
PopD
If !_Fldrs! GTR 0 If !_Count! GTR 0 >>"%_OutPut%" Echo %%I -- Number of Folders = !_Fldrs!
If !_Count! GTR 0 (
>>"%_OutPut%" Echo %%I -- Number of Files = !_Count!
>>"%_OutPut%" Type "%_tmp%"
>>"%_OutPut%" Echo.
))
If Exist "%_tmp%" Del "%_tmp%"
And for PushDirectory and PopDirectory type the command followed by /? for help:
Code:
C:\>pushD /?
Stores the current directory for use by the POPD command, then
changes to the specified directory.

PUSHD [path | ..]

  path        Specifies the directory to make the current directory.

If Command Extensions are enabled the PUSHD command accepts
network paths in addition to the normal drive letter and path.
If a network path is specified, PUSHD will create a temporary
drive letter that points to that specified network resource and
then change the current drive and directory, using the newly
defined drive letter.  Temporary drive letters are allocated from
Z: on down, using the first unused drive letter found.

C:\>PopD /?
Changes to the directory stored by the PUSHD command.

POPD


If Command Extensions are enabled the POPD command will delete
any temporary drive letter created by PUSHD when you POPD that
drive off the pushed directory stack.
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

thermal's Avatar
Junior Member with 5 posts.
 
Join Date: Nov 2009
Experience: Intermediate
15-Nov-2009, 06:59 AM #7
Hi TheOutcaste,

thanks for this, your explainations are helping me get to grips with describing exactly how I want things. [For your info: for backup, I am using xcopy over a network connection, but things get a little more complicated in that the files are dicom image files, so I am then copying the files to another (Backup) folder for sorting (using DicomSorter) into relevant folders dependent on imaging session etc.]

So, ideal situation:
The list would be in tree order and would like the total number of files in each lowest subfolder, which is always at the same level (4th level, with Backup being 1st level). I would also like the total number of folders at the fourth level so the text file produced looks like:

E:\Backup\folder_X\3rd_level\ 'contains X folders'
4th_level_folder1 'contains X files'
4th_level_folderx 'contains X files'

I tried the new code, but its output is identical to the first code and not giving any folder number info. (This could be because im using a dummy files system at home though, rather than the real thing at work).
Thanks
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
17-Nov-2009, 04:05 PM #8
Still not clear on just what folders will have files.
Is it only the 4th level folders that will have files, or will Folder_X and 3rd_level folders have files?
Is there a 5th or 6th level, or higher? Do you want files in those folders listed?

This should be getting close.
It will list the folders in tree order.
It will always list the number of 4th level folders, even if they contain no new files.
Should work for any number of levels.
You could easily remove the time and size and just list the date and file name, or list just the file name.

Code:
@Echo Off
Setlocal EnableDelayedExpansion
Set _Src=C:\Test Dir
Set _OutPut=%_Src%\list.txt
Set _tmp=%_Src%\del1.txt
Echo Wscript.Echo ^(Date^(^)- 1^)>yesterday.vbs
For /F %%a In ('cscript //nologo yesterday.vbs') Do Set ydate1=%%a
Del yesterday.vbs
Set ydate1=%ydate1:/=%
Set d=%ydate1:~0,2%
Set m=%ydate1:~2,2%
Set y=%ydate1:~4,4%
set ydate2=%d%/%m%/%y%
>"%_OutPut%" Echo Good morning, here is backup 2 yesterdays (%ydate2%) list. . .
>>"%_OutPut%" Echo.
For /R "%_Src%" %%I In (.) Do (
For /F "Tokens=1-4* Delims=\" %%J In ("%%~pnxI") Do (
Set _Level=0
If NOT "%%K"=="" Set _Level=1
If NOT "%%L"=="" Set _Level=2
If NOT "%%M"=="" Set _Level=3
If NOT "%%N"=="" Set _Level=4
)
If Exist "%_tmp%" Del "%_tmp%"
PushD "%%I"
Set _Count=0
Set _Fldrs=0
If "!_Level!"=="2" For /F "tokens=1" %%A In ('Dir /AD') Do Set /A _Fldrs=%%A-2
For /F "tokens=*" %%A In ('Dir /A-D /OD^|Findstr "%ydate2%"') Do (
>>"%_tmp%" Echo.   %%A
Set /A _Count+=1
)
PopD
If !_Fldrs! GTR 0 >>"%_OutPut%" Echo.%%~dpnxI -- Number of Folders = !_Fldrs!
If !_Count! GTR 0 (
>>"%_OutPut%" Echo.%%~dpnxI -- Number of Files = !_Count!
>>"%_OutPut%" Type "%_tmp%"
Echo. >>"%_OutPut%"
))
If Exist "%_tmp%" Del "%_tmp%"
With my test folders and forcing the date to 11/13, here's the output I get:
Code:
Good morning, here is backup 2 yesterdays (11/13/2009) list. . .

C:\Test Dir -- Number of Files = 1
   11/13/2009  12:57 PM               422 Test1.cmd
 
C:\Test Dir\Folder 1 -- Number of Files = 4
   11/13/2009  06:20 AM            64,440 Att00062.jpg
   11/13/2009  06:20 AM            65,880 Att00053.jpg
   11/13/2009  06:20 AM            37,607 Att00056.jpg
   11/13/2009  06:20 AM            73,732 Att00059.jpg
 
C:\Test Dir\Folder 1\F1S1Level3 -- Number of Folders = 2
C:\Test Dir\Folder 1\F1S1Level3\F1S1Level4 -- Number of Files = 4
   11/13/2009  06:20 AM            64,440 Att00062.jpg
   11/13/2009  06:20 AM            65,880 Att00053.jpg
   11/13/2009  06:20 AM            37,607 Att00056.jpg
   11/13/2009  06:20 AM            73,732 Att00059.jpg
 
C:\Test Dir\Folder 1\F1S1Level3\F1S1Level4\F1S1Level5 -- Number of Files = 2
   11/13/2009  06:20 AM            64,440 Att00062.jpg
   11/13/2009  06:20 AM            73,732 Att00059.jpg
 
C:\Test Dir\Folder 1\F1S1Level3\F1S2Level4 -- Number of Files = 4
   11/13/2009  06:20 AM            64,440 Att00062.jpg
   11/13/2009  06:20 AM            65,880 Att00053.jpg
   11/13/2009  06:20 AM            37,607 Att00056.jpg
   11/13/2009  06:20 AM            73,732 Att00059.jpg
 
C:\Test Dir\Folder 1\F1S1Level3\F1S2Level4\F1S2Level5 -- Number of Files = 2
   11/13/2009  06:20 AM            64,440 Att00062.jpg
   11/13/2009  06:20 AM            73,732 Att00059.jpg
 
C:\Test Dir\Folder 1\F1S2Level3 -- Number of Folders = 1
C:\Test Dir\Folder 1\F1S2Level3\F1S2Level4 -- Number of Files = 4
   11/13/2009  06:20 AM            64,440 Att00062.jpg
   11/13/2009  06:20 AM            65,880 Att00053.jpg
   11/13/2009  06:20 AM            37,607 Att00056.jpg
   11/13/2009  06:20 AM            73,732 Att00059.jpg
 
C:\Test Dir\Folder 1\F1S2Level3\F1S2Level4\F1S2Level5 -- Number of Files = 2
   11/13/2009  06:20 AM            64,440 Att00062.jpg
   11/13/2009  06:20 AM            73,732 Att00059.jpg
 
C:\Test Dir\Folder 2 -- Number of Files = 4
   11/13/2009  06:21 AM            64,464 Att00076.jpg
   11/13/2009  06:21 AM            73,700 Att00073.jpg
   11/13/2009  06:21 AM            37,633 Att00070.jpg
   11/13/2009  06:21 AM            65,880 Att00067.jpg
 
C:\Test Dir\Folder 2\F2S1Level3 -- Number of Folders = 1
C:\Test Dir\Folder 2\F2S1Level3\F2S1Level4 -- Number of Files = 2
   11/13/2009  06:20 AM            64,440 Att00062.jpg
   11/13/2009  06:20 AM            73,732 Att00059.jpg
 
C:\Test Dir\Folder 3\F2S1Level3 -- Number of Folders = 1
C:\Test Dir\Folder 4 -- Number of Files = 5
   11/13/2009  06:15 AM                 3 Br3tmary.doc
   11/13/2009  06:15 AM                 3 br3tMartin.doc
   11/13/2009  06:15 AM                 3 Br3tlisa.doc
   11/13/2009  06:15 AM                 3 br3tLindsey.doc
   11/13/2009  06:15 AM                 3 Br3tjohn.doc
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

thermal's Avatar
Junior Member with 5 posts.
 
Join Date: Nov 2009
Experience: Intermediate
18-Nov-2009, 07:25 AM #9
Hi theOutcaste,

thanks for this, it works a treat.
thermal
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 12:04 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.