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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a folder of the same name as filename

Status
Not open for further replies.

electricwizard84

Technical User
Feb 18, 2011
17
0
0
AU
Hi,

I am trying to create a folder based upon the base name of a filename. For example, if a file is named 'test.txt' then the program will create a folder called 'test'.

Additionally, the filename 'test.txt' shall be moved such that is become inside the folder 'test'. Also, the folder 'test' shall then be moved to a destination path.

-----------------------------------------------------
dim objFSO, objFolder, b
dim colFiles, source, destination

source = "C:\Test\"
destination = "C:\Test1\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(source)
Set colFiles = objFolder.Files

For Each objFile In colFiles

b = objFSO.GetBaseName ("C:\Test\test.txt") 'cannot the string b store a value called 'test', from the files within a folder as opposed to having to write the extension explicitly?

objFSO.CreateFolder(b)

objFile.Move destination & "\" & objFile.Name ' I want to move the file to within the new folder.

Next


 
At least, replace this:
b = objFSO.GetBaseName ("C:\Test\test.txt")
with this:
b = objFSO.GetBaseName (objFile.Name)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Your destination path doesn't exist because it contains an additional slash.

objFile.Move "C:\Test1\\test.txt"

- 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
 
Hi all,

Appreciate your comments. The problem is that i'm trying to create a folder with the same name as the filename, but created in a particular destination.

Using the updated code, the folder is always created on the desktop and not in "C:\test\".

-------------------------
dim objFSO, objFolder, b, folder
dim colFiles, source, destination

source = "C:\Test\"
destination = "C:\Test1\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(source)
Set colFiles = objFolder.Files

For Each objFile In colFiles

b = objFSO.GetBaseName (objFile.Name)
objFSO.CreateFolder(b)

'folder.MoveFolder "C:\Users\m.lee\Desktop\", source

'objFile.Move week1 & "\" & objFile.Name


Next
 
Replace this:
objFSO.CreateFolder(b)
with this:
objFSO.CreateFolder source & b

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks for this PHV. The last thing i'm trying to do is to move the newly created folder (with file inside) to a different directory. This seems very basic to do however, i get an error reporting 'Path Not Found'. I'm not sure whether it is because of the '\', but i want the program to move all folders in "C:\Test\" to the new directory.

-----------------------------------------------------

dim objFSO, objFolder, b, c
dim colFiles, source, destination

source = "C:\Test\"
destination = "C:\Test1\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(source)
Set colFiles = objFolder.Files

For Each objFile In colFiles

b = objFSO.GetBaseName (objFile.Name) 'get 'text' instead of 'text.txt'
c = objFSO.CreateFolder(source & b) 'create folder of same filename in same location as source directory
objFile.Move c & "\" & objFile.Name 'move file to within new folder

objFSO.MoveFolder source, destination

Next
 
It's all fixed - it was a simple matter of ensuring i did not call a variable before i actually stored data in it.

Final code:

---------------------------


dim objFSO, objFolder, b, c, newfolder
dim colFiles, source, destination

source = "C:\Test\"
destination = "C:\Test1\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(source)
Set colFiles = objFolder.Files

For Each objFile In colFiles

b = objFSO.GetBaseName (objFile.Name) 'get 'text' instead of 'text.txt'
c = objFSO.CreateFolder(source & b) 'create folder of same filename in same location as source directory
objFile.Move c & "\" & objFile.Name 'move file to within new folder

set newfolder = objFSO.GetFolder(source & b)
newfolder.Move(destination)

Next
 
Guys,

Another problem i need your help with! I tested the code at work today, moving some text files around and it worked well, without any problems. However, at home i am trying to move some movie files (movie.avi) onto a network drive and at line 18 newfolder.Move(destination), i receive a permission error. I've used other scripts in the past to move picture files to the network drive, which is why i am thinking there must be something wrong with my script. Any ideas would be appreciated!

----------------------

dim objFSO, objFolder, b, c, newfolder
dim colFiles, source, destination

source = "C:\Users\Matthew\Desktop\Test\"
destination = "Z:\MyVideos\Movies\TV\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(source)
Set colFiles = objFolder.Files

For Each objFile In colFiles

b = objFSO.GetBaseName (objFile.Name) 'get 'text' instead of 'text.txt'
c = objFSO.CreateFolder(source & b) 'create folder of same filename in same location as source directory
objFile.Move c & "\" & objFile.Name 'move file to within new folder

set newfolder = objFSO.GetFolder(source & b) 'get all folders located in "source\" directory
newfolder.Move(destination) 'move all folders to new destination

Next
 
make sure the destination directory exists before moving

Code:
if NOT (objFSO.FolderExists(destination)) then
	objFSO.CreateFolder destination
end if

Put this code before the for..next. Once I added it, the script ran correctly

-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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top