Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB Script - Create Folder With Inputbox & Move Files/Folders Into It

Status
Not open for further replies.

asldesktop

IS-IT--Management
Apr 5, 2011
5
0
0
GB
Hi

I must point out that I am a novice when it comes to scripting - I can usually get myself through things but I am stuck with this one.

I am attempting to write a script to do the following:

1. Create a folder, the name of which is input in to an inputbox
2. Move the contents of a directory (files & subfolders) in to the newly created subfolder

Sounds simple to me. I have been able to get the part working to create a folder named from an inputbox but not the folder/file move - I get an error 800A004C "Path not found"

Here is the script I have:


************************************************

'Create the file system object for creating folders
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Create A Folder With A Name Given By The User
ParentFolder = "C:\"
set objShell = CreateObject("Shell.Application")
set objFolder = objShell.NameSpace(ParentFolder)
foldername=inputbox ("Enter Folder Name")
objFolder.NewFolder (foldername)

'Move the files and folders in to the new folder
objFSO.MoveFile "C:\logon\*.*" , "foldername"
objFSO.MoveFolder "C:\logon\*.*" , "foldername"

************************************************

Is anyone able to provide assistance so I am able to get this working please? Any help is greatly appreciated!

Thanks

Paul
 
When creating a directory, you will get an error if the parent directory does not exist. Use a simple recursive function to make sure the directory is created completely. Also, paths cannot contain a trailing "\" - this will also cause an error.

Code:
function createDirectory (strDir)
	set objFSO = CreateObject("Scripting.FileSystemObject")
	if (right(strDir, "\")) then strDir = left(strDir, len(strDir) - 1)
	strParentDir = left(strDir, inStrReV(strDir,"\")
	
	if NOT (objFSO.FolderExists(strParentDir)) then createDirectory (strParentDir)
	if NOT (objFSO.FolderExists(strDir)) objFSO.CreateFolder (strDir)
end function

implement the code above and change
Code:
objFolder.NewFolder (foldername)
to
Code:
createDirectory(foldername)
-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
Thanks for the quick respsonse. I have put your pieces in as suggested but I am getting an error:


Line: 10
Char: 5
Type mismatch: '[string: "\"]'

The script looks like this:

Code:
'Create the file system object for creating folders
Set objFSO = CreateObject("Scripting.FileSystemObject")	
    
'Create A Folder With A Name Given By The User (InputBox)
function createDirectory (strDir)
    set objFSO = CreateObject("Scripting.FileSystemObject")
    if (right(strDir, "\")) then strDir = left(strDir, len(strDir) - 1)
    strParentDir = left(strDir, inStrReV(strDir,"\"))
    
    if NOT (objFSO.FolderExists(strParentDir)) then createDirectory (strParentDir)
    if NOT (objFSO.FolderExists(strDir)) then objFSO.CreateFolder (strDir) 
end function
ParentFolder = "C:\" 
set objShell = CreateObject("Shell.Application")
set objFolder = objShell.NameSpace(ParentFolder) 
foldername=inputbox ("Enter Folder Name")
createDirectory(foldername)

objFSO.MoveFile "C:\logon\*.*" , "foldername"
objFSO.MoveFolder "C:\logon\*.*" , "foldername"

Any advice would be appreciated! :)
 
Again, paths ending with a "\" will cause and error.

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
Change:
if (right(strDir, "\")) then strDir = left(strDir, len(strDir) - 1)

To:
if right(strDir, 1) = "\" then strDir = left(strDir, len(strDir) - 1)
 
Thanks for the replies.

I have changed the script but i am still having problems.

Perhaps I didnt explain correctly in my first post though - I dont have any issues with creating the directory. The directory is created fine - but I am having problems moving files and folders in to the newly created folder (which will always have a unique name as it's decided by the user with the inputbox)

If someone can point me in the right direction to alter the script so I am able to move files/folders in to it?
 
I'd try to replace this:
objFSO.MoveFile "C:\logon\*.*" , "foldername"
objFSO.MoveFolder "C:\logon\*.*" , "foldername"
with this:
objFSO.MoveFile "C:\logon\*.*" , foldername
objFSO.MoveFolder "C:\logon\*" , foldername

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
a single DOC command thru the objShell.

Code:
set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "%COMSPEC% /c xcopy ""C:\source"" ""C:\destination"" /e /q", 0, true

- Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top