By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,143 Members | 1,854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,143 IT Pros & Developers. It's quick & easy.

creating a folder on each drive

P: n/a
Hi, I am trying to find a VB way that would create a folder on all
existing drives - the folder name would be the same on each drive. ie
c:\backup, d:\backup, etc. But the folders would only be created if
they don't already exist, and if the drive happens to be one a folder
cannot be created on (ie a cdrom drive) it would just be skipped
without the code generating any errors.
your help on this would be most appreciated. jenny
Jul 17 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
je**@onlink.net (jenny) wrote in message news:<3d**************************@posting.google. com>...
Hi, I am trying to find a VB way that would create a folder on all
existing drives - the folder name would be the same on each drive. ie
c:\backup, d:\backup, etc. But the folders would only be created if
they don't already exist, and if the drive happens to be one a folder
cannot be created on (ie a cdrom drive) it would just be skipped
without the code generating any errors.
your help on this would be most appreciated. jenny


You can use the FSO, the only drawback is (apart from having to
reference it)
it can be optionaly switched off on a users machine.

I would avoid using the FSO with the following.

Private Declare Function GetLogicalDrives Lib "kernel32" () As Long
Use it to Create a Bitwise loop to give you the drives

Private Declare Function GetDriveType Lib "kernel32" Alias
"GetDriveTypeA" (ByVal nDrive As String) As Long
Use to rid unwanted drives CD's etc

VB's DIR to see if folders exist & MKDIR to create them.

You can also use instr mid etc in the loop
if you want to get fancy to create one time multiple folders
e.g. Create/All/These/Folders/in/one/go
Jul 17 '05 #2

P: n/a
> > Hi, I am trying to find a VB way that would create a folder on all
existing drives - the folder name would be the same on each drive. ie
c:\backup, d:\backup, etc. But the folders would only be created if
they don't already exist, and if the drive happens to be one a folder
cannot be created on (ie a cdrom drive) it would just be skipped
without the code generating any errors.
your help on this would be most appreciated. jenny


You can use the FSO, the only drawback is (apart from having to
reference it)
it can be optionaly switched off on a users machine.

I would avoid using the FSO with the following.

Private Declare Function GetLogicalDrives Lib "kernel32" () As Long
Use it to Create a Bitwise loop to give you the drives

Private Declare Function GetDriveType Lib "kernel32" Alias
"GetDriveTypeA" (ByVal nDrive As String) As Long
Use to rid unwanted drives CD's etc

VB's DIR to see if folders exist & MKDIR to create them.


What about making the code even simpler?

Dim X As Integer
On Error Resume Next
For X = Asc("c") To Asc("z")
MkDir Chr$(X) & ":\backup"
Next

Rick - MVP
Jul 17 '05 #3

P: n/a
"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message news:<K3********************@comcast.com>...
Hi, I am trying to find a VB way that would create a folder on all
existing drives - the folder name would be the same on each drive. ie
c:\backup, d:\backup, etc. But the folders would only be created if
they don't already exist, and if the drive happens to be one a folder
cannot be created on (ie a cdrom drive) it would just be skipped
without the code generating any errors.
your help on this would be most appreciated. jenny


You can use the FSO, the only drawback is (apart from having to
reference it)
it can be optionaly switched off on a users machine.

I would avoid using the FSO with the following.

Private Declare Function GetLogicalDrives Lib "kernel32" () As Long
Use it to Create a Bitwise loop to give you the drives

Private Declare Function GetDriveType Lib "kernel32" Alias
"GetDriveTypeA" (ByVal nDrive As String) As Long
Use to rid unwanted drives CD's etc

VB's DIR to see if folders exist & MKDIR to create them.


What about making the code even simpler?

Dim X As Integer
On Error Resume Next
For X = Asc("c") To Asc("z")
MkDir Chr$(X) & ":\backup"
Next

Rick - MVP

Thanks a bunch! - that was good -never knew it could be so simple
Your code looks similar to some vbs in a file I have which I would
love to be able to convert to VB. Seeing your code makes it look like
it may be possible. This vbs code I have is a study demo to show
moving a prefixed and suffixed folder to another folder and at the
same time renaming it without the prefix suffix:

Dim WshShell, fso, fn, drv, dst, src
set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
fn=InputBox("Enter folder name from. ","Folder Move")
If Not Trim(fn) = "" then
For drv = Asc("C") to Asc("Z")
src=Chr(drv)+":\backup\ab"+fn+"12"
If fso.FolderExists(src) Then
dst = Chr(drv)+":\work\"+fn
fso.MoveFolder src, dst
End If
Next
End If

I don't really like it in vbs because it always brings up a Norton Av
alert even though it is innocent vbs. Anyway, it can be made the same
in VB?

thanks again for the help!

jenny
Jul 17 '05 #4

P: n/a
> > What about making the code even simpler?

Dim X As Integer
On Error Resume Next
For X = Asc("c") To Asc("z")
MkDir Chr$(X) & ":\backup"
Next

Rick - MVP
Thanks a bunch! - that was good -never knew it could be so simple


I should point out one potential problem with the code I posted. I'm no
longer on a network to test this, but I would guess if you have any attached
network drives, and if you have write access to them, then I believe the
above routine would create a directory out on the network drive too. (Again,
I can't test this, but I believe it to be so.) If you have that situation,
then you will probably need to use GetDriveType to check if any drive letter
not producting an error is a network drive or not and skip over it if it is.

Your code looks similar to some vbs in a file I have which I would
love to be able to convert to VB. Seeing your code makes it look like
it may be possible. This vbs code I have is a study demo to show
moving a prefixed and suffixed folder to another folder and at the
same time renaming it without the prefix suffix:

Dim WshShell, fso, fn, drv, dst, src
set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
fn=InputBox("Enter folder name from. ","Folder Move")
If Not Trim(fn) = "" then
For drv = Asc("C") to Asc("Z")
src=Chr(drv)+":\backup\ab"+fn+"12"
If fso.FolderExists(src) Then
dst = Chr(drv)+":\work\"+fn
fso.MoveFolder src, dst
End If
Next
End If

I don't really like it in vbs because it always brings up a Norton Av
alert even though it is innocent vbs. Anyway, it can be made the same
in VB?


I would avoid using FSO in VB projects like the plague (you can Google the
newsgroups to find out why; this subject has been beaten to death in many
previous newsgroup posts). VB has a built-in Name...As statement that can
move a file around. Consider this (off the top of my head) code sample:

Path = "d:\SomeDirectory\backup\"
NewPath = "d:\SomeDirectory\work\"
Prefix = "ab"
FileName = "RICK"
Suffix = "12"
FileType = ".txt"
Name Path & Prefix & FileName & Suffix & _
FileType As NewPath & FileName & FileType

Rick - MVP
Jul 17 '05 #5

P: n/a
Hi, I got it solved now with the code Rick gave but thanks for the
suggestion.
However, I looked at the Reference list and I cannot find anything
listed as FileSystemObject. I'm using VB6.

jenn
"Stephane Richard" <st**************@verizon.net> wrote in message news:<cN*****************@nwrdny02.gnilink.net>...
Hi Jenny,

I suggest you reference the FileSystemObject "Project menu", then
"References"

It has everything you need to do what you're asking for. and it's worked
quite well for me....create yourself a variable As FileSystemObject. and
look at all the methods and properties available to you. You'll see it has
it all :-).

--
Stéphane Richard
Senior Software and Technology Supervisor
http://www.totalweb-inc.com
For all your hosting and related needs
"jenny" <je**@onlink.net> wrote in message
news:3d**************************@posting.google.c om...
Hi, I am trying to find a VB way that would create a folder on all
existing drives - the folder name would be the same on each drive. ie
c:\backup, d:\backup, etc. But the folders would only be created if
they don't already exist, and if the drive happens to be one a folder
cannot be created on (ie a cdrom drive) it would just be skipped
without the code generating any errors.
your help on this would be most appreciated. jenny

Jul 17 '05 #6

P: n/a
"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message news:<-7********************@comcast.com>...
What about making the code even simpler?

Dim X As Integer
On Error Resume Next
For X = Asc("c") To Asc("z")
MkDir Chr$(X) & ":\backup"
Next

Rick - MVP


Thanks a bunch! - that was good -never knew it could be so simple


I should point out one potential problem with the code I posted. I'm no
longer on a network to test this, but I would guess if you have any attached
network drives, and if you have write access to them, then I believe the
above routine would create a directory out on the network drive too. (Again,
I can't test this, but I believe it to be so.) If you have that situation,
then you will probably need to use GetDriveType to check if any drive letter
not producting an error is a network drive or not and skip over it if it is.

Your code looks similar to some vbs in a file I have which I would
love to be able to convert to VB. Seeing your code makes it look like
it may be possible. This vbs code I have is a study demo to show
moving a prefixed and suffixed folder to another folder and at the
same time renaming it without the prefix suffix:

Dim WshShell, fso, fn, drv, dst, src
set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
fn=InputBox("Enter folder name from. ","Folder Move")
If Not Trim(fn) = "" then
For drv = Asc("C") to Asc("Z")
src=Chr(drv)+":\backup\ab"+fn+"12"
If fso.FolderExists(src) Then
dst = Chr(drv)+":\work\"+fn
fso.MoveFolder src, dst
End If
Next
End If

I don't really like it in vbs because it always brings up a Norton Av
alert even though it is innocent vbs. Anyway, it can be made the same
in VB?


I would avoid using FSO in VB projects like the plague (you can Google the
newsgroups to find out why; this subject has been beaten to death in many
previous newsgroup posts). VB has a built-in Name...As statement that can
move a file around. Consider this (off the top of my head) code sample:

Path = "d:\SomeDirectory\backup\"
NewPath = "d:\SomeDirectory\work\"
Prefix = "ab"
FileName = "RICK"
Suffix = "12"
FileType = ".txt"
Name Path & Prefix & FileName & Suffix & _
FileType As NewPath & FileName & FileType

Rick - MVP

Well almost there now with your impressive top of the head code - only
problem is it the drive letter has to be specified for the backup
folder (in the vbs original, the backup folder is found on no matter
what drive it is on)
Anyway, with drive letter, this is what I came up with so far:

FolderName = InputBox("Enter Folder Name")
Path = "d\backup\"
NewPath = "d:\work\"
Prefix = "ab"
Suffix = "12"
Name Path & Prefix & FolderName & Suffix & _
FileType As NewPath & FolderName & FileType

See any errors there? It seems to work just fine with the drive letter
specified but how would I make it like the vbs one so that the backup
folder would be found on whatever drive it is on? And the vbs also
has "On Error Resume Next"

ie
On Error Resume Next
fn=InputBox("Enter folder name from. ","Folder Move")
For drv = Asc("C") to Asc("Z")
src=Chr(drv)+":\backup\ab"+fn+"12"
If fso.FolderExists(src) Then
dst = Chr(drv)+":\work\"+fn
fso.MoveFolder src, dst

Thanks so much - I have learned something new. I always learn best
from examples.
And also thanks for the warning about the network drives - I'll have
to look into that more. Got no idea how to use GetDriveType at this
time.

cheers!
jenny
Jul 17 '05 #7

P: n/a
Great Rick! - that worked and creating the folder if not exist
eliminates the need to create a folder on each drive! (my original
post) but one thing I can't get now is message boxes to confirm
Success or Error. I tried different ways with no success: ie:

FolderName = InputBox("Enter Folder Name")
Path = ":\backup\"
NewPath = ":\work\"
Prefix = "ab"
Suffix = "12"
On Error Resume Next
For X = Asc("c") To Asc("z")
MkDir Chr$(X) & ":\Restored"
Name Chr$(X) & Path & Prefix & FolderName & _
Suffix & FileType As _
Chr$(X) & NewPath & _
FolderName & FileType
MsgBox "Success"
End
Next
MsgBox "Error"
End Sub

Obviously I'm missing something - in the above I assumed that "On
Error Resume Next" means that if there's an error - ie folder doesn't
exist - the code would be skipped to after Next but i guess not.

Re - the cheesy Input box - It's all I know now(I have the Idiots
Guide to VB on order lol) plus the code is just a workaround to what
I really think it should be able to do and that is to move any folder
back to it's original location
What I have now is a simple Browse for Folder dialogue where you
select a folder, click Ok, and it gets moved to to the folder Backup
on the ssame drive. But there is no opposite code to restore one of
those moved folders back to their original location. Ideally, when a
folder name is entered in the Input box, that folder would be moved
back to where it came from. But that is such a stumper, that I am
using this learning workaround code of just making the folder move
outside the backup folder to another folder. And that is why I used a
prefix suffix - so it would be easier to identify the folder to move
(the prefix -suffix is automatically added when the folder is
originally moved)
The closest answer I got to solving such a problem was vague -
something about creating a log file holding the original paths and
then using the same move code to move any folder back to where it was.
Do you have a clue to how to do that and would it work?
If this is something you think you can figure and would be willing to
try, I can post the move code. But regardless, you've been a great
help already.
thank you again
jenny

"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message news:<zY********************@comcast.com>...
Well almost there now with your impressive top of the head code - only
problem is it the drive letter has to be specified for the backup
folder (in the vbs original, the backup folder is found on no matter
what drive it is on)
Anyway, with drive letter, this is what I came up with so far:

FolderName = InputBox("Enter Folder Name")
Path = "d\backup\"
NewPath = "d:\work\"
Prefix = "ab"
Suffix = "12"
Name Path & Prefix & FolderName & Suffix & _
FileType As NewPath & FolderName & FileType

See any errors there?


You're missing the colon in the assignment to the Path variable

It seems to work just fine with the drive letter
specified but how would I make it like the vbs one so that the backup
folder would be found on whatever drive it is on? And the vbs also
has "On Error Resume Next"

ie
On Error Resume Next
fn=InputBox("Enter folder name from. ","Folder Move")
For drv = Asc("C") to Asc("Z")
src=Chr(drv)+":\backup\ab"+fn+"12"
If fso.FolderExists(src) Then
dst = Chr(drv)+":\work\"+fn
fso.MoveFolder src, dst


At this point, I'm not sure what you have and what you don't have. Here's
some code that I think does what you want... it assumes the backup directory
already exists, it creates the work directory if it doesn't exist (even if
there is no backup directory on the drive), and it moves the file meeting
the filename "shape" from the backup directory to the work directory.

FolderName = InputBox("Enter Folder Name")
Path = ":\backup\"
NewPath = ":\work\"
FileType = ".txt"
Prefix = "ab"
Suffix = "12"
On Error Resume Next
For X = Asc("c") To Asc("z")
MkDir Chr$(X) & ":\work"
Name Chr$(X) & Path & Prefix & FolderName & _
Suffix & FileType As _
Chr$(X) & NewPath & _
FolderName & FileType
Next

A quick comment... I used the InputBox function because you did in your
sample; however, in a real-life project, I wouldn't (it looks too cheesy for
my tastes). Instead, I would add a new form to the project to serve as an
input dialog box, I would design it to look/work nicer than the InputBox
function using a larger, nicer font, some error handling code and so on. It
would have a Label for the message, a TextBox to accept input and probably
an icon of some sort.

Thanks so much


You're welcome.

I have learned something new.


Great!

And also thanks for the warning about the network drives - I'll have
to look into that more. Got no idea how to use GetDriveType at this
time.


Let us know if this is a problem and some one here will try and help out.
Rick - MVP

Jul 17 '05 #8

P: n/a
Success or Error of what? We know there were errors for each drive that
didn't exist? There would also be errors if the Work directory already
exists. There are errors all over the place, hence the use of the

On Error Resume Next

statement. I'm guessing there would even be an error if one of the backup
files was opened and in use (it wouldn't be moved). The point is, we aren't
tracking each error... there would be too many of them.

Rick
"jenny" <je**@onlink.net> wrote in message
news:3d**************************@posting.google.c om...
Great Rick! - that worked and creating the folder if not exist
eliminates the need to create a folder on each drive! (my original
post) but one thing I can't get now is message boxes to confirm
Success or Error. I tried different ways with no success: ie:

FolderName = InputBox("Enter Folder Name")
Path = ":\backup\"
NewPath = ":\work\"
Prefix = "ab"
Suffix = "12"
On Error Resume Next
For X = Asc("c") To Asc("z")
MkDir Chr$(X) & ":\Restored"
Name Chr$(X) & Path & Prefix & FolderName & _
Suffix & FileType As _
Chr$(X) & NewPath & _
FolderName & FileType
MsgBox "Success"
End
Next
MsgBox "Error"
End Sub

Obviously I'm missing something - in the above I assumed that "On
Error Resume Next" means that if there's an error - ie folder doesn't
exist - the code would be skipped to after Next but i guess not.

Re - the cheesy Input box - It's all I know now(I have the Idiots
Guide to VB on order lol) plus the code is just a workaround to what
I really think it should be able to do and that is to move any folder
back to it's original location
What I have now is a simple Browse for Folder dialogue where you
select a folder, click Ok, and it gets moved to to the folder Backup
on the ssame drive. But there is no opposite code to restore one of
those moved folders back to their original location. Ideally, when a
folder name is entered in the Input box, that folder would be moved
back to where it came from. But that is such a stumper, that I am
using this learning workaround code of just making the folder move
outside the backup folder to another folder. And that is why I used a
prefix suffix - so it would be easier to identify the folder to move
(the prefix -suffix is automatically added when the folder is
originally moved)
The closest answer I got to solving such a problem was vague -
something about creating a log file holding the original paths and
then using the same move code to move any folder back to where it was.
Do you have a clue to how to do that and would it work?
If this is something you think you can figure and would be willing to
try, I can post the move code. But regardless, you've been a great
help already.
thank you again
jenny

"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message

news:<zY********************@comcast.com>...
Well almost there now with your impressive top of the head code - only
problem is it the drive letter has to be specified for the backup
folder (in the vbs original, the backup folder is found on no matter
what drive it is on)
Anyway, with drive letter, this is what I came up with so far:

FolderName = InputBox("Enter Folder Name")
Path = "d\backup\"
NewPath = "d:\work\"
Prefix = "ab"
Suffix = "12"
Name Path & Prefix & FolderName & Suffix & _
FileType As NewPath & FolderName & FileType

See any errors there?


You're missing the colon in the assignment to the Path variable

It seems to work just fine with the drive letter
specified but how would I make it like the vbs one so that the backup
folder would be found on whatever drive it is on? And the vbs also
has "On Error Resume Next"

ie
On Error Resume Next
fn=InputBox("Enter folder name from. ","Folder Move")
For drv = Asc("C") to Asc("Z")
src=Chr(drv)+":\backup\ab"+fn+"12"
If fso.FolderExists(src) Then
dst = Chr(drv)+":\work\"+fn
fso.MoveFolder src, dst


At this point, I'm not sure what you have and what you don't have. Here's some code that I think does what you want... it assumes the backup directory already exists, it creates the work directory if it doesn't exist (even if there is no backup directory on the drive), and it moves the file meeting the filename "shape" from the backup directory to the work directory.

FolderName = InputBox("Enter Folder Name")
Path = ":\backup\"
NewPath = ":\work\"
FileType = ".txt"
Prefix = "ab"
Suffix = "12"
On Error Resume Next
For X = Asc("c") To Asc("z")
MkDir Chr$(X) & ":\work"
Name Chr$(X) & Path & Prefix & FolderName & _
Suffix & FileType As _
Chr$(X) & NewPath & _
FolderName & FileType
Next

A quick comment... I used the InputBox function because you did in your
sample; however, in a real-life project, I wouldn't (it looks too cheesy for my tastes). Instead, I would add a new form to the project to serve as an input dialog box, I would design it to look/work nicer than the InputBox
function using a larger, nicer font, some error handling code and so on. It would have a Label for the message, a TextBox to accept input and probably an icon of some sort.

Thanks so much


You're welcome.

I have learned something new.


Great!

And also thanks for the warning about the network drives - I'll have
to look into that more. Got no idea how to use GetDriveType at this
time.


Let us know if this is a problem and some one here will try and help out.

Rick - MVP

Jul 17 '05 #9

P: n/a
Sorry, I meant success or error in entering a valid folder name like
in the vbs code I had - I was able to add a msgbox
confirming success that the folder name entered existed and if not it
would
go to the error message.
ie
<snip>
On Error Resume Next
fn=InputBox("Enter folder name")
If Not Trim(fn) = "" then
For drv = Asc("C") to Asc("Z")
src=Chr(drv)+":\recycled\recyclebin\abc"+fn+"123"
If fso.FolderExists(src) Then
dst = Chr(drv)+":\restored\"+fn
fso.MoveFolder src, dst
msgbox "Success"
End If
Next
msgbox "Error - try again"
End If

And if nothing was entered in the box, nothing would happen.
So it worked perfectly but I just didn't like it in vbs
Would this be more complicated to do in VB?

jenny


"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message news:<yL********************@comcast.com>...
Success or Error of what? We know there were errors for each drive that
didn't exist? There would also be errors if the Work directory already
exists. There are errors all over the place, hence the use of the

On Error Resume Next

statement. I'm guessing there would even be an error if one of the backup
files was opened and in use (it wouldn't be moved). The point is, we aren't
tracking each error... there would be too many of them.

Rick
"jenny" <je**@onlink.net> wrote in message
news:3d**************************@posting.google.c om...
Great Rick! - that worked and creating the folder if not exist
eliminates the need to create a folder on each drive! (my original
post) but one thing I can't get now is message boxes to confirm
Success or Error. I tried different ways with no success: ie:

FolderName = InputBox("Enter Folder Name")
Path = ":\backup\"
NewPath = ":\work\"
Prefix = "ab"
Suffix = "12"
On Error Resume Next
For X = Asc("c") To Asc("z")
MkDir Chr$(X) & ":\Restored"
Name Chr$(X) & Path & Prefix & FolderName & _
Suffix & FileType As _
Chr$(X) & NewPath & _
FolderName & FileType
MsgBox "Success"
End
Next
MsgBox "Error"
End Sub

Obviously I'm missing something - in the above I assumed that "On
Error Resume Next" means that if there's an error - ie folder doesn't
exist - the code would be skipped to after Next but i guess not.

Re - the cheesy Input box - It's all I know now(I have the Idiots
Guide to VB on order lol) plus the code is just a workaround to what
I really think it should be able to do and that is to move any folder
back to it's original location
What I have now is a simple Browse for Folder dialogue where you
select a folder, click Ok, and it gets moved to to the folder Backup
on the ssame drive. But there is no opposite code to restore one of
those moved folders back to their original location. Ideally, when a
folder name is entered in the Input box, that folder would be moved
back to where it came from. But that is such a stumper, that I am
using this learning workaround code of just making the folder move
outside the backup folder to another folder. And that is why I used a
prefix suffix - so it would be easier to identify the folder to move
(the prefix -suffix is automatically added when the folder is
originally moved)
The closest answer I got to solving such a problem was vague -
something about creating a log file holding the original paths and
then using the same move code to move any folder back to where it was.
Do you have a clue to how to do that and would it work?
If this is something you think you can figure and would be willing to
try, I can post the move code. But regardless, you've been a great
help already.
thank you again
jenny

"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message

news:<zY********************@comcast.com>...
> Well almost there now with your impressive top of the head code - only
> problem is it the drive letter has to be specified for the backup
> folder (in the vbs original, the backup folder is found on no matter
> what drive it is on)
> Anyway, with drive letter, this is what I came up with so far:
>
> FolderName = InputBox("Enter Folder Name")
> Path = "d\backup\"
> NewPath = "d:\work\"
> Prefix = "ab"
> Suffix = "12"
> Name Path & Prefix & FolderName & Suffix & _
> FileType As NewPath & FolderName & FileType
>
> See any errors there?

You're missing the colon in the assignment to the Path variable
> It seems to work just fine with the drive letter
> specified but how would I make it like the vbs one so that the backup
> folder would be found on whatever drive it is on? And the vbs also
> has "On Error Resume Next"
>
> ie
> On Error Resume Next
> fn=InputBox("Enter folder name from. ","Folder Move")
> For drv = Asc("C") to Asc("Z")
> src=Chr(drv)+":\backup\ab"+fn+"12"
> If fso.FolderExists(src) Then
> dst = Chr(drv)+":\work\"+fn
> fso.MoveFolder src, dst

At this point, I'm not sure what you have and what you don't have. Here's some code that I think does what you want... it assumes the backup directory already exists, it creates the work directory if it doesn't exist (even if there is no backup directory on the drive), and it moves the file meeting the filename "shape" from the backup directory to the work directory.

FolderName = InputBox("Enter Folder Name")
Path = ":\backup\"
NewPath = ":\work\"
FileType = ".txt"
Prefix = "ab"
Suffix = "12"
On Error Resume Next
For X = Asc("c") To Asc("z")
MkDir Chr$(X) & ":\work"
Name Chr$(X) & Path & Prefix & FolderName & _
Suffix & FileType As _
Chr$(X) & NewPath & _
FolderName & FileType
Next

A quick comment... I used the InputBox function because you did in your
sample; however, in a real-life project, I wouldn't (it looks too cheesy for my tastes). Instead, I would add a new form to the project to serve as an input dialog box, I would design it to look/work nicer than the InputBox
function using a larger, nicer font, some error handling code and so on. It would have a Label for the message, a TextBox to accept input and probably an icon of some sort.
> Thanks so much

You're welcome.
> I have learned something new.

Great!
> And also thanks for the warning about the network drives - I'll have
> to look into that more. Got no idea how to use GetDriveType at this
> time.

Let us know if this is a problem and some one here will try and help out.

Rick - MVP

Jul 17 '05 #10

P: n/a
> Sorry, I meant success or error in entering a valid folder name like
in the vbs code I had - I was able to add a msgbox
confirming success that the folder name entered existed and if not it
would
go to the error message.
ie
<snip>
On Error Resume Next
fn=InputBox("Enter folder name")
If Not Trim(fn) = "" then
For drv = Asc("C") to Asc("Z")
src=Chr(drv)+":\recycled\recyclebin\abc"+fn+"123"
If fso.FolderExists(src) Then
dst = Chr(drv)+":\restored\"+fn
fso.MoveFolder src, dst
msgbox "Success"
End If
Next
msgbox "Error - try again"
End If


I think you are after something like the following (change to my last posted
code):

FolderName = InputBox("Enter Folder Name")
If Len(Trim$(FolderName)) Then
Path = ":\backup\"
NewPath = ":\work\"
FileType = ".txt"
Prefix = "ab"
Suffix = "12"
On Error Resume Next
For X = Asc("c") To Asc("z")
MkDir Chr$(X) & ":\work"
Name Chr$(X) & Path & Prefix & FolderName & _
Suffix & FileType As _
Chr$(X) & NewPath & _
FolderName & FileType
Next
MsgBox "Success!", vbExclamation, "Valid Entry"
Else
MsgBox "You didn't enter anything!", _
vbCritical, "Blank Entry"
End If

Note that the If-Then statement is testing for a non-entry. The Trim$
function was used to make sure the user didn't accidentally hit the space
bar one or more times. While I could have made the test this way

If Trim$(FolderName) = "" Then

the method I used (using the Len function) is, in fact, a faster testing
method. Since VB evaluates logical operators as being either 0, which is
identically what False is, or not 0 (that is, any number but zero), which is
what VB considers to be True. Since the Len("") = 0, then if the
Len(Trim$(FolderName)) is not zero, VB will consider it as True (which is
why I don't perform the redundant test of >0).

Rick - MVP
Jul 17 '05 #11

P: n/a
On 7 Aug 2003 02:14:00 -0700, Cr********@hotmail.com (Geoff) wrote:
You can check with: Dir(Path & FolderName,vbDirectory)
It will return the folders name if it exists, if not it will return a blank string.

Best NOT use Dir()

' ---
Function FileExists(Fle$) As Boolean
Dim Q%
On Error Resume Next
Q = GetAttr(Fle$)
If Err = 0 Then
If (Q And vbDirectory) = 0 Then
FileExists = True
End If
End If
Err.Clear
End Function

' ---
Function DirExists(ADir$) As Boolean
Dim Q%
On Error Resume Next
Q = GetAttr(ADir$)
If Err = 0 Then
If (Q And vbDirectory) = vbDirectory Then
DirExists = True
End If
End If
Err.Clear
End Function
'
################################################## #######################
'
' C:\DEV\USLIB\USLIB.BAS --> X:\DEV\USLIB
'
Function ExtractFilePath$(Fle$)

Dim L9%

For L9 = Len(Fle$) To 1 Step -1
If InStr(":\", Mid$(Fle$, L9, 1)) Then
If Mid$(Fle$, L9, 1) = "\" Then
ExtractFilePath$ = Left$(Fle$, L9 - 1)
End If
If Mid$(Fle$, L9, 1) = ":" Then
ExtractFilePath$ = Left$(Fle$, L9)
End If
L9 = 1
End If
Next

End Function
Sub MakeDir(FileSpec$, Erm$)
Dim S$

Erm$ = ""
If InStr(FileSpec$, "\") Then
S$ = ExtractFilePath(FileSpec$)
Call MakeDir(S$, Erm$)
If Len(Erm$) = 0 Then
If DirExists(FileSpec$) = False Then
On Error Resume Next
MkDir FileSpec$
If Err Then Erm$ = "Error Making " + FileSpec$
On Error GoTo 0
End If
End If
End If
End Sub

Jul 17 '05 #12

P: n/a
On 6 Aug 2003 05:00:06 -0700, je**@onlink.net (jenny) wrote:
Great Rick! - that worked and creating the folder if not exist
eliminates the need to create a folder on each drive! (my original
post) but one thing I can't get now is message boxes to confirm
Success or Error. I tried different ways with no success: ie:

FolderName = InputBox("Enter Folder Name")
Path = ":\backup\"
NewPath = ":\work\"
Prefix = "ab"
Suffix = "12"
On Error Resume Next
For X = Asc("c") To Asc("z")
MkDir Chr$(X) & ":\Restored"
Name Chr$(X) & Path & Prefix & FolderName & _
Suffix & FileType As _
Chr$(X) & NewPath & _
FolderName & FileType
MsgBox "Success"
End
Next
MsgBox "Error"
End Sub

Obviously I'm missing something - in the above I assumed that "On
Error Resume Next" means that if there's an error - ie folder doesn't
exist - the code would be skipped to after Next but i guess not.


"On Error Resume Next" will skip to the next _line_ if an error is
encountered. It doesn't skip to the "Next" statement.

If you want to skip to after "Next" try the following

On Error Goto AfterNext
For X = Asc(...

Next
AfterNext: 'Note the trailing colon
Jul 17 '05 #13

P: n/a
Thanks to all - I got it working now so cancel does nothing, right
folder name works, and wrong folder name doesn't.
Something else I can't figure though but i'll make that a new post.

thanks again
jenn

Ben Hall <be*******@yahoo.com.au> wrote in message news:<4v********************************@4ax.com>. ..
On 6 Aug 2003 05:00:06 -0700, je**@onlink.net (jenny) wrote:
"On Error Resume Next" will skip to the next _line_ if an error is
encountered. It doesn't skip to the "Next" statement.

If you want to skip to after "Next" try the following

On Error Goto AfterNext
For X = Asc(...

Next
AfterNext: 'Note the trailing colon

Jul 17 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.