Computing.Net > Forums > Programming > VB file name parser and sorter

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

VB file name parser and sorter

Reply to Message Icon

Original Message
Name: RiverQ586
Date: August 5, 2007 at 18:44:06 Pacific
Subject: VB file name parser and sorter
OS: WinXP
CPU/Ram: Intel Core 2 Duo, 1GB
Comment:

Hello,

I've been trying to write a VB Script that would take in a list of file names in the same directory, parse out the ones with a *.zip extension and then arrange by state with the max version number.

Example:

STPICA01.zip
STPICA02.zip
STPIIA01.zip
STPIIL01.zip
STPIIL02.zip
STPIIL10.zip

They all follow the same convention:
STPI<state code><version>.zip

So what would be outputted ideally:

CA Version 02
IA Version 01
IL Version 10

I've gotten close with AutoIt scripting, it would just output the parsed names to a text file, but everytime it ran, I had to click on Run for the security settings. Also it displayed all the states. It needs to run with no user intervention.

I've also tried DOS scripting and got as far as this command:

dir /B /N STPI*.zip > STATEVER.txt

After that the syntax was too daunting for me.

And the reason I resorted to using VB Script is that I'm using BGinfo to display information on my desktop (there's a custom field setting), and I needed something to tell me what state versions are loaded on this machine. (It's for tax prep software)

Any help would be greatly appreciated, thanks!


Report Offensive Message For Removal


Response Number 1
Name: ghostdog
Date: August 6, 2007 at 03:50:53 Pacific
Reply: (edit)

[code]
Option Explicit
Dim objFSO,myFolder,myFile,myArray(),objDictionary,item
Dim state,stateNum,a,i,b
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
myFolder="c:\temp"
For Each myFile In objFSO.GetFolder(myFolder).Files
If objFSO.GetExtensionName(myFile) = "zip" _
And InStr(1,myFile,"STP") > 0 Then
state = Mid(myFile.Name,5,2)
stateNum=Mid(myFile.Name,7,2)
If objDictionary.Exists(state) Then
a = objDictionary.Keys
For i = 0 To objDictionary.Count -1
If a(i) = state Then
objDictionary.Remove(state)
objDictionary.Add state, stateNum
End If
Next
Else
objDictionary.Add state, stateNum
End If
End If
Next
a=objDictionary.Items
b=objDictionary.Keys
For i = 0 To objDictionary.Count -1
WScript.Echo "StateNum is ", a(i), "State is: ",b(i)
Next
[/code]

I leave it to you for the rest.


Report Offensive Follow Up For Removal

Response Number 2
Name: Mechanix2Go
Date: August 6, 2007 at 03:52:22 Pacific
Reply: (edit)

@echo off > STATEVER.txt
setLocal EnableDelayedExpansion


for /f "tokens=* delims= " %%a in ('dir/b/on/a-d STPI*.zip') do (
set str=%%~Na
echo !str:~4,2! Version !str:~6,2! >> STATEVER.txt
)



=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software