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

Move files to specific folders based on filename

Status
Not open for further replies.

Nashvegas

Programmer
Feb 15, 2002
7
US
I am trying to create a script that will check the date created of a file, then move it to a specific folder based on that file name, if it is so old.

Example
Folders: 345000, 346000, 347000
Files: 345678, 346898, 345123

The files beginning with the same first 3 characters would go to that folder.

I've been able to do the date part of it and strip the first three characters from the file name, I just can't figure our how to make the move based on the names.

Any help at all will be much appreciated.
 
Nashvegas,

You could try something like this:

If file = file1.txt Then
Folder = folder1
ElseIf file = file2 Then
Folder = folder2
End If

You can use a wildcard ie, *. Then later in the script use a file move snippet and insert the Folder variable. It will use the folder that matches the file.

Hope this makes sense.

Z
 
Sorry, I should have mentioned this in the original post.

The folder names change. We store 100 files, then go to the next number (a number our system creates). So you fill folder name "345000" with file names "345000 - 345999"
 
Can you post what you have so far telling us where you're stuck ?
 
if i get you right,
foldername = "/" & left(filename,3) & "000" & "/"




if it is to be it's up to me
 
Here is what I have so far. The problem is with the MoveFile statement. How do I match up the first 3 characters of the filename to the correct first 3 characters of the folder name, then move the file there.

Again, thanks for all help in advance.


Option Explicit
DATE2 = DateAdd("d", -2, Date)
dim PDFNAME
'
Set FSO = CreateObject ("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder("D:\Billing Collections\Recent")
Set Folder2 = FSO.GetFolder("D:\Billing Collections")
'
For Each File In Folder.Files
PDFNME = Left (File.Name, 3)
If File.DateCreated <= DATE2 Then
objFSO.MoveFile **THIS IS WHAT I CAN'T FIGURE OUT**

End If
Next
 
infinitelo, Thank you. It took me a little while to understand what you were telling me.


I am much closer now, at least I feel like I'm on the right track.

Here's what I have now.

DATE2 = DateAdd(&quot;d&quot;, -2, Date)
'
Set FSO = CreateObject (&quot;Scripting.FileSystemObject&quot;)
Set Folder = FSO.GetFolder(&quot;D:\recent\&quot;)
'
For Each File In Folder.Files
If File.DateCreated <= DATE2 Then
Wscript. Echo (File.Name)
FSO.MoveFile &quot;D:\recent\&quot; & (File.Name) & &quot;.pdf&quot; , &quot;D:\&quot; & left(File.Name,3) & &quot;000&quot;
End If
Next

If I use a * in the first File.Name in the MoveFile, it moves all of the files to one folder. With this current configuration, I get a error message saying &quot;File Not Found&quot;.

Thank you.
 
Everyone,

Thank you for your responses. I finally have it figured out. infinetelo - your response was key.

The last issue I had was just in the way I had my path's laid out.

Here is the final working code, there are other things I have to add for error trapping, etc., but this was the thing I was worried about.

Thanks again.

DATE2 = DateAdd(&quot;d&quot;, -2, Date)
'
Set FSO = CreateObject (&quot;Scripting.FileSystemObject&quot;)
Set Folder = FSO.GetFolder(&quot;D:\Billing Collections\Recent&quot;)
'
For Each File In Folder.Files
If File.DateCreated <= DATE2 Then
FSO.MoveFile &quot;D:\Billing Collections\Recent\&quot; & (File.Name) , &quot;D:\Billing Collections\&quot; & left(File.Name,3) & &quot;000&quot; & &quot;\&quot;
End If
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top