Need some vbscripting help

News and important info, general banter, and suggestions for 5punk

Moderator: Forum Moderators

Post Reply
Wiggy
5pork
5pork
Posts: 925
Joined: June 12th, 2005, 17:00
Location: Chesterfield, UK

Need some vbscripting help

Post by Wiggy »

Werd, yous lot.

I'm trying to write a vbscript that will be able to look at a filename and be able to read off certain characters, then use those characters to create a folder that I can put that file in. Anyone know of any way I can enumerate characters - say - 18 to 24 in a 37-character filename (not including the file extension at the end) and store it as a variable to use when creating the folder?
deject
Berk
Berk
Posts: 10353
Joined: December 7th, 2004, 17:02
Location: Oklahoma City, OK, USA
Contact:

Re: Need some vbscripting help

Post by deject »

Hmmm, that should be pretty easy but I am not familiar with vbscript. I'll look up some documentation and see if I can find out.
Wiggy
5pork
5pork
Posts: 925
Joined: June 12th, 2005, 17:00
Location: Chesterfield, UK

Re: Need some vbscripting help

Post by Wiggy »

As long as I can run it as some sort of script, I'm not too bothered. I'm still trying to learn vbscript myself.

What I'm aiming for is:
1. Move file from remote PC to this PC.
2. Find out what the 18th to 24th characters of the filename are and save that data as a variable.
3. Use the variable to create a folder of the same name on the server, unless one already exists.
4. Move files from PC to new folder on server.
5. Repeat until no more files are available on the 1st PC.

Like I say, I'm still learning scripting and not entirely sure if vbscript is the best way to go, but it just needs to be something that can be set up as a scheduled task and run every 10 minutes.
buzzmong
Weighted Storage Cube
Weighted Storage Cube
Posts: 7167
Joined: February 26th, 2007, 17:26
Location: Middle England, nearish Cov

Re: Need some vbscripting help

Post by buzzmong »

Dunno if VBScript can do all that but VB.Net (or any other .net, namely C# or C++) will be able to do what you want easily.

Running it as a task is easy as well, especially if it's an application.
FatherJack
Site Owner
Site Owner
Posts: 9597
Joined: May 16th, 2005, 15:31
Location: Coventry, UK
Contact:

Re: Need some vbscripting help

Post by FatherJack »

Not familiar with it much myself, but the chopping the string bit and directory-creating bit would be something like this I guess:

dim filesys, newfolder, newfolderpath
newfolderpath = Mid(filenamestring,18,6)
set filesys=CreateObject("Scripting.FileSystemObject")
If Not filesys.FolderExists(newfolderpath) Then
Set newfolder = filesys.CreateFolder(newfolderpath)
Response.Write("A new folder has been created at: " newfolderpath)
End If


You'd probably be best first reading the remote directory listing to a text file, then processing the text file to do the directory create and file move. If you search for 'read directory in to text file in VBscript' it should give you enough, or simply use dir/b > textfile.txt as a scheduled script.
buzzmong
Weighted Storage Cube
Weighted Storage Cube
Posts: 7167
Joined: February 26th, 2007, 17:26
Location: Middle England, nearish Cov

Re: Need some vbscripting help

Post by buzzmong »

FatherJack wrote:You'd probably be best first reading the remote directory listing to a text file, then processing the text file to do the directory create and file move. If you search for 'read directory in to text file in VBscript' it should give you enough, or simply use dir/b > textfile.txt as a scheduled script.
^^ Is important.

You could read the directory and store it as an array, but appending to a text file is 1) Easier 2) Program has smaller memory footprint and 3) Means less duplication, especially in light of you wanting to run it every 10 minutes and finally 4) Acts as a visual log.
ProfHawking
Zombie
Zombie
Posts: 2101
Joined: February 20th, 2005, 21:31

Re: Need some vbscripting help

Post by ProfHawking »

How about this?

Code: Select all

@ECHO off
setlocal EnableDelayedExpansion

:: SETTINGS
SET dirsource=\\server\somefolder
SET dirdest=c:\localfolder

:: BEGIN JOB
ECHO ======================= >> log.txt
ECHO RUNNING JOB >> log.txt
DATE/T >> log.txt
TIME/T >> log.txt
ECHO ======================= >> log.txt
:: LOOP THROUGH FILES
FOR %%f IN ("%dirsource%\*.*") DO (
	SET file=%%~nxf
	@ECHO Processing File: !file! >> log.txt
	:: GET FOLDER TO USE (18 chars in, 7 chars long)
	SET folder=!file:~18,7!
	:: COPY FILE TO NEW DESTINATION
	ECHO F | XCOPY /Y "%dirsource%\!file!" "%dirdest%\!folder!\!file!"
	:: DELETE FROM SOURCE (Careful with this!)
	DEL "%dirsource%\!file!"
)
EXIT
Stick it in a batch file, save as .cmd etc
Be careful with the delete - it doesn't check the file was copied or anything...
Wiggy
5pork
5pork
Posts: 925
Joined: June 12th, 2005, 17:00
Location: Chesterfield, UK

Re: Need some vbscripting help

Post by Wiggy »

Nice one. Seems to work well, thanks for that.

Is there any line I can add in that would make it ignore files that aren't 36 characters long (plus the .csv on the end)?
FatherJack
Site Owner
Site Owner
Posts: 9597
Joined: May 16th, 2005, 15:31
Location: Coventry, UK
Contact:

Re: Need some vbscripting help

Post by FatherJack »

Wiggy wrote:Nice one. Seems to work well, thanks for that.

Is there any line I can add in that would make it ignore files that aren't 36 characters long (plus the .csv on the end)?
I guess you would just change

Code: Select all

%dirsource%\*.*
to

Code: Select all

%dirsource%\????????????????????????????????????.csv
HereComesPete
Throbbing Cupcake
Throbbing Cupcake
Posts: 10249
Joined: February 17th, 2007, 23:05
Location: The maleboge

Re: Need some vbscripting help

Post by HereComesPete »

Code: Select all

%dirsource%\CIIJASIIE
Post Reply