Need some vbscripting help
Moderator: Forum Moderators
Need some vbscripting help
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?
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

- Posts: 10353
- Joined: December 7th, 2004, 17:02
- Location: Oklahoma City, OK, USA
- Contact:
Re: Need some vbscripting help
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
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.
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

- Posts: 7167
- Joined: February 26th, 2007, 17:26
- Location: Middle England, nearish Cov
Re: Need some vbscripting help
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.
Running it as a task is easy as well, especially if it's an application.
-
FatherJack
- Site Owner

- Posts: 9597
- Joined: May 16th, 2005, 15:31
- Location: Coventry, UK
- Contact:
Re: Need some vbscripting help
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.
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

- Posts: 7167
- Joined: February 26th, 2007, 17:26
- Location: Middle England, nearish Cov
Re: Need some vbscripting help
^^ Is important.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.
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

- Posts: 2101
- Joined: February 20th, 2005, 21:31
Re: Need some vbscripting help
How about this?
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...
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!"
)
EXITBe careful with the delete - it doesn't check the file was copied or anything...
Re: Need some vbscripting help
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)?
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

- Posts: 9597
- Joined: May 16th, 2005, 15:31
- Location: Coventry, UK
- Contact:
Re: Need some vbscripting help
I guess you would just changeWiggy 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)?
Code: Select all
%dirsource%\*.*Code: Select all
%dirsource%\????????????????????????????????????.csv-
HereComesPete
- Throbbing Cupcake

- Posts: 10249
- Joined: February 17th, 2007, 23:05
- Location: The maleboge
Re: Need some vbscripting help
Code: Select all
%dirsource%\CIIJASIIE