• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 706
  • Last Modified:

VBScript not working / glitchy on a folder with extremely large number of files in it

Hi,

I have a folder with about 600,000 text files in it, windows having a lot of problem just opening this folder.
I needed to analyze some of the text files, so I wanted to organize it by months.
I made a very simple vbscript to move a file to a folder based on the creation date.
for testing, I limited the loop to the first 10 files.
The script completed its run, no errors, but it doesn't do anything.
no files were moved, no folders were created, no log files were created.

tested on other folders with small amount of text files, script worked fine.

Is there any limitation preventing me from working with a folder of this magnitude?

Dim myFSO
Dim txtdir
Dim fil

Set myFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set fil = myFSO.GetFile(Wscript.ScriptFullName)
Set txtdir = fil.ParentFolder

Dim log
Set log = myFSO.CreateTextFile("log.txt")

for each file in txtdir.files
      dim fileDate, fileMonth, fileYear, newpath
      fileDate = CDATE(file.DateLastModified)
      fileMonth = monthname(month(fileDate),true)
      fileYear = Year(fileDate)
      newpath = fileMonth & fileYear & "\"
      if not myfso.folderexists(newpath) then
            myfso.createfolder(newpath)
      end if
      log.writeline("moving " & file.name & " to " & newpath)
      myFSO.movefile file.path, newpath

next

log.close
msgbox "Done!"

I redirected my output and my log to a different folder and it works.
Permission is the same on both folders.
0
FujiMed
Asked:
FujiMed
  • 4
  • 3
  • 2
1 Solution
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Hi, can you try this:
Dim myFSO
Dim txtdir
Dim fil

Set myFSO = CreateObject("Scripting.FileSystemObject")
Set txtdir = myFSO.GetFolder("..")

Dim log
Set log = myFSO.CreateTextFile("log.txt")
for each file in txtdir.files
      dim fileDate, fileMonth, fileYear, newpath
      fileDate = CDATE(file.DateLastModified)
      fileMonth = monthname(month(fileDate),true)
      fileYear = Year(fileDate)
      newpath = txtdir & "\" & fileMonth & fileYear & "\"
      if not myfso.folderexists(newpath) then
            myfso.createfolder(newpath)
      end if
      log.writeline("moving " & file.name & " to " & newpath)
      myFSO.movefile file.path, newpath
	
next

log.close
msgbox "Done!"

Open in new window

Put the script as mover.vbs in a new subdirectory of the directory that you want to process.
0
 
FujiMedAuthor Commented:
hey, thanks for the response.

I did try running the vb script from a different folder although not the subfolder of the problematic folder, and then specify the txtdir
- logs get created in the same folder of the vbscript
- logs get written that it is moving files
- yet, no folders or files are being moved within that folder....

weird huh? fso operation inside the folder doesn't seem to work, even though vbscript doesn't throw any runtime error / exceptions. maybe severe fso delays due to the size of the folder?

what I've done that is working is to copy the files from that folder to a different folder altogether, not a subdirectory and that works. just puzzled why it won't organize into subfolders.
0
 
ltlbearand3Commented:
A few quick things here.  Your running into a permissions issue because of how you are moving the file.  You have script gain access to the file through
For each file in txtdir.files

Open in new window

However, when you move it you have another separate object try and move the file:
myFSO.movefile file.path, newpath

Open in new window

The best practice is to move the file with the file object.  Also I recommend using a name other than file since it can be a key word.

Try this adapted version:
Dim objFSO
Dim objWorkingDir, objTextLog
Dim objFile, objScriptFile

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objScriptFile = objFSO.GetFile(Wscript.ScriptFullName)
Set objWorkingDir = objScriptFile.ParentFolder

Set objTextLog = objFSO.CreateTextFile("log.txt")
for each objFile in objWorkingDir.files
      dim fileDate, fileMonth, fileYear, newpath
      fileDate = CDATE(objFile.DateLastModified)
      fileMonth = monthname(month(fileDate),true)
      fileYear = Year(fileDate)
      newpath = objWorkingDir & "\" & fileMonth & fileYear & "\"
      If not objFSO.folderexists(newpath) then
            objFSO.createfolder(newpath)
      end if
      objTextLog.writeline("moving " & objFile.name & " to " & newpath)
	  
	  ' Dont try to move the script or the log.  Would not be good.
	  If objFile.path <> Wscript.ScriptFullName and _
		objFile.path <> objWorkingDir.Path & "\log.txt" Then
		
		objFile.Move newpath
	end if
Next

objTextLog.close
msgbox "Done!"
                                            

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
FujiMedAuthor Commented:
Thanks, I'll give that a go.
The actual script does a few more checks than the one I pasted (checks if it's a txt file) so it won't move the script and log.
0
 
ltlbearand3Commented:
That is good.  I had added those in as I did not see it.  The big change is to go from:
myFSO.movefile file.path, newpath

Open in new window

to
objFile.Move newpath

Open in new window


-Bear
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
>> - yet, no folders or files are being moved within that folder....

Strange, I tested by creating a 'script' folder in the 'problematic' folder and put the script in there. After running the script, it created the .log file in the 'script' folder (with the script) and created <month><year> folders in the 'problematic' folder moving files from the 'problematic' folder to each separate <month><year> folder. Log file showing each move as it is performed.

log.txt:
moving file1.pdf to C:\Temp\ProblemFolder\Nov2002\
moving file2.pdf to C:\Temp\ProblemFolder\Feb2005\

Are you allowed to create new <month><year> subfolders and move files there?
moving file3.pdf to C:\Temp\ProblemFolder\Mar2005\
moving file4.pdf to C:\Temp\ProblemFolder\Jun2006\

C:\Temp\ProblemFolder\script containing the script.
0
 
FujiMedAuthor Commented:
I can create folders and move files in that folder in windows explorer.
my script works fine on my test folders too.

Just a note that windows explorer takes a good 8-10 minutes to be able to completely open this folder, and if I do too many file operations in it, explorer will crash.
0
 
ltlbearand3Commented:
Were you able to test with my changes and if so how did it go?
0
 
FujiMedAuthor Commented:
Thanks, I don't think I could find the time to test it at the moment unfortunately. anyway i'm sorting it out to a different folder and that works anyhow. probably just windows being... well, windows.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now