Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Defining current date in batch file  (Read 6503 times)

0 Members and 1 Guest are viewing this topic.

swinters

    Topic Starter


    Rookie

    Defining current date in batch file
    « on: September 29, 2009, 01:42:35 PM »
    I have a file that gets exported daily.  It always ends with todays date, i.e. support_20090929.csv.  I've tried several variables to pull in the current date each day in my ftp batch file without any luck.  Below are some examples of what I've tried.  If anyone has any idea how to do this I'd appreciate the help.

    put \\slnk1001a_support_&date&.csv /slnk1001a/slnk1001a_support&date&.csv

    put \\slnk1001a_support_$YYYYMMDD$.csv /slnk1001a/slnk1001a_support_$YYYYMMDD$.csv

    put \\slnk1001a_support_%date:~10,4%%date:~4,2%%date:~7,2%.csv /slnk1001a/slnk1001a_support_%date:~10,4%%date:~4,2%%date:~7,2%.csv

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Defining current date in batch file
    « Reply #1 on: September 29, 2009, 02:02:23 PM »
    Not clear what "put" means
    Command prompt says it is not a command.
    Try to explain what you want in some more detail.

    swinters

      Topic Starter


      Rookie

      Re: Defining current date in batch file
      « Reply #2 on: September 29, 2009, 02:37:11 PM »
      Its taking the file from my server and ftp'ing it to another vendor...what I do currently is change the date manually everytime.  Below is the .bat file code and the ftp.txt file it calls is below that.  I'm trying to find code that will replace the 20090929 each day with the current date so that I don't have to key it manully each time.

      ftp.bat
      ftp -n -i -d -g -s:S:\BatchFiles\FTP.txt ftp.site.com

      ftp.txt
      put S:\EXCEL\slnk1001a_support_20090929.csv /slnk1001a/slnk1001a_support_20090929.csv

      oldun

      • Guest
      Re: Defining current date in batch file
      « Reply #3 on: September 29, 2009, 05:22:52 PM »
      Your third example looks as though it ought to work.
      What output are you getting?

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Defining current date in batch file
      « Reply #4 on: September 29, 2009, 06:16:33 PM »
      Part of a batch to use current date could be like this:
      Code: [Select]
      REM Below is an exercise to show...
      REM date string manipulation.
      DATE /T
      echo %date:~4,2%
      echo %date:~7,2%
      echo %date:~10,4%
      echo %date:~4,2%>MM.TMP
      echo %date:~7,2%>DD.TMP
      echo %date:~10,4%>YYYY.TMP
      set /P MM= <MM.TMP
      SET /P DD= <DD.TMP
      SET /P YYYY= <YYYY.TMP
      ECHO %MM%%DD%%YYYY%

      swinters

        Topic Starter


        Rookie

        Re: Defining current date in batch file
        « Reply #5 on: September 30, 2009, 06:34:42 AM »
        Below is an excert from the third example.  I've also included what it looks like when i have the date hard coded in and the ftp is successful.

        550 /slnk1001a/slnk1001a_disconnect_%date:~10,4%%date:~4,2%%date:~7,2%.csv: The
        filename, directory name, or volume label syntax is incorrect.


        ---> STOR /slnk1001a/slnk1001a_disconnect_20090930.csv
        150 Opening ASCII mode data connection for /slnk1001a/slnk1001a_disconnect_20090
        930.csv.
        226 Transfer complete.
        ftp: 75671 bytes sent in 0.45Seconds 167.04Kbytes/sec.

        Thanks!

        oldun

        • Guest
        Re: Defining current date in batch file
        « Reply #6 on: September 30, 2009, 04:44:41 PM »
        OK. The problem appears to occur because FTP is unable to expand the variable. You may need to CALL the PUT command. Something like:
        Code: [Select]
        call:put \\slnk1001a_support_%date:~10,4%%date:~4,2%%date:~7,2%.csv /slnk1001a/slnk1001a_support_%date:~10,4%%date:~4,2%%date:~7,2%.csv


        :put
        put %*
        « Last Edit: September 30, 2009, 06:37:00 PM by oldun »

        swinters

          Topic Starter


          Rookie

          Re: Defining current date in batch file
          « Reply #7 on: October 02, 2009, 06:24:16 AM »
          Hi Oldun - Thank you once again for helping me.  I tried the new code you suggested and still get an error...little different though.

          ftp> call:put S:\slnk1001a_support_%date:~10,4%date:~4,2%date:~7,2%.csv /slnk1001a/slnk1001a_support_%date:~1
          0,4%date:~4,2%date:~7,2%.csv
          Invalid command.

          Spoiler



            Specialist

            Thanked: 50
          • Experience: Beginner
          • OS: Windows XP
          Re: Defining current date in batch file
          « Reply #8 on: October 02, 2009, 11:12:40 AM »

          Change your FTP to use the mput comand.

          PROMPT

          lcd put S:\EXCEL\

          mput *.csv

          this will send all the files named .csv in that folder.

          If you only want to send todays file then setup a temp folder to move all the old stuff out of the working folder leaving todays file only. After the FTP move the file to the temp folder for storage and start a new blank file for the next day.



           

          Whenever I watch TV and I see those poor starving kids all over the world, I can't help but cry. I mean I would love to be skinny like that, but not with all those flies and death and stuff." - Mariah Carey, Pop Singer

          oldun

          • Guest
          Re: Defining current date in batch file
          « Reply #9 on: October 03, 2009, 06:41:56 PM »
          Sorry. Try rewriting your ftp.bat file like this:

          NOTE: The Echo line should be on a single line.
          Code: [Select]
          @Echo Off
          Echo put \\slnk1001a_support_%date:~10,4%%date:~4,2%%date:~7,2%.csv /slnk1001a/slnk1001a_support_%date:~10,4%%date:~4,2%%date:~7,2%.csv >S:\BatchFiles\ftp.txt

          ftp -n -i -d -g -s:S:\BatchFiles\FTP.txt ftp.site.com


          Salmon Trout

          • Guest
          Re: Defining current date in batch file
          « Reply #10 on: October 04, 2009, 09:19:49 AM »
          I am presuming that the %date% format is the US style, Day dd/mm/yyyy.

          why not shorten the long line thus and make things more readable

          Code: [Select]
          set YYYYMMDD=%date:~10,4%%date:~4,2%%date:~7,2%
          set file=slnk1001a_support_%YYYYMMDD%.csv
          set path1=\\%file%
          set path2=/slnk1001a/%file%
          set ftptxtfile=S:\BatchFiles\ftp.txt
          Echo put %path1% %path2% >%ftptxtfile%
          ftp -n -i -d -g -s:%ftptxtfile% ftp.site.com

          swinters

            Topic Starter


            Rookie

            Re: Defining current date in batch file
            « Reply #11 on: October 15, 2009, 08:21:30 AM »
            Hi Oldun - Thanks so much for the reply.  I finally got some time to play with it this morning and it worked perfectly.  No more manual FTP'ing.  Thanks so much!!!!