Vb program lists files from extensions to text, need improve

Hewlett-packard Z420 d8d09ut convertible...
October 22, 2014 at 07:40:28
Specs: Windows 7, 16
I have a .vbs script, that lists files to text but i need to remove duplicated lines. My .vbs is

Option Explicit 'force all variables to be declared
Const ForWriting = 2
Dim objFSO 'File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objTS 'Text Stream Object
Set objTS = objFSO.OpenTextFile("C:\Output.txt", ForWriting, True)
Call Recurse("Z:\PAYTV_TX\MOVIE")

Sub Recurse(strFolderPath)
Dim objFolder
Set objFolder = objFSO.GetFolder(strFolderPath)
Dim objFile
Dim objSubFolder

For Each objFile In objFolder.Files
'only proceed if there is an extension on the file.
If (InStr(objFile.Name, ".") > 0) Then
'If the file's extension is "pfx", write the path to the output file.
If (LCase(Mid(objFile.Name, InStrRev(objFile.Name, "."))) = ".mxf") Then _
objTS.WriteLine(objfile.Path) end if
If (LCase(Mid(objFile.Name, InStrRev(objFile.Name, "."))) = ".stl") Then _
End If

For Each objSubFolder In objFolder.SubFolders
Call Recurse(objSubFolder.Path)
End Sub

This works perfectly and output looks like;

.stl files must have _TUR before .stl extension. I want to see faulty ones like

Z:\PAYTV_TX\MOVIE\Messenger_The\Messenger_The_PAYTV_TX_T7_TUR.stl (*.stl file dont have _U1 before _TUR and )

Is there a way to remove right ones and see others ?

Any help would be much appreciated

See More: Vb program lists files from extensions to text, need improve

Report •

October 22, 2014 at 20:07:48
I can supply a batch version that is simpler and seems to work:
::==== begin batchscript
@echo off>bad & setlocal
for /D %%a in (*) do (
for /f "tokens=*" %%b in ('dir /b "%%~nxa\*.mxf'") do (
rem echo %%~dpnxa\%%~nb_tur.stf
if not exist "%%~dpnxa\%%~nb_tur.stf" >> bad echo %%b
::==== end batch
Vbscript is great, but sometimes it's overkill and slightly more cumbersome to use.

message edited by nbrane

Report •

October 22, 2014 at 22:32:24
@nbrane thank you for your answer, its not important batch or vbscript.
When i run your program, it makes a file, named "bad" and it comes empty. Would you please tell me what am i doing wrong ?

Other hand, is that possible? i changed all lines from ending "_TUR.stl" to ".mxf" with Notepad+.
That means i have exactly duplicated lines. I want to remove all of the duplicate lines, including the first duplicated line, leaving only lines that did not have any duplicates at all.
with that "_tur.stl" to ".mxf" change my text file is like;


i want to output file like.

Maybe with this way it should have 2 script but its not important.

message edited by Jakida

Report •

October 25, 2014 at 17:38:49
I don't understand exactly what you're doing and what the problem is but you may need to look into the function substr(). Also, store all of the directories in an array before output. From the array you would run through another iteration of checking for duplicates and then removing any extras. From there you would output the data how you like it.

dim FileLoc as string = objSubFolder.Path
if substr(FileLoc, FileLoc.count, 8) == "_TUR.stl" then
FileLoc = substr(FileLoc, 0, FileLoc.count - 8)
FileLoc &= ".MXF"

This will change "Z:\PAYTV_TX\MOVIE\Messenger_The\Messenger_The_PAYTV_TX_T7_TUR.stl" to "Z:\PAYTV_TX\MOVIE\Messenger_The\Messenger_The_PAYTV_TX_T7.MXF"

Ideally you would have FileLoc be an array and the above code would be part of the initial iteration to rewrite the path names. From there you would go through another FOR loop to open the first item, check for duplicates with another FOR loop inside of that FOR loop.

For %I = 0 to FileLoc.count
for %J = %I to FileLoc.count
if FileLoc[%I] == FileLoc[%J] then
FileLoc[%J] = ""

For %I = 0 to FileLoc.count
if FileLoc[%I] <> "" then
textbox1.text &= FileLoc[%I] & vbcrlf

I haven't used VB.NET in a while so the syntax may be a bit off but this idea will easily solve your problem if I had any idea what it was. On the COUNTS you may have to add a "- 1" after that otherwise you might get an "array out of bounds" error.

Lemme know how it works out!

Report •

Related Solutions

October 25, 2014 at 18:28:54
Not sure, worked on my short-sample test (two directories). Try this alteration from:
rem echo %%~dpnxa\%%~nb_tur.stf
if not exist "%%~dpnxa\%%~nb_tur.stf" >> bad echo %%b

echo testing for: %%~dpnxa\%%~nb_tur.stf
if not exist "%%~dpnxa\%%~nb_tur.stf" >> bad echo %%b
if not exist "%%~dpnxa\%%~nb_tur.stf" echo BAD NAMING CONVENTION ==========%%b&pause

That should show up on your screen with enough fireworks to draw attention. If you don't get any fireworks, I'll need to get more info from you about your situation.

Report •

October 26, 2014 at 04:34:14
i dont know why but it is still giving me same empty bad file. thats my bad, i didnt tell you, my all .mxf and .stl files are in a different folder. .mxf files are videos, .stl files are subtitles. Thats because of they needs _TUR before .stl extension. There is thousands of movies and subtitles. Subtitle names have to be same as movie names except _TUR and .stl extension. I need to find wrong typed subtitles.

All movies are in Z:\PAYTV_TX\MOVIE folder. Every movie and subtitle has a folder like if the movie name is Matrix, .mxf and .stl files are in
Z:\PAYTV_TX\MOVIE\MATRIX\ , .mxf and .stl files like


Folder names are not the same as the file names(before extensions).
In my first post, vbscript gives every .mxf and .stl files in a text. But its still very hard to check every line and to see difference, its upper and lower case sensitive too :(

for .bat script "dir /b" is not giving the .mxf and .stl files if you in MOVIE's folder.

@FBI Agent
My english is not so well, sorry if i didnt explain. I want to see the differences in a text file or something like that. I didnt want to change _TUR.stl files to .MXF, I did it in a text line in text editor. I didnt understand the second part, i am very new for batch

Report •

October 26, 2014 at 10:34:02
The second part is not batch, it's VB.NET (most syntax will be correct when used with VBA).

Sorry man but I really cannot figure out what you're trying to do. Hopefully one of these other guys can help out :/

Report •

October 26, 2014 at 11:54:38
Ok, although my first response worked (even though it was sloppy coding), I re-vamped it as follows:
::========= begin second attempt
@echo off & setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ('dir /b /s *.mxf') do (
echo testing movie-path: %%~pa
for /f "tokens=*" %%b in ('dir /b %%~dpa\*.stf') do (
set no_TUR=%%~nb
set no_TUR=!no_TUR:~0,-4!
echo testing: !no_TUR! against: %%~na
if /i "!no_TUR!" neq "%%~na" (
echo -------- bad: !no_TUR! ### should be: %%~na
>>bad echo bad: !no_TUR! ### should be: %%~na
::========= end script
this one worked as well. I put some displays, and a pause in to try and see what's going on. Remove the pause etc and re-run if it looks like it's working.
For a text-file method, like you seem to favor, this klunky code might work:
::======== begin script using textfile instead of "DIR"
@echo off>jak1 & setlocal enabledelayedexpansion
:: two methods to build the JAK2 textfile. If this first doesn't work, try the second.
dir /b /s /a-d *paytv* | sort > jak2
goto :x
:: method 2, requires two passes and two files
for %%a in (stf mxf) do dir /b /s /a-d *.%%a >> jak1
sort jak1 > jak2
echo JAK2 file should look like your vbscript output:
type JAK2
for /f "tokens=*" %%a in (jak2) do (
set t3=%%~pa
set t1=%%~na
set t2=%%~xa
if /i "!t2!" equ ".stf" set t1=!t1:~0,-4!

if "!t3!" equ "!pt3!" if /i "!t1!" neq "!pt1!" echo bad: %%a

set pt3=!t3!
set pt1=!t1!
::======= end script
This coding was very awkward and klunky, but it also seemed to work. I don't know what's hanging up on your tests... If you're not running from the command-prompt, I would recommend you do so. Everything on my end seems on the beam.

Report •

October 26, 2014 at 14:19:28
Thank you for your work. My Z: drive is in network. I didnt know that command cant reach it. I changed cd "Z:\PAYTV_TX\MOVIE" to pushd "\\\PAYTV_TX\MOVIE"
(I only have read permissions, i cant write network folder)
and actually first one going well. But some movie folders has 2 different type of movies and subtitles. Because some movies have a day version( without blood, sexuality, drug scenes). For example, This files are in same folder

First one is running well but it shows these too. that means a lot of lines :(

For second part, jak2 is worked for local(jak1 is empty). I used pushd thing in second part (changed cd "Z:\PAYTV_TX\MOVIE" to pushd "\\\PAYTV_TX\MOVIE") but couldnt worked out. It says Access Denied.

Thank you for helping me again

Report •

October 26, 2014 at 18:36:45
I'm not real clear what you current status is. I understand about the network-mapped drive, but you said your fix fixed that. As for the day-time versions change this line:
for /f "tokens=*" %%a in ('dir /b /s *.mxf') do (
to this:
for /f "tokens=*" %%a in ('dir /b /s *.mxf ^|find /v "_DAY_"') do (

And make sure the output is written to someplace you have file create/write privelage (fully qualify the path of "bad" to somewhere on C drive or wherever). Also, you don't have to run the batch from the network path, as long as your batch can access it to recursively read filenames from the network drive and path, which again requires full qualification of all references.

Report •

Ask Question