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

Moving .txt files from one folder to another

Status
Not open for further replies.

gjsala

Technical User
Feb 20, 2003
107
US
Thanks for your help in advance. I have multiple .txt files in a folder that I would like to move to a different folder if they meet certain criteria. If the txt file is less than 1000 kb then it’s moved to a different folder. Also if the txt file is greater than 36 hours old then this will go to the same archive folder. Here is the code I have so far:

Const SourceDir As String = "C:\test"
Const DestinationDir As String = "C:\test\Archive"
Dim strFileSize As String, strTempDate As String
Dim strCurrentFileName As String

'''''Less than 1000kb then move to archive folder
strCurrentFileName = (SourceDir & "*.txt")

Do

strFileSize = FileLen(strCurrentFileName)
If strFileSize < 1000 Then
FileCopy SourceDir & strCurrentFileName, DestinationDir & strCurrentFileName
Kill SourceDir & strCurrentFileName
End If


Loop Until strCurrentFileName = ""

Thanks!
 
With the code you have so far I'd use the the Dir() function.
But with FileSystemObject you'll get more flexibility.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
1. Please always state what application you are running code from (and version)

2. Please post all relevant code. This:
Code:
strCurrentFileName = (SourceDir & "*.txt")
looks like it may be using the Dir() function, but that is certainly not clear.

Gerry
 
PHV,
How would you use the Dir()function for this example?

Thanks!
 
Code:
Sub MyDIR()
Dim file
Dim SourcePath As String
Dim DestinationPath As String

SourcePath = "c:\test\"
DestinationPath = "c:\test\Archive\"

file = Dir(SourcePath & "*.txt")
Do While file <> ""
   If FileLen(SourcePath & file) < 1000 Then
         FileCopy SourcePath & file, DestinationPath & file
   End If
   file = Dir()
Loop
End Sub

Gerry
 
N.B. when using Dir it is important to make sure you include the terminating slash for the paths.

SourcePath = "c:\test\"

not

SourcePath = "c:\test"

Gerry
 
fumei,
The code did work and thanks! How should I change this so the files are not copied but moved from one folder to the next?

Thanks!
 



Code:
Dim OldName, NewName
OldName = "OLDFILE": NewName = "NEWFILE"    ' Define file names.[b]
Name OldName As NewName    ' Rename file. [/b]

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 



more relevant...
Code:
OldName = "C:MYDIROLDFILE": NewName = "C:YOURDIRNEWFILE"
Name OldName As NewName    ' Move and rename file.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
files are not copied but moved from one folder to the next?
Define "next".

As for Move instead of Copy, you could also simply add a Kill instruction after your copy:
Code:
   If FileLen(SourcePath & file) < 1000 Then
         FileCopy SourcePath & file, DestinationPath & file
         [b]Kill SourcePath & file[/b]
   End If
There has been no previous mention of any name change. So my code has no change in name. A change in PATH, yes.

Gerry
 
the Name method can be used to change the path as well. You can choose not to change the filename

Code:
oldpath = "C:\Folder1\"
newpath = "C:\Folder2\"

name oldpath & filename, newpath & filename
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top