473,320 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to copy a file from one location to another OnClick?

sueb
379 256MB
I have a file that I would like to copy from a static location to one based on the value in a control (ChartNum) when the user clicks a button I'll provide for this purpose.

I know how to create the path string, but I don't know the MS command to do this.
Mar 18 '11 #1

✓ answered by ADezii

The following Code will Copy Test.xls in the C:\Test\ Folder, to the C:\Stuff\ Folder, and Rename it to B1234.xls.
Expand|Select|Wrap|Line Numbers
  1. Dim ChartNum As String
  2.  
  3. ChartNum = "B1234"
  4.  
  5. FileCopy "C:\Test\Test.xls", "C:\Stuff\" & ChartNum & ".xls"

11 4008
ADezii
8,834 Expert 8TB
The following Code will Copy Test.xls in the C:\Test\ Folder, to the C:\Stuff\ Folder, and Rename it to B1234.xls.
Expand|Select|Wrap|Line Numbers
  1. Dim ChartNum As String
  2.  
  3. ChartNum = "B1234"
  4.  
  5. FileCopy "C:\Test\Test.xls", "C:\Stuff\" & ChartNum & ".xls"
Mar 18 '11 #2
sueb
379 256MB
Oh that's great, ADezii!

But I forgot one thing: I want to first test whether the file is already there before I copy it.

How do I do that, too?
Mar 18 '11 #3
sueb
379 256MB
Oh, shoot--it turns out I'm not handling my MkDir commands at all correctly!

Here's what I have:

Expand|Select|Wrap|Line Numbers
  1. Function Public_Open_eAccount(strChartNum As String, strAccount As String)
  2. On Error GoTo Err_Public_Open_eChart
  3.  
  4.     Dim retVal As Variant
  5.     Dim strChartPath As String
  6.     Dim strAccountPath As String
  7.     Dim strFileName As String
  8.     Const conPath As String = "V:\Utilization Review\UR\eCharts\"
  9.     Const conFormSource As String = "V:\Utilization Review\UR\DATABASE\FORMS\"
  10.  
  11.     ' Need both parts of the path
  12.     If IsNull(strChartNum) Or IsNull(strAccount) Then Exit Function
  13.  
  14.     ' Make the path pieces
  15.     strChartPath = Replace(Replace(strChartNum, "\", ""), ",", "_")
  16.     strAccountPath = Replace(Replace(strAccountPath, "\", ""), ",", "_")
  17.  
  18.     ' Make the directory, if necessary
  19.     If Dir$(conPath & strChartPath, vbDirectory) = "" Then
  20.         MkDir (conPath & strChartPath & strAccountPath)
  21.     End If
  22.  
  23.     ' Copy forms into Account folder
  24.     strFileName = "18-1.DOC"
  25.     FileCopy conFormSource & strFileName, conPath & strChartPath & strAccountPath & strFileName
  26.     strFileName = "FAX COVER.DOT"
  27.     FileCopy conFormSource & strFileName, conPath & strChartPath & strAccountPath & strFileName
  28.     strFileName = "Medi-Cal UR Review.doc"
  29.     FileCopy conFormSource & strFileName, conPath & strChartPath & strAccountPath & strFileName
  30.     strFileName = "UR FORM.DOC"
  31.     FileCopy conFormSource & strFileName, conPath & strChartPath & strAccountPath & strFileName
  32.  
  33.     ' Display an Explorer window
  34.     retVal = Shell("Explorer.exe " & conPath & strChartPath & strAccountPath, vbNormalFocus)
  35.  
  36. Exit_Public_Open_eChart:
  37.    Exit Function
  38.  
  39. Err_Public_Open_eChart:
  40.     MsgBox Err.Description
  41.     Resume Exit_Public_Open_eChart
  42.  
  43. End Function
  44.  
but it's creating only the path up to the chartnum, and it's not copying the files in (although that could very well be a function of not creating the path, too).
Mar 18 '11 #4
sueb
379 256MB
I minimized all my windows, and I just saw that all these copies of the forms are on my desktop, with variously mangled names! What???
Mar 18 '11 #5
TheSmileyCoder
2,322 Expert Mod 2GB
Do you remember to add a \ in between strChartPath and strAccountPath? and a \ after strAccountPath?

When debugging remember to use either Debug.Print or msgbox to help you. For example, to make teh copy process clear:
Expand|Select|Wrap|Line Numbers
  1. strFileName = "UR FORM.DOC" 
  2. Msgbox "About to copy:" & vbnewline & _
  3.        "FROM: [" & conFormSource & strFileName & "]" & vbnewline & _
  4.        "TO:   [" & conPath & strChartPath & strAccountPath & strFileName & "]"
  5. FileCopy conFormSource & strFileName, conPath & strChartPath & strAccountPath & strFileName 
Having good debugging skills will help you alot.

EDIT: Also remember that mkDir can only create one directory at a time. So to create the folder C:\Test\Folder, you must first ensure that C:\Test exists.
I have my own version I call MakeDir which will create any other folders needed to create the subfolder. I will include it here:
Expand|Select|Wrap|Line Numbers
  1. Public Function MakeDir(ByVal strPath As String) As Boolean
  2.     'creates a directory independent of whether the parent directory exists
  3.     'Code by TheSmileyOne
  4.     'Version 0.1
  5.     'Date 2010-05-19
  6.  
  7.     'Known issues
  8.     'No error handling for cases such as network drives, with restricted permissions to create folders.
  9.     'No input validation
  10.  
  11.  
  12.     On Error GoTo err_Handler
  13.  
  14.     'Check if rightmost char is a \
  15.     If Right(strPath, 1) = "\" Then
  16.         'Strip it
  17.         strPath = Left(strPath, Len(strPath) - 1)
  18.     End If
  19.  
  20.     'Check if each individual directory exists, and if not, create it
  21.     Dim strSplitPath() As String
  22.     ReDim strSplitPath(UBound(Split(strPath, "\")))
  23.     strSplitPath = Split(strPath, "\")
  24.     Dim intI As Integer
  25.     Dim strCombined As String
  26.     For intI = 0 To UBound(strSplitPath)
  27.         If intI <> 0 Then strCombined = strCombined & "\"
  28.  
  29.         strCombined = strCombined & strSplitPath(intI)
  30.         If Dir(strCombined, vbDirectory) = "" Then
  31.             MkDir strCombined
  32.         End If
  33.  
  34.  
  35.  
  36.     Next
  37.     MakeDir = True
  38. Exit Function
  39. err_Handler:
  40.     MakeDir = False
  41.         MsgBox "Error " & Err.Number & " occured." & vbNewLine & Err.Description
  42.  
  43. End Function
Mar 18 '11 #6
NeoPa
32,556 Expert Mod 16PB
Sue,

Please try to keep a thread to a single (preferably the original) topic. This one's too messy now to split up.

We're happy to keep answering questions, but only one post can be flagged as the selected answer (ADezii's in this case I suggest), so your other questions need to be separated for the purposes of this site. Just a point to remember for what I hope are many more of your questions :-)
Mar 18 '11 #7
sueb
379 256MB
Oh, I apologize, NeoPa! At the time, these seemed like the same issue to me (because I was so wrapped up in it, I suppose!), but, of course, they are not.

I will certainly keep a closer watch on my posts in the future. I do know that a tight focus to a thread increases its usefulness, and this site is so useful that I want to help keep it that way.
Mar 18 '11 #8
sueb
379 256MB
@ADezii, that all seems to be working great. Thanks so much for your help!

(Next time, I'll try to stick to the subject! :D )
Mar 18 '11 #9
sueb
379 256MB
@TheSmileyCoder, I used your suggestions to correct how my MkDir was working, and that is working great now, too! Thanks!
Mar 18 '11 #10
ADezii
8,834 Expert 8TB
You got 2 answers for the price of 1, nice deal sueb! (LOL).
Mar 19 '11 #11
NeoPa
32,556 Expert Mod 16PB
I know you're being as co-operative as you can Sue. I'm always happy to explain why we have the rules and you always appreciate that and learn for future postings :-)

PS. I should mention that, of course, Smiley's post was equally helpful and deserved a Best Answer too, but it was simply not a post that pertained to the original question (Hence, for this thread, I suggested ADezii's one). I hope no-one misunderstood my comment to be any form of favouritism, or even as denigrating Smiley's (excellent) post in any way.
Mar 20 '11 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Paul | last post by:
Dear All, I want to use web form to upload my file and copy the file to another machine. I can upload the file, but when I copy the file(file.CopyTo(".....", true)) to another machine(map...
5
by: Patrice Dargenton | last post by:
Hello, I have to convert an xml file to another xml file that is very similar, but some content are different, and the names of some nodes are different. In fact it's a response to a message :...
1
by: Jim Heavey | last post by:
Hello, I have done some volunteer work for my club and develop a web site in ASP.Net. I have the code working fine and now I am ready to move the code up to their web hosting company. I have...
1
by: chiranjiv choudhary via .NET 247 | last post by:
Can we have CodeBehind file in another server or same machine but diffrent location or directory -------------------------------- From: chiranjiv choudhary ----------------------- Posted by a...
3
by: Christopher Lusardi | last post by:
If I want to copy one file to another how do I do that? I want to do this even if the second file already exists. Thanks, Christopher Lusardi
2
by: LiveCycle | last post by:
Hi, I'm writing a web service, and, as part of its function, I want it to copy a file from a remote server and place that file on the local web server under another name. However, I always get...
9
rizwan6feb
by: rizwan6feb | last post by:
Hello everybody. I need help on how to use copy function to copy file from one computer to another ( or any other way i can copy the file). The computers are on a local network.
18
by: wizdom | last post by:
Help - change text on click - text has another onclick inside with php variables ---------- I think what I'm trying to do is simple. I have a 2 buttons on a page. 1 button allows a thread of...
2
by: foss | last post by:
hi all, I am not able to copy file from a directory in the server to another directory. Here, the source is outside the web root directory and the destination is inside the web root directory. ...
2
by: ahmurad | last post by:
Dear Brothers, I am using a 'text file location' at my code to read the file contents- which is working fine in my local server. The web contents/php files and the specified txt file are at the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.