[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Search folders and rename and move the files to another folder

Posted on 2011-09-12
11
Medium Priority
?
231 Views
Last Modified: 2012-05-12
We have the following situations:
We are scanning documents on a large Scanner to a certain folder(s), example …\share\
Below the main folders, the system generates automatically subfolders based on date and a running number, examples:

…\share\folderdatetoday-001
…\share\folderdatetoday-002
…\share\folderdatetoday-003

Then the next day, it looks like this:

…\share\folderdatetomorrow-001
…\share\folderdatetomorrow-002
…\share\folderdatetomorrow-003

etc. etc.

Within these folders, we have .tif documents.

Every night, we need to move these files from the subfolders to a folder called for example:  “transfer”

All the files in the subfolders need to be renamed with a running number, i.e. 0001, 0002, 0003, 0004 etc.  The actual names are not important, only that a file that already exists will not be overwritten.

So here comes the question:

How can we create a script where we can search for within subfolders for .tif files and move them?  After we have moved them, we need to delete the folders.

I have been searching for fso recursive but so far, I have been out of luck....
0
Comment
Question by:terra-it
  • 6
  • 4
11 Comments
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36522177
Would the "running number" start at 0001 each time the job runs, or does it somehow need to remember where it left off last time?

How long do files stay in the transfer location, and once they are moved out can their file names be reused.

In the interest of uniqueness, why not make the file names in the transfer location the name of the folder the file came from (folderdatetoday-001) and then add a sequence number on the end of that?

~bp
0
 

Author Comment

by:terra-it
ID: 36522207
It does not have to "remember" where it left off because each nicht, we will empty the folder with the "moved" data.

Names can be reused.

We could also use the foldernames as filenames, no problem.

MIR
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36522502
See how this works for you. Save as a VBS file and adjust the two paths near the top. Also, right now it will only display the files it would move and the names involved. After you run it in this mode if it looks good you can uncomment the actual Move line to move the files. This script assumes the transfer directory already exists. Also wasn't sure if you wanted the folder to be deleted after the files were moved, so I added that line but left it commented out.

sBaseDir = "c:\ee\EE27303021\base"
sXferDir = "c:\ee\EE27303021\xfer"

iNumber = 0
Set oFSO = CreateObject("Scripting.FileSystemObject")
For Each oFolder In oFSO.GetFolder(sBaseDir).SubFolders
   For Each oFile In oFolder.Files
      iNumber = iNumber + 1
      Wscript.Echo "Moving [" & oFile.Path & "] to [" & sXferDir & "\" & oFolder.Name & "-" & LeftPad(iNumber, 3, "0") & "]"
      ' oFile.Move sXferDir & "\" & oFolder.Name & "-" & LeftPad(iNumber, 3, "0")
   Next
   'oFolder.Delete
Next

Function RightPad( strText, intLen, chrPad )
  RightPad = Left( strText & String( intLen, chrPad ), intLen )
End Function

Function LeftPad( strText, intLen, chrPad )
  LeftPad = Right( String( intLen, chrPad ) & strText, intLen )
End Function

Open in new window

~bp
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
LVL 57

Expert Comment

by:Joe Winograd, EE MVE 2015&2016
ID: 36522722
All of my scripting needs have been met by AutoHoteky:
http://www.autohotkey.com/

It is an excellent product, extremely robust, and FREE! Regards, Joe
0
 

Author Comment

by:terra-it
ID: 36522914
Hi bp
I did change the 2 lines to my paths:
sBaseDir = "D:\HabelDocuments\DOC010911"
sXferDir = "D:\xfer"

and ran the script.  No failure, no message.
Then I uncommented the "move" and ran it, no failure, no message, no files moved
Then I uncommented the "Delete" and ran it, no failure, no message, no files moved, no deletion of folder.

Do we search for all types of files, we are not defining specifically the .tif?

In the script I would need to search within all folders that start with DOC* because as said before, the folders will be named today as DOC1-001, DOC1-002, DOC1-003 and then tomorrow or next time something will be scanned, you have more folders, example DOC2-001, DOC2-002 in addition to the ones created above..

Possible to take a look if there is something that I forgot or if there is somewhere a spelling error?  I did not see any and the script runs, but no action..

Script would be run every night (I wrote before "nicht", sorry)

MIR

0
 
LVL 59

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 36522981
The sBaseDir should be the "…\share", not the "…\share\folderdatetoday-001", I suspect "D:\HabelDocuments\DOC010911" is one of the daily folders, not the parent where all dailys reside? The script looks for subfolders in the sBaseDir to find all the day folders.

I suspect you want this, but without knowing your folder structure I can't be positive.

sBaseDir = "D:\HabelDocuments"

~bp
0
 

Author Comment

by:terra-it
ID: 36523016
jepp, found the failure... testing again,, sorry...
0
 

Author Comment

by:terra-it
ID: 36523091
brilliant, working great, BUT, always these "buts"...
The new files in the folders are missing the extensions, that is .tif

Where could I add that?  Must be a variable, or?

MIR
0
 

Author Comment

by:terra-it
ID: 36523149
Fixed, added the extension after:

oFile.Move sXferDir & "\" & oFolder.Name & "-" & LeftPad(iNumber, 3, "0") & ".tif"

Works perfectly

Thanks a lot, this was really a superb help to my long searching problem...

Michael
0
 

Author Closing Comment

by:terra-it
ID: 36523158
Brilliant solution, only Experts in this KB
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36523345
Ah yes, extension was an oversight, sorry, glad you worked that out.  Thanks for the comments.

~bp
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

834 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