Hello! This is the code:
Dim oFSO, folder, folder2, coll
folder = "Test"
folder2 = "Test1"
Set oFSO = CreateObject("Scripting.FilesystemObject")
oFSO.CreateFolder ("C:Documents and SettingsAdministratorDesktop" &folder)
Set path = oFSO.GetFolder ("C:Documents and SettingsAdministratorDesktop" & folder)
Set coll = path.SubFolders
coll.Add (folder2)
How can I create new text file inside folder2 (Test1) subfolder but specifying a name from variable?
For example. If I add this line of code to the end:
Set Test1 = oFSO.GetFolder (coll ("Test1"))
Test1.CreateTextFile "Example.txt"
it will create Example.txt file. But how can I replace "Test1" with variable name "folder2" so I can grab folder specified in variable?
Call subfolder from variable
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
- tech_soul8
- Posts: 26
- Last visit: Sat Mar 08, 2014 12:14 pm
Call subfolder from variable
"You need to start by studying the basics of VBScript. You are mising soem very fundamental bits of knowledge."
Yes, I know that. I just started to learn scripting so stupid questions like this are expected.
What I'm trying to achive is next. In real script I'm trying to write, name for "folder" and "folder2" won't be
contained in predefined variable as string. It will be assigned to a variable as scripts executes. More precisely "folder" name would be made up of Year (now) function, and "folder2" of Month (now) function.
So I'm trying to find a way how to move some files another location to that folders after they are created and how can I reference to that folders in:
oFSO.Movefile () statement.
Maybe I should mention that wright from the beginning. I hope so you understand me and that's the reason why it is not acceptable to me to grab those newly created folders like this:
Set Test1 = oFSO.GetFolder (coll ("Test1"))
becouse I'm not going to change my script every year or month.
Yes, I know that. I just started to learn scripting so stupid questions like this are expected.
What I'm trying to achive is next. In real script I'm trying to write, name for "folder" and "folder2" won't be
contained in predefined variable as string. It will be assigned to a variable as scripts executes. More precisely "folder" name would be made up of Year (now) function, and "folder2" of Month (now) function.
So I'm trying to find a way how to move some files another location to that folders after they are created and how can I reference to that folders in:
oFSO.Movefile () statement.
Maybe I should mention that wright from the beginning. I hope so you understand me and that's the reason why it is not acceptable to me to grab those newly created folders like this:
Set Test1 = oFSO.GetFolder (coll ("Test1"))
becouse I'm not going to change my script every year or month.
- tech_soul8
- Posts: 26
- Last visit: Sat Mar 08, 2014 12:14 pm
Call subfolder from variable
You asked how to specify a folder in a variable. Her eis the answer.
No, I understand that. What I was trying to ask is how can I call subfolder from a variable?? I completely understand your example code. I'm going to explain that to you again.
For example let's say that I want to create two folders. First folder would be named based on the current year (2012), second folder name would be assigned based on the current month (lets say April (4)). Second folder would be created inside first folder so it will be subfolder of 2012 folder. And finally let's say that I want to move some files from "C:Test" to second folder.
So here is the code:
Dim oFSO, folder1, folder2, pathf1, collf1
folder1 = Year (Now)
folder2 = Month (Now)
Set oFSO = CreateObject ("Scripting.FileSystemObject")
oFSO.CreateFolder ("C:Documents and settingsAdministratorDesktop" & folder1)
Set PathF1 = oFSO.GetFolder ("C:Documents and settingsAdministratorDesktop" & folder1)
Set CollF1 = PathF1.Subfolders
CollF1.Add (folder2) & "." & "Month"
So far we have created two new folders. On Desktop Folder1 "2012" and Folder2 "4.Month" as subfolder.
Now I have bunch of files in "C:Test" that I want to move to folder2 (4.Month).
Add the necessary code to the end please tech_soul82012-04-24 11:40:15
- tech_soul8
- Posts: 26
- Last visit: Sat Mar 08, 2014 12:14 pm
Call subfolder from variable
We can re-write it to get it RIGHT ok Mr. Wright.
Again typo mistake From now on I'm sure I'll get it RIGHT!
But it can happen to anyone
You also missed the answer that I posted and that was described more completely in the lonks I posted.
[ tech_soul82012-04-24 12:30:44
Call subfolder from variable
Unfortuantely you are trying to do things that make no sense.
This construct is wrong.
Set PathF2 = oFSO.GetFolder (CollF1 ("4.Month"))
You cannot address the folder collection object like an array. It is not an array it is an object collection that can only be enumerated. It also does not have an index or key so strings won't work.
I recommend downloading the PrimalScript Trial and testing your code under the debugger. It will show you how the code is failing and allow you to experiment more incrementally. You also need to read the sections on the FSO so you can learn how it works.
Learn also why we use 'Set' and when to not use it. Learn what an object is compared to a variable.
Learning that will get you back on a path that will clear upoi why your code keeps getting broken. I canmn see it in the code and mistakes. I am trying to point you to a way to fix your knowledge so yu can see how easy VBScript really is.
This construct is wrong.
Set PathF2 = oFSO.GetFolder (CollF1 ("4.Month"))
You cannot address the folder collection object like an array. It is not an array it is an object collection that can only be enumerated. It also does not have an index or key so strings won't work.
I recommend downloading the PrimalScript Trial and testing your code under the debugger. It will show you how the code is failing and allow you to experiment more incrementally. You also need to read the sections on the FSO so you can learn how it works.
Learn also why we use 'Set' and when to not use it. Learn what an object is compared to a variable.
Learning that will get you back on a path that will clear upoi why your code keeps getting broken. I canmn see it in the code and mistakes. I am trying to point you to a way to fix your knowledge so yu can see how easy VBScript really is.
Call subfolder from variable
I still do not know what it is you are trying to do. I have posted numerous bits of code and shown you what is wtong with your code. I do not understand what you are asking.
You have made folders with variables. You have moved files. What else is wrong?
You have made folders with variables. You have moved files. What else is wrong?
Call subfolder from variable
No - you still doidn't read the basics so you are trying to do spomething that I would guess you have seem somewhere else.
There is no 'Add' on the file system object.
CollF1.Add (folder2) & "." & "Month"
http://technet.microsoft.com/en-us/libr ... 98723.aspx
There are numerous examples of all of the calls along with sample scritps showing you how to use the fso with vbscript. Te left side of teh pages lists all of the tasks that can be done and links to them,
There is no 'Add' on the file system object.
CollF1.Add (folder2) & "." & "Month"
http://technet.microsoft.com/en-us/libr ... 98723.aspx
There are numerous examples of all of the calls along with sample scritps showing you how to use the fso with vbscript. Te left side of teh pages lists all of the tasks that can be done and links to them,
- tech_soul8
- Posts: 26
- Last visit: Sat Mar 08, 2014 12:14 pm
Call subfolder from variable
I found the solution. This is what I was looking for:Set PathF2 = oFSO.GetFolder (CollF1 ("" & folder2 & ""))So final code looks like this: Dim oFSO, folder1, folder2, pathf1, collf1
folder1 = Year (Now)
folder2 = Month (Now)
& "." & "Month"
Set oFSO = CreateObject ("Scripting.FileSystemObject")
oFSO.CreateFolder ("C:Documents and settingsAdministratorDesktop" & folder1)
Set PathF1 = oFSO.GetFolder ("C:Documents and settingsAdministratorDesktop" & folder1)
Set CollF1 = PathF1.Subfolders
CollF1.Add (folder2)
Set PathF2 = oFSO.GetFolder (CollF1 ("" & folder2 & ""))oFSO.MoveFile "C:Test*.*", PathF2Set oFSO = NothingSet PathF1 = NothingSet CollF1 = NothingSet PathF2 = NothingI don't know if this is right or wrong but it works. Don't get me wrong because I'm not saying that you're wrong or something like that... I'm just trying to say that this code works. Maybe this is not how things should be done so your criticism are always wellcome.
tech_soul82012-04-24 17:45:22
folder1 = Year (Now)
folder2 = Month (Now)
& "." & "Month"
Set oFSO = CreateObject ("Scripting.FileSystemObject")
oFSO.CreateFolder ("C:Documents and settingsAdministratorDesktop" & folder1)
Set PathF1 = oFSO.GetFolder ("C:Documents and settingsAdministratorDesktop" & folder1)
Set CollF1 = PathF1.Subfolders
CollF1.Add (folder2)
Set PathF2 = oFSO.GetFolder (CollF1 ("" & folder2 & ""))oFSO.MoveFile "C:Test*.*", PathF2Set oFSO = NothingSet PathF1 = NothingSet CollF1 = NothingSet PathF2 = NothingI don't know if this is right or wrong but it works. Don't get me wrong because I'm not saying that you're wrong or something like that... I'm just trying to say that this code works. Maybe this is not how things should be done so your criticism are always wellcome.
tech_soul82012-04-24 17:45:22
Call subfolder from variable
Simple Method #1
Only two small changes but the code is more readable and easier to use. It also avoids all of the path navigation.
Code: Select all
Set shell = CreateObject("WScript.Shell")
rootfolder = shell.SpecialFolders("desktop")
sFolder1 = rootfolder & "" & Year (Now)
sFolder2 = sFolder1 & "" & Month(Now) & "." & "Month"
Set fso = CreateObject ("Scripting.FileSystemObject")
If fso.FolderExists(sFolder1) Then
Set nf1 = fso.GetFolder(sFolder1)
Else
set nf1 = oFSO.CreateFolder(sFolder1)
End If
If fso.FolderExists(sFolder2) Then
Set nf2 = fso.GetFolder(sFolder2)
Else
Set nf2 = oFSO.CreateFolder(sFolder2)
End If
fso.MoveFile "e:Test2*.txt", nf2
WScript.Echo "Move Complete"
Only two small changes but the code is more readable and easier to use. It also avoids all of the path navigation.
Call subfolder from variable
I jsut remembered why we never use Subfolders("name of folder"). It is because these are old fromEarly W98 and do not work with the extended file naming. You cannot put spaces and other characters in these srtings or you will get an "Invalid Procedure Call" error. They willwork for all simple names but you don't want to use them.
"Documents And Settings" cannot be refrenced because it has spaces in it.
"Documents And Settings" cannot be refrenced because it has spaces in it.