Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2012-08-21
9
Medium Priority
?
689 Views
Last Modified: 2012-09-02
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
Comment
Question by:FujiMed
  • 4
  • 3
  • 2
9 Comments
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 38321046
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
 

Author Comment

by:FujiMed
ID: 38322695
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
 
LVL 20

Accepted Solution

by:
ltlbearand3 earned 2000 total points
ID: 38323303
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:FujiMed
ID: 38323325
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
 
LVL 20

Expert Comment

by:ltlbearand3
ID: 38323367
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
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 38323681
>> - 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
 

Author Comment

by:FujiMed
ID: 38323703
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
 
LVL 20

Expert Comment

by:ltlbearand3
ID: 38348545
Were you able to test with my changes and if so how did it go?
0
 

Author Closing Comment

by:FujiMed
ID: 38359559
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

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
In this tutorial, we’re going to learn how to convert Youtube to mp3 for Free. We'll show you how easy it is to make an mp3 from your video clips so that you can enjoy them offline.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question