Locating and copying a string in a text file

General Macro Scheduler discussion

Moderators: JRL, Dorian (MJT support)

Post Reply
BlackWisdom
Pro Scripter
Posts: 58
Joined: Thu Oct 16, 2003 12:53 am

Locating and copying a string in a text file

Post by BlackWisdom » Tue Dec 07, 2004 6:55 pm

:D Hi guys awesome work your doing - I have a batch file that basically does the dir /s /a command and gives me the total of folders and subfolders on each drive and then writes that total to a text file "FolderManifest.txt". My challengs is looking inside the txt file and pulling the totals statement info for each drive. The total look like this:

Total Files Listed:
91708 File(s) 59,279,178,006 bytes
18417 Dir(s) 20,479,672,320 bytes free

But since it scans for each drive - my challenge is skipping past all the subdirecotry entries and locating the three strings above, for each drive and copying that data into a text file with a new name for instance "FolderFinal.txt" - you follow me. Either that or deleting everything in the file except those total entries...

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Tue Dec 07, 2004 8:25 pm

Hi,

One way is to use the DOS find command. This will give you the total number of files:

for /f "tokens=1" %q in ('dir /s /a ^| find "File(s)"') do echo %q

But there's a much easier way:


VBSTART

Set objFSO = CreateObject ("Scripting.FileSystemObject")
dim intFil
dim bytes

Sub CountFiles1(Folder)
Set colFiles= Folder.files
For each objFile in colFiles
intFil=intFil+1
bytes=bytes+objFile.size
Next
For Each Subfolder in Folder.SubFolders
Set objfolder= objFSO.GetFolder(Subfolder.Path)
CountFiles Subfolder
Next
End Sub

Sub CountFiles(FolderSpec)
CountFiles1(objFSO.GetFolder(FolderSpec))
End Sub
VBEND

VBRun>CountFiles,f:\
VBEval>intFil,totfiles
VBEval>bytes,totbytes
MessageModal>%totfiles% %totbytes%


Have fun!
MJT Net Support
[email protected]

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Tue Dec 07, 2004 10:47 pm

I was also going to suggest using FIND redirected to another file.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

BlackWisdom
Pro Scripter
Posts: 58
Joined: Thu Oct 16, 2003 12:53 am

fascinating..

Post by BlackWisdom » Wed Dec 08, 2004 8:18 pm

Its amazing that what is seemingly a simple thing to do baffles so many I have talked to other developers and everyone seems to be stuck on this simple need: the VBScript generated a stack error (it was the only thing in the macro). My kingdom for a script!! I need a way to:

Count the folders and subfolders on each drive
and then write that data to a text file - without the ten thousand lines
that precede it (like when you do a batch file) ...

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Wed Dec 08, 2004 9:04 pm

What was the error? This script counts all the files in the C: drive, totals up their sizes and outputs to a text file. It does what you asked for. It works for me 100%:


VBSTART

Set objFSO = CreateObject ("Scripting.FileSystemObject")
dim intFil
dim bytes

Sub CountFiles1(Folder)
Set colFiles= Folder.files
For each objFile in colFiles
intFil=intFil+1
bytes=bytes+objFile.size
Next
For Each Subfolder in Folder.SubFolders
Set objfolder= objFSO.GetFolder(Subfolder.Path)
CountFiles Subfolder
Next
End Sub

Sub CountFiles(FolderSpec)
CountFiles1(objFSO.GetFolder(FolderSpec))
End Sub
VBEND

VBRun>CountFiles,f:\
VBEval>intFil,totfiles
VBEval>bytes,totbytes
WriteLn>c:\myfile.txt,r,Files: %totfiles% Bytes: %totbytes%


If you get an error with this let me know what happens.
MJT Net Support
[email protected]

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Wed Dec 08, 2004 9:16 pm

Count the folders and subfolders on each drive
and then write that data to a text file - without the ten thousand lines
that precede it (like when you do a batch file) ...
No time to do the script right now, but this skeleton should work for you.
Make a macro to do the following:

1. \\List all folders to a text file
dir c:\*. /s >>Drivec.lst...........Note using "*." vs. "*.*" to get folders vs. files.
Run Program>cmd.com....DIR.....

2. \\Wrilte the summary lines from the text file to another text file
find "dir(s)" Drivec.lst >> Summary.lst
Run Program>cmd.com......FIND...

3. \\count the lines in summary text file and read contents of last line.
last line = total folders, just read last line.
ReadLn>....Summary.lst...

3. \\Write that last line to a new text file.
WriteLn>.....NewFile.txt...
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Wed Dec 08, 2004 9:22 pm

Yes, but this is VERY VERY VERY VERY VERY slow. Here's how it looks:


//Set the temp work file here
Let>TempFile=d:\work.txt
//Set Output file here
Let>OutFile=d:\output.txt
Let>RP_WAIT=1
Let>RP_WINDOWMODE=0
IfFileExists>TempFile
DeleteFile>TempFile
Endif
IfFileExists>OutFile
DeleteFile>OutFile
EndIf

//set drive letter here and run CountFiles
Let>drive=C
GoSub>CountFiles

//Do it again for another drive etc etc
Let>drive=D
GoSub>CountFiles


SRT>CountFiles
WriteLn>TempFile,r,Drive %drive%:
Run>cmd /c dir %drive%:\ /s /a >> %TempFile%
Let>k=0
Label>scraplines
Let>k=k+1
ReadLn>TempFile,k,line
If>line##EOF##,scraplines
Let>s1=k-3
Let>s2=k-2
Let>s3=k-1
ReadLn>TempFile,s1,line1
ReadLn>TempFile,s2,line2
ReadLn>TempFile,s3,line3
DeleteFile>TempFile
WriteLn>OutFile,r,Totals for Drive %drive%
WriteLn>OutFile,r,line1
WriteLn>OutFile,r,line2
WriteLn>OutFile,r,line3
End>CountFiles


It will take a very long time indeed if your drives are anywhere near average sizes.
MJT Net Support
[email protected]

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Wed Dec 08, 2004 9:36 pm

Support is addressing FILES, I was addressing FOLDERS. Will speed still be such a big issue?

I see no need to read files.
Can add FIND /N on summary.lst file to get the line number with the result. Redirect that to another file,
and parse out the first 10 or so characters to get the line number.
and just do a ReadLn> for that line number.

Actually, don't need summary.lst file, can do FIND on drivec.lst because there will only be one line for dir(s), not like files(s) which will repeat.

Do Find /n "dir(s)" drivec.lst to get the line number.
Now ReadLn of drivec.lst for that line number and send to the final Manifest.txt file.

(Note.....all untested)
Last edited by Bob Hansen on Wed Dec 08, 2004 9:42 pm, edited 1 time in total.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Wed Dec 08, 2004 9:39 pm

Yes, because the original poster asked for these lines:

Total Files Listed:
91708 File(s) 59,279,178,006 bytes
18417 Dir(s) 20,479,672,320 bytes free

Which are total files and total bytes.

This version uses Find command to get total files and total bytes. Much quicker.


//Set the temp work file here
Let>TempFile=d:\work.txt
//Set Output file here
Let>OutFile=d:\output.txt
Let>RP_WAIT=1
Let>RP_WINDOWMODE=0
IfFileExists>TempFile
DeleteFile>TempFile
Endif
IfFileExists>OutFile
DeleteFile>OutFile
EndIf

//This section does drive D:
WriteLn>OutFile,r,Drive D:
Run>cmd /c "for /f "tokens=1" %q in ('dir d: /s /a ^| find "File(s)"') do echo Files: %q" >> %TempFile%
Let>k=0
Label>scraplines
Let>k=k+1
ReadLn>TempFile,k,line
If>line##EOF##,scraplines
Let>s3=k-1
ReadLn>TempFile,s3,line3
DeleteFile>TempFile
WriteLn>OutFile,r,line3
Run>cmd /c "for /f "tokens=3" %q in ('dir d: /s /a ^| find "Dir(s)"') do echo Bytes: %q" >> %TempFile%
Let>k=0
Label>scraplines2
Let>k=k+1
ReadLn>TempFile,k,line
If>line##EOF##,scraplines2
Let>s3=k-1
ReadLn>TempFile,s3,line3
DeleteFile>TempFile
WriteLn>OutFile,r,line3
End>CountFiles
MJT Net Support
[email protected]

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Wed Dec 08, 2004 9:47 pm

How about a compromise if files is needed with folders?

Since only one line with dir(s), we can get that line number with find /n.
That means that line for total files is on "n - 1"

Now Do ReadLn for lines n-1 and n and ouput them to FolderManifest.txt
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts