locked
Using a variable in a FileCopy command as file name RRS feed

  • Question

  • Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim fName,s_Date, e_Date, tDate, Header
    
    If tDate = "Monday" Then
    fName = "DailySO_"& DatePart("m",Date)&"-"& DatePart("d",Date -3)&".csv"
    Else
    fName = "DailySO_"& DatePart("m",Date)&"-"& DatePart("d",Date -1)&".csv"
    End If
    
    objFSO.CopyFile "C:\Dropbox\InFlow\DailySalesOrders\_frontend_txt_4113_SOExport.txt", "C:\Dropbox\Inflow\DailySalesOrders\DailySalesOrds.csv"
    WScript.Sleep 2000
    objFSO.CopyFile "C:\Dropbox\Inflow\DailySalesOrders\DailySalesOrds.csv", "C:\Dropbox\InFlow\DailySalesOrders\GetSalesArchive\&fName"
    
    

    I would greatly appreciate it if someone could tell me how to use the fname variable in the last line of  this script. i have tried many variations without success.

    Thank you in advance for any and all suggestions

    OldCityCat

    Saturday, January 18, 2020 3:20 PM

All replies

  • Just use it where you need it.  Why is that an issue?

    You should not be using VBScript as it is almost obsolete.  You can use PowerShell and not waste time trying to learn a dfunct technology.


    \_(ツ)_/


    • Edited by jrv Saturday, January 18, 2020 5:16 PM
    Saturday, January 18, 2020 5:16 PM
  • Yes I've been hearing that a lot lately.

    I'm slowly learning PS but just needed a quick fix, not ready to re-write.

    The issue is it just returns "&fname" not the value it was assigned.

    OldCityCat

    Saturday, January 18, 2020 5:42 PM
  • fName = "DailySO_" & DatePart("m",Date) & "-" & DatePart("d",Date -3) & ".csv"
    wscript.echo fname
    Works fine for me.

    \_(ツ)_/


    • Edited by jrv Saturday, January 18, 2020 5:46 PM
    Saturday, January 18, 2020 5:46 PM
  • $d = if($tDate.DayOfWeek = 'Monday'){
        [datetime]::Today.AddDays(-3)
    }else{
        [datetime]::Today.AddDays(-3)
    }
    $fName = 'C:\Dropbox\Inflow\DailySalesOrders\DailySO_{0:MMdd}.csv' -f $d
    Copy-Item 'C:\Dropbox\InFlow\DailySalesOrders\_frontend_txt_4113_SOExport.txt', $fname


    \_(ツ)_/

    Saturday, January 18, 2020 5:53 PM
  • Assigning the value to fname has never been an issue, the only issue is with CopyFile statement not returning the value of fName, So the file name ends up as &fName and not DailySO_XXxx.csv. 

    Thanks for the code snippet but but looks like Powershell? not quite ready to go down that road yet.

    objFSO.CopyFile "C:\Dropbox\Inflow\DailySalesOrders\DailySalesOrds.csv", "C:\Dropbox\InFlow\DailySalesOrders\GetSalesArchive\&fName"

    Monday, January 20, 2020 12:19 PM
  • The code you wrote does not use fname as a variable.  It is just a string.  Look closely at the code.

    OI posted PowerShell because, if you do not know VBScript you should not even try to learn it.  It is obsolete and will not work well with modern Windows.

    The bottom line is that your code is syntactically wrong.  You can only embed variables in strings in PowerShell.  VBScript does not allow this.


    \_(ツ)_/

    Monday, January 20, 2020 12:40 PM