PDA

View Full Version : repetitive copy and paste (W2K)



boxenberg
05-05-2004, 09:17 PM
Hi,
Here is my scenario...I have a bunch of folders in a directory, each named according to the user's logon id appended by random numbers. For example, usera1234, userb5678, userc9012, and so forth. I also have a file in this directory, let's call it somefile.txt. I need an automated process where it will iterate through each user folder looking for a specific file, let's call it target.txt (the file name is exactly the same in each user's folder), open that file, copy the text contained within that file (there are only three short lines of text) and then append them into somefile.txt in the parent directory mentioned before.
I would like to do this in WSH or batch file but need some help with designing the script (logic etc.) and also the syntax.
Thanks!!!
Barry

boxenberg
05-06-2004, 08:36 AM
well, I think I have some of the "logic" worked out but I need some advice. If I use the FileSystemObject.GetFolder method to capture the folder names, how can I pass those folder names to a subsequent portion of the script to direct the OpenFile (for reading and writing etc.)???
Thanks,
Barry

dineshcooper
05-06-2004, 10:56 AM
So the code at the bottom would retrieve all the
subfolders in the somedir directory now you have all these in the collection colSubfolders.

(The code below is going to output the subfolder names, but you can now add your code in the for loop to do whatever action)


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\somedir")
Set colSubfolders = objFolder.Subfolders
For Each objSubfolder in colSubfolders
Wscript.Echo objSubfolder.Name, objSubfolder.Size
Next

Hope that helps

Brf
05-06-2004, 11:39 AM
That only goes one layer deep.... I wrote a script for someone that does all the subdirectories in this thread: <a target="_blank" href=http://www.winguides.com/forums/showflat.php?Cat=&Board=brdScripting&Number=111395&page=0&view=collapsed&sb=5&part=>http://www.winguides.com/forums/showflat.php?Cat=&Board=brdScripting&Number=111395&page=0&view=collapsed&sb=5&part=</a>

dineshcooper
05-06-2004, 12:53 PM
I don't deny that, although the requirement given in the intital post only asks for a 1 level deep solution.

boxenberg
05-06-2004, 02:24 PM
Hi Dinesh!
Thank you so much for your reply. Obviously I am a total rookie at doing this so please bear with me.
I am using the code exactly the way you suggested and here is what I am running into:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\somedir")
Set colSubfolders = objFolder.Subfolders
For Each objSubfolder in colSubfolders
'Here is where I need to open a text file
'contained within the current subfolder
'so I tried it like this
set objTextFile = objFSO.OpenTextFile("evpm.ini", ForReading)
'This does not work and I get an error
'message indicating the file was not found :-(
Next

dineshcooper
05-06-2004, 04:30 PM
The code below should give you what you looking
for.

I have added quite a lot of comments but if
you need any detailed explanation of some statement, don't hesitate to ask.....

Now on to the fun stuff
(Now dont forget you need to change the values
of the variables:

strMainDir = "C:\somedir"
strINIFile = "target.txt"
strSomeFile = strMainDir & "\somefile.txt"

to suit your folder structure.
)
******************************************

Dim strMainDir
Dim strINIFile
Dim strSomeFile
Dim strData

const ForReading = 1
const ForWriting = 2
const ForAppending = 8

strMainDir = "C:\somedir"
strINIFile = "target.txt"
strSomeFile = strMainDir & "\somefile.txt"

'Get a filesystemobject
Set objFSO = CreateObject("Scripting.FileSystemObject")

'get a handle to the folder we using (Note I changed the hardcoded paramter to a variable)
Set objFolder = objFSO.GetFolder(strMainDir)

'obtain collection containing all subfolders
Set colSubfolders = objFolder.Subfolders


'Check if somefile.txt exists if it does then open for appending
'if it doesnt create it and open it for writing

If (objFSO.FileExists(strSomeFile)) Then
Set objSomeFile = objFSO.OpenTextFile(strSomeFile, ForAppending)
Else
Set objSomeFile = objFSO.OpenTextFile(strSomeFile, ForWriting)
End If


'loop through subfolders
For Each objSubfolder in colSubfolders

'!!! The problem here is that you specified the file without adding
'!!! the path of the subfolder, therefore the call to OpenTextFile
'!!! will look in the same dir as the script for that file and probably
'!!! wont find it there. Now check the changes I made

'So open the file (Note: we opening the evpm.ini file in the subfolder)
Set objTextFile = objFSO.OpenTextFile(strMainDir & "\" & objSubfolder.Name & "\" & strINIFile, ForReading)
'Read all its data
strData = objTextFile.ReadAll()
'append the data to the file somefile.txt (Note: we are writing to somefile.txt)
objSomeFile.WriteLine strData
'Now Close this evpm.ini file
objTextFile.Close

Next

'Now that we done appending to the somefile.txt we close it too
objSomeFile.Close

'If there is nothing else to do exit the script
WScript.Quit

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

<P ID="edit"><FONT class="small">Edited by dineshcooper on 05/06/04 15:37.</FONT></P>

dineshcooper
05-07-2004, 02:29 AM
Yeah my logic was a bit out and confusing there, i apologise about that, but in anycase here's the updated version of the script.

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

Dim strMainDir
Dim strINIFile
Dim strSomeFile
Dim strData

const ForReading = 1
const ForWriting = 2
const ForAppending = 8

strMainDir = "c:\pst"

' I changed the names of the variable to help out with the explanation a bit
' so we have the read file, all we know about the read file is that it resides
' in the subfolders and we dont yet know the names of the subfolders, so we
' store just the file name
strReadFile = "evpm.ini"

' the append file being the one in the pst folder that we copying the data too
strAppendFile = strMainDir & "\migrateM.ini"


'Get a filesystemobject
Set objFSO = CreateObject("Scripting.FileSystemObject")

'get a handle to the folder we using (Note I changed the hardcoded paramter to a
'variable)
Set objFolder = objFSO.GetFolder(strMainDir)

'obtain collection containing all subfolders
Set colSubfolders = objFolder.Subfolders


'Check if Appendfile exists if it does then open for appending
'if it doesnt create it and open it for writing

If (objFSO.FileExists(strAppendFile)) Then
Set objAppendFile = objFSO.OpenTextFile(strAppendFile, ForAppending)
Else
Set objAppendFile = objFSO.OpenTextFile(strAppendFile, ForWriting)
End If


'loop through subfolders
For Each objSubfolder in colSubfolders


'!!! The problem here is that you specified the file without adding
'!!! the path of the subfolder, therefore the call to OpenTextFile
'!!! will look in the same dir as the script for that file and probably
'!!! wont find it there. Now check the changes I made

'So open the file (Note: we opening the evpm.ini file in the subfolder)
Set objReadFile = objFSO.OpenTextFile(strMainDir & "\" & objsubfolder.Name & "\" & strReadFile, ForReading)

'Read a line
strData = objReadFile.ReadAll()

'append the line
objAppendFile.WriteLine strData


'Done Reading now Close this evpm.ini file
objReadFile.Close

Next

'Now that we done appending to the AppendFile we close it too
objAppendFile.Close

'If there is nothing else to do exit the script
WScript.Quit
*****************************************