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

Script to move folders... need assistance.

Status
Not open for further replies.

sxmont

IS-IT--Management
Sep 25, 2003
46
US
Hello,

I need some help, please see below:

I have folder with about 17,000 subfolders which are case folders. They are in the following format

LASTNAME,FIRSTNAME(%CASENUMBER%)

I have to move about 7500 of these folders out of that directory and onto a different share. I have a list of the case numbers that need to be moved in a text document.

Is there anyway to create a script that can help me with this? Uggg...
 
start by reading the contents of your 'text document' into something like a dictionary object.
then do a recursive folder search (see FAQ) and in the .Folders loop check to see if the name of the folder / case exists in the dictionary, if it does, then copy / move it
 
mrmovie,

Thank you for your replace, I guess I forgot to mention that I am a novice at scripting, but I will look things up. If you provide a little more detail, that would be greatly appreciated.
 
strFile = "c:\cases.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objTS = FSO.OpenTextFile(strFile, 1, False) 'can never remember
Do While Not objTS.AtEndOfStream
strLine = ""
strLine = objTS.ReadLine
Wscript.Echo strLine
Loop
objTS.Close
Set objTS = Nothing
Set FSO = Nothing
 
Ok, But where within the script am I stating where I want to move to the folders too...
 
I guess a better way to put it, where do I specific the source and destination addresses.
 
you dont yet.
all the script i have posted 'might' do is read the contents of your c:\cases.txt file and display it on the screen, if the script is started with cscript.exe (otherwsie you will have lot of OK's to click)
have a look at the code i posted, and actually read it word for word and line by line. once you have understood what it is doing we can move onto the next part of the script.
if your intention is to learn something i would suggest this is your best approach. alternatively someone might post a whole working script for you, you will only be back in 3 months time when you want to do something new, or change some small part of it.
its up to you
 
OK, I don't understand what you mean by read the contents of my text file. My text file has a list of case numbers in a row, they were pulled from a spreadsheet column.

 
you said you have a list of case numbers in a text file.
the script you want needs to be able to read these case numbers.
the code i presented simply reads a text file line by line and displays it on the screen.

so, when you run the code presented does it list the case numbers one at a time?
 
When I run what you sent to me, what it shows is each number then I have to click OK ( of course, I only used a small sample because I would still be clicking OK... as you stated)

 
so, dont double click on the script to start it, instead open a cmd.exe window and run "cscript.exe name_of_script.vbs"
you will still get the output but you wont have to click all the time.

next, add each 'case' into a dictionary object

strFile = "c:\cases.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objTS = FSO.OpenTextFile(strFile, 1, False) 'can never remember
Do While Not objTS.AtEndOfStream
strLine = ""
strLine = objTS.ReadLine
Wscript.Echo strLine
If Not dicCases.Exists(strLine) Then
dicCases.Add strLine, ""
Else
Wscript.Echo "duplicate"
End If
Loop
objTS.Close
Set objTS = Nothing
Set FSO = Nothing
 
Here's what I came up with. Same general idea as mrmovie.

Code:
option explicit
dim objFSO
dim objInputFile
dim objLog
dim re
dim objStartFolder
dim moveList
dim colFolders
dim objSubFolder
dim strLine

Const source = "C:\TEMP\testmove"
Const destination = "C:\TEMP\testmove\testmove2"
Set objFSO = CreateObject("scripting.filesystemobject")
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set objInputFile = objFSO.GetFile("movelist.txt")
Set objLog = objFSO.CreateTextFile("foldersMoved.log")
set re = new regexp

'make sure source folder exists
If not objFSO.FolderExists(source) Then
	wscript.echo("invalid source folder")
	wscript.quit
else
	Set objStartFolder = objFSO.GetFolder(source)
end if

'make sure destination folder exists
If not objFSO.FolderExists(destination) Then
	wscript.echo("invalid destination folder")
	wscript.quit
	'or add code to create it
end if

Set moveList = objInputFile.OpenAsTextStream(ForReading)
Set colFolders = objStartFolder.SubFolders

Do Until moveList.AtEndOfStream
	strLine = moveList.Readline
	re.Pattern = "[^\d]+" & strLine & "$"
	objLog.WriteLine("re.Pattern: " & re.Pattern)
	for each objSubFolder in colFolders
		'wscript.echo("objSubFolder = " & objSubFolder.Name)
		if re.Test(objSubFolder.Name) then
			'move folder to destination
			objLog.WriteLine("moving: " & objSubFolder.Name)
			objFSO.MoveFolder objSubFolder, destination & "\"
		end if
	next
	
Loop
moveList.Close
objLog.Close
wscript.echo("Done")
 
Ok, I added this part.... Again, I apologize for not having too much knowledge with this, I will buy you lunch for your help... :)
 
To jges, I tried to run your script and in the foldersMoved.log I get this re.Pattern: [^\d]+210155$

Note: 210155 is the first case listed in my text file.
 
the way the folders are structured is the following.

doe,john_123456

Where "123456" is the case number, my text document as all of the case numbers that I want to move...

I tested the above script by just having one case number in the text document that I know exist in the directory and in the foldersMoved.log it gave me the message re.Pattern: [^\d]+212982$


 
The log file is just stuff I used for some debugging purposes.

I forgot to mention that you will have to modify the lines:
Code:
Const source = "C:\TEMP\testmove"
Const destination = "C:\TEMP\testmove\testmove2"
to reference your source and destination directories.
 
Yes, I've done that, but it is not finding the folders, Is there something that I am missing because of the folder names...
 
IT'S WORKING!

You guys are great... You ever come to Philly, I will buy you the best cheese steak this town has...

Thanks a million...
 
glad you got a result sxmont, i had to go home for my tea.
i will comment on the Do For Each Next Loop
the purpose of the dictionary object would made it more efficient, e.g at the moment for each line in the 'cases' file the script enums the sub folders. ideally you would only want to enum the folders once, e.g.

For Each objFolder In colSubFolders
If dicCases.Exists(some_mid_or_right_stringmanip(objFolder.Name)) Then
objFolder.Move
End If
Next

anyway, i am just being picky i am sure :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top