Automate – Rename file with yesterdays Date Stamp and FTP

Today I was asked if we could automate something where a file would be FTP’d to the clients FTP server.

My answer was sure that’s as easy as pie. Then the finer details came to light.

Problem

So the the details are:

the file changes every day and must have a filename followed by the date stamp at the end of the file name.

For example: file_24042008.txt

that’s easy enough to do, But then I was told that it needed to use the following days date stamp not the current day.

Now it just became a little bit harder

Solution

Being that I am dumping the file to the clients FTP, this makes it a little easier or give me a little bit more control, As the output of the file will just be: file.txt  with that then I can script a rename of that file to: file_24042008.txt

So everyday the file.txt would be dumped in a specific directory and then renamed to the file_yesterdays date here.txt 

here is the code that was used for that part

(code was taken and modified from a user on Experts Exchange, thanks for that if your reading)

 

@echo off

set yyyy=

set $tok=1-3
for /f “tokens=1 delims=.:/-, “ %%u in (‘date /t’) do set $d1=%%u
if “%$d1:~0,1%” GTR “9″ set $tok=2-4
for /f “tokens=%$tok% delims=.:/-, “ %%u in (‘date /t’) do (
for /f “skip=1 tokens=2-4 delims=/-,().” %%x in (‘echo.^|date’) do (
    set %%x=%%u
    set %%y=%%v
    set %%z=%%w
    set $d1=
    set $tok=))

if “%yyyy%”==“” set yyyy=%yy%
if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% – 100

set CurDate=%mm%/%dd%/%yyyy%

REM Substract your days here
set /A dd=1%dd% – 100 – 1
set /A mm=1%mm% – 100

:CHKDAY

if /I %dd% GTR 0 goto DONE

set /A mm=%mm% – 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yyyy=%yyyy% – 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

set /A tt=%yyyy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yyyy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yyyy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY
:D ONE

if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

rename “C:\Users\bradm\Desktop\ideas.txt” “ideas_%dd%-%mm%-%yyyy%.txt”

This script allows the change of the file name, with this all you have to change is:
rename "C:\Users\bradm\Desktop\ideas.txt" "ideas_%dd%-%mm%-%yyyy%.txt" 

The Next Part (FTP script)

Ok here we need to create some thing that dumps this to the FTP, again we are scripting this.

typically with in command line FTP you cant pass a variable, so we have to do all of that before we get to the FTP

So being that we are using a different script (there is no reason why you just cant do this on one script, I am just breaking it down) we then use the same as above but with a few additions

set yyyy=

set $tok=1-3
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u
if "%$d1:~0,1%" GTR "9" set $tok=2-4
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
 for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (
    set %%x=%%u
    set %%y=%%v
    set %%z=%%w
    set $d1=
    set $tok=))

if "%yyyy%"=="" set yyyy=%yy%
if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% - 100

set CurDate=%mm%/%dd%/%yyyy%

REM Substract your days here
set /A dd=1%dd% - 100 - 1
set /A mm=1%mm% - 100

:CHKDAY

if /I %dd% GTR 0 goto DONE

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yyyy=%yyyy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

set /A tt=%yyyy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yyyy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yyyy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY
 :D ONE

if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

echo YourFTPUserHere>ftp.txt
echo yourFTPpasswordHere>>ftp.txt
echo cd C:\Users\bradm\Desktop\>>ftp.txt
echo put file_%dd%-%mm%-%yyyy%.txt>>ftp.txt
echo bye>>ftp.txt
ftp -i -s:.\ftp.txt ftp.youraddress.com.au

As you can see its using the same code above,  toward the end we are simple creating the FTP.txt that will be used to upload the file.  All you need to do is change the user, pass path and file.

If you are using these as 2 separate scripts make sure you enter a ‘call’ statement to your FTP script.


2 Responses to “Automate – Rename file with yesterdays Date Stamp and FTP”

Leave a Reply