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

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Tetra201

MrExcel MVP
Joined
Oct 14, 2016
Messages
3,652
Why not use the Name statement?
Code:
Name "C:\rainbowmike\datafile.txt" As "C:\datafile.txt"
 

sdruley

Well-known Member
Joined
Oct 3, 2010
Messages
557
Office Version
  1. 365
Platform
  1. Windows
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,133,243
Messages
5,657,574
Members
418,401
Latest member
B_A_M155

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
Top