Page 1 of 1
Need some vbscripting help
Posted: February 24th, 2012, 13:50
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?
Re: Need some vbscripting help
Posted: February 24th, 2012, 14:41
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.
Re: Need some vbscripting help
Posted: February 24th, 2012, 14:45
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.
Re: Need some vbscripting help
Posted: February 24th, 2012, 19:09
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.
Re: Need some vbscripting help
Posted: February 24th, 2012, 19:23
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.
Re: Need some vbscripting help
Posted: February 24th, 2012, 20:12
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.
Re: Need some vbscripting help
Posted: February 25th, 2012, 14:14
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...
Re: Need some vbscripting help
Posted: February 29th, 2012, 11:49
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)?
Re: Need some vbscripting help
Posted: February 29th, 2012, 12:05
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
to
Code: Select all
%dirsource%\????????????????????????????????????.csv
Re: Need some vbscripting help
Posted: February 29th, 2012, 16:40
by HereComesPete