Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Windows Command line FTp - issue with todays date

  • 13-10-2010 9:17am
    #1
    Registered Users, Registered Users 2 Posts: 268 ✭✭


    Im trying to create an FTP script that will copy a file called FileName_todaysdate.txt This will run every day so obviously the filename will change every day.

    I have the below code which will copies the first file it finds alphabetically. How can I amend this to only copy the file with todays date?

    open <ftp site>
    <username>
    <password>
    put c:\FileName_*.txt
    bye

    Thanks for any help you can give me


Comments

  • Registered Users, Registered Users 2 Posts: 1,340 ✭✭✭bhickey


    One very simple way would be to make sure beforehand that only the file with today's date is in that directory?


  • Registered Users, Registered Users 2 Posts: 1,772 ✭✭✭woolymammoth


    I'm not good with scripts at all, but you could use a different script to empty the directory, then copy in the text file based on its date. then use your script to upload it wherever. What OS are you using for this?


  • Moderators, Arts Moderators, Regional Abroad Moderators Posts: 11,106 Mod ✭✭✭✭Fysh


    Based on this, it doesn't look like there's a way to do it within the CLI FTP utility. As such, I'd guess your best bet is to create a batch file which identifies the file you want to copy (perhaps making use of these instructions), then invoke the CLI FTP client from within the batch file.


  • Registered Users, Registered Users 2 Posts: 89 ✭✭tehjimmeh


    I would use powershell, much nicer than batch. You don't need to use the FTP CLI program either. Something like:
    $WebClient = New-Object System.Net.WebClient
    $WebClient.Credential = New-Object System.Net.NetworkCredential($username, $password)
    
    ls filename* | 
    %{ if ((Get-Date).Date -eq $_.CreationTime.Date){
            $WebClient.UploadFile(<address of ftp server>, $_.Name)
          }
       }
    

    Note that it's not actually looking at the date in the filename, but rather the date when the file was created on the system. If you really need to match based on the date in the filename, then (Get-Date).ToString("ddMMyyyy") should get you a string of today's date.


  • Registered Users, Registered Users 2 Posts: 268 ✭✭LillyVanilli


    bhickey wrote: »
    One very simple way would be to make sure beforehand that only the file with today's date is in that directory?
    Not possible unfortunately
    I'm not good with scripts at all, but you could use a different script to empty the directory, then copy in the text file based on its date. then use your script to upload it wherever. What OS are you using for this?
    Using Windows 2003 Server, but testing it on XP. Its not possible to empty the folder either.
    tehjimmeh wrote: »
    I would use powershell, much nicer than batch.

    If you really need to match based on the date in the filename, then (Get-Date).ToString("ddmmyyyy") should get you a string of today's date.
    Could you let me know the exact code for matching the date in the filename as this is what I need? I messed around with it but Ive never used Powershell before and couldnt crack it.

    Thanks for all the help


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 89 ✭✭tehjimmeh


    Sure.

    It'd be something like this:
    ls filename* | 
    %{ if ($_.Name -eq "filename"+(Get-Date).ToString("ddMMyyyy")+".txt"){
            $WebClient.UploadFile(<address of ftp server>, $_.Name)
          }
       }
    


  • Registered Users, Registered Users 2 Posts: 1,772 ✭✭✭woolymammoth


    Using Windows 2003 Server, but testing it on XP. Its not possible to empty the folder either.

    but you can use a different folder for the FTP part. copy the file you need to it, upload it, empty the folder. You only read from the original folder.

    have a look at the forfiles command, might be of some use.


  • Registered Users, Registered Users 2 Posts: 268 ✭✭LillyVanilli


    but you can use a different folder for the FTP part. copy the file you need to it, upload it, empty the folder. You only read from the original folder.

    have a look at the forfiles command, might be of some use.


    Ill take a look at the Forfiles. Its not possible to change the folder structure. This needs to be automated and run every night, I dont want to have to move files everyday.

    Tehjimmeh, thanks for that, I havent a clue about Powershell and dont know how to get it working.

    If anyone else know of a way to do this using command line or batch scripts let me know. Ive never used this stuff before so Im only getting my head around it.


  • Moderators, Arts Moderators, Regional Abroad Moderators Posts: 11,106 Mod ✭✭✭✭Fysh


    Tehjimmeh, thanks for that, I havent a clue about Powershell and dont know how to get it working.

    It's worth getting to grips with Powershell (trying to do this myself at the minute) as it's basically the current standard for Windows scripting. Have a look at the Scripting With Powershell page on Technet, and the Script Repository.


Advertisement