Moving 2 files to the root of C drive

sdruley

Well-known Member
Joined
Oct 3, 2010
Messages
557
Office Version
  1. 365
Platform
  1. Windows
My task is simple. I want to
1) post a text stream into 4 text files in the folder C:\rainbowmike\
2) before the text stream can be posted to a text file, the old text file must be deleted and a new one created
3) Of the 4 text files in total, 2 files by the names of datafile.txt and triggerfile.txt must be moved to the root of "C:" after they are updated with new data.
My code works for steps 1 and 2 but I can't get step 3 to work. If anyone can figure out how to move two files to the C directory, that code would probably fit where the xxxx's are shown... over my head

Code:
Sub CreateFile(fName As String, c)
Dim FSO As New FileSystemObject
Dim ts As TextStream
If FSO.FileExists(fName) Then FSO.DeleteFile (fName) 'If file exists then delete it
Set ts = FSO.CreateTextFile(fName, True) ' Create a new file in it's place
ts.WriteLine (c) ' Fill the new file with information
ts.Close ' Disengage the pointer to the file
some          'xxxx    if the fName is "c:\rainbowmike\datafile.txt" or "c:\rainbowmike\triggerfile.txt
code           'xxxx    move these files to "c:\".    FSO.MoveFile "datafile.txt", "c"\" should work
here            'xxxx    but I can't get it to work unless, perhaps, I Dim FSO as FileSystemObject instead of New FileSystemObject
End Sub

Any help would be great. The FSO convention has always confused me but it seems to be fast and i need fast.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Why not use the Name statement?
Code:
Name "C:\rainbowmike\datafile.txt" As "C:\datafile.txt"
 
Upvote 0
Tetra201, I spent a day working on this and you come up with an elegant, simple one liner that saves the day. Thank you so much for your contributions here.
I guess I will never forget the Name statement.
 
Upvote 0

Forum statistics

Threads
1,214,897
Messages
6,122,151
Members
449,068
Latest member
shiz11713

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top