Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Batch file FTP transfer with name change

Posted on 2012-03-22
27
Medium Priority
?
789 Views
Last Modified: 2012-03-29
Hello everyone,

I am need help getting a script that will transfer a file from our server to an remote ftp server.

The script should do the following:
 Take a file from a folder our server and rename the file appending a date/time stamp format and move it to an archive folder.
It then would leave a copy of the newly renamed file and send a copy of the file to an FTP address. The FTP site has a folder we need to drop it in.  The FTP site has a user name and pw that has to be used.

Conventions:
The filename for the example script should be filename.txt
The file will start off being located at \\servername\foldername
Then moved and renamed to \\servername\archive
The changed name format would be yearmonthdate-timefilename.txt
ie 20120322-0948filename.txt
for the user name please use USERNAME
for the password please use PASSWORD

This script will be kicked off every five minutes by the task scheduler.
The file resides on a server using Server 2003 Standard.
The file server has an ip address.

Thanks for the help
0
Comment
Question by:POB-IT-DEPT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 17
  • 10
27 Comments
 
LVL 11

Expert Comment

by:paultomasi
ID: 37761265
The changed name format would be yearmonthdate-timefilename.txt

You mean:

    The changed name format would be yearmonthday-timefilename.txt
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37761267
What's your date format? (what do you see when you enter the command ECHO %DATE% in DOS)
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37761373
Try this.

NOTE: Assumes date format = DD/MM/YYYY
@echo off
set "ftpaddress=ftpaddress"
set "remotefolder=folder"
set "username=USERNAME"
set "password=PASSWORD"

set "sourcefile=filename.txt"
set "sourcefolder=foldername"
set "destinationfolder=archive"

for /f "tokens=1-3 delims=/" %%a in ('date /t') do set "datestamp=%%c%%b%%a"
for /f "tokens=1-2 delims=:" %%a in ('time /t') do set "timestamp=%%a%%b"

set "timestamp=%timestamp: =0%"
set "datestamp-%datestamp%-%timestamp%"
set "destinationfile=%datestamp%%sourcefile%"

move /y "\%sourcefolder%\%sourcefile%" "\%destinationfolder%\%destinationfile%"

(
  echo %username%
  echo %password%
  echo binary
  echo prompt
  echo lcd "%destinationfolder%"
  echo cd "%remotefolder%"
  echo put "%destinationfile%"
  echo bye
)>"%temp%\ftpscript.tmp"

ftp -s:"%temp%\ftpscript.tmp" %ftpaddress%

del "%temp%\ftpscript.tmp"
exit

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:POB-IT-DEPT
ID: 37766332
As far as the date format question, when I put in Echo %date%, I get the following:

Mon 03/26/2012
0
 

Author Comment

by:POB-IT-DEPT
ID: 37766468
I just ran the batch file and it returned the following message:
Enviroment variable datestamp-2012 26Mon not defined
The filename, directory name or volume label syntax is incorrect.
Press any key to continue.

I put a pause before the end of the program. I also placed a rem in front of the ftp part to check the naming convention portion.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37767312
Change line 11 to:

for /f "tokens=2-4 delims=/ " %%a in ('date /t') do set "datestamp=%%c%%a%%b"

I will address the error message in my next post.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37767382
Please try the following:
@echo off
set "ftpaddress=ftpaddress"
set "remotefolder=folder"
set "username=USERNAME"
set "password=PASSWORD"

set "sourcefile=filename.txt"
set "drive=c:"
set "sourcefolder=%drive%\foldername"
set "destinationfolder=%drive%\archive"

for /f "tokens=2-4 delims=/ " %%a in ('date /t') do set "datestamp=%%c%%a%%b"
for /f "tokens=1-2 delims=:" %%a in ('time /t') do set "timestamp=%%a%%b"

set "timestamp=%timestamp: =0%"
set "datestamp=%datestamp%-%timestamp%"
set "destinationfile=%datestamp%%sourcefile%"

move /y "%sourcefolder%\%sourcefile%" "%destinationfolder%\%destinationfile%"

(
  echo %username%
  echo %password%
  echo binary
  echo prompt
  echo lcd "%destinationfolder%"
  echo cd "%remotefolder%"
  echo put "%destinationfile%"
  echo bye
)>"%temp%\ftpscript.tmp"

ftp -s:"%temp%\ftpscript.tmp" %ftpaddress%

del "%temp%\ftpscript.tmp"
exit /b

Open in new window

NOTE: You will need to add your own values to lines 2 through 10.
0
 

Author Comment

by:POB-IT-DEPT
ID: 37768625
We are making some progress. The resulting name of the file was tagged:
20120326-04080PM .  It got the date just fine. The time is strange. It was sent at 2:55 pm.
So I am not sure what is going on there. Also file didn't get transferred onto the ftp server under its folder.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37769154
You're using a 12-hour clock then.

Instead of something like '13:05' your system shows '1:05 pm' but you still want your timestamp to show '1305' and '0935' if the time is '9:35 am'. Is that right?

Okay, let me make some adjustments...
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37770931
Please try this:
@echo off
set "ftpaddress=ftpaddress"
set "remotefolder=folder"
set "username=USERNAME"
set "password=PASSWORD"

set "sourcefile=filename.txt"
set "drive=c:"
set "sourcefolder=%drive%\foldername"
set "destinationfolder=%drive%\archive"

for /f "tokens=2-4 delims=/ " %%a in ('date /t') do set "datestamp=%%c%%a%%b"
for /f "tokens=1-3 delims=: " %%a in ('time /t') do (
  set /a hour=%%a
  set "minute=%%b"
  set "meridiem=%%c"
)

if /i "%meridiem%" equ "am" (
  if %hour% equ 12
    set "hour=00"
  ) else (
    if %hour% lss 10 set "hour=0%hour%"
  )
) else (
  if %hour% lss 12 set /a hour+=12
)

set "datestamp=%datestamp%-%hour%%minute%"
set "destinationfile=%datestamp%%sourcefile%"

move /y "%sourcefolder%\%sourcefile%" "%destinationfolder%\%destinationfile%"

(
  echo %username%
  echo %password%
  echo binary
  echo prompt
  echo lcd "%destinationfolder%"
  echo cd "%remotefolder%"
  echo put "%destinationfile%"
  echo bye
)>"%temp%\ftpscript.tmp"

ftp -s:"%temp%\ftpscript.tmp" %ftpaddress%

del "%temp%\ftpscript.tmp"
exit /b

Open in new window

0
 

Author Comment

by:POB-IT-DEPT
ID: 37771718
It isn't moving the source file to the destination. In fact it has to be failing before the move on line 32 because I put a pause in there so I could see what the file would be renamed to before the ftp process tries to transfer the file. It never made it to the pause.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37772063
Oh, I do apologise.

I missed an opening bracket on line 20. Line 20 should read as follows:

  if %hour% equ 12 (
0
 

Author Comment

by:POB-IT-DEPT
ID: 37772180
Ok some more progress. It is moving the file to destination folder, but it isn't ftp'ing the file.
0
 

Author Comment

by:POB-IT-DEPT
ID: 37773878
Also is there a way of showing the seconds on the file name when renaming it?
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37774049
Yes, the seconds is no problem.... What do you see when you type the followig command in a DOS session?

    ECHO %TIME%
0
 

Author Comment

by:POB-IT-DEPT
ID: 37774101
I get 16:53:29.29
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37774121
With regard to the AM/PM meridiem value, do you really want the AM/PM as part of the filename? If so, what format would you prefer ie:

    yyyymmdd-hhmm am     - space, followed by lowercase 'am' (and 'pm')
    yyyymmdd-hhmm AM     - space, followed by uppercase 'AM' (and 'PM')
    yyyymmdd-hhmmam      - no space, specify preferred case
    yyyymmdd-hhmm-am     - hyphen, followed by meridiem value. Specify preferred case
    etc...

Or specify a preferred format.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37774126
Thanks for the quick respnse confirming your TIME setting.
0
 

Author Comment

by:POB-IT-DEPT
ID: 37774127
24 hour format is fine. I dont have to have the am/pm. But I will need the seconds included
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37774136
No probs... Give me a mo...
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37774201
How does this look?

Can you please confirm the file is copied to it's new location with the correct filename and datestamp?
@echo off
set "ftpaddress=ftpaddress"
set "remotefolder=folder"
set "username=USERNAME"
set "password=PASSWORD"

set "sourcefile=filename.txt"
set "drive=c:"
set "sourcefolder=%drive%\foldername"
set "destinationfolder=%drive%\archive"

for /f "tokens=2-4 delims=/ " %%a in ('date /t') do set "datestamp=%%c%%a%%b"

set "timestamp=%time%"
set "timestamp=%timestamp: =0%"
set "timestamp=%timestamp::=%"
set "datestamp=%datestamp%-%timestamp:~0,6%"

set "destinationfile=%datestamp%%sourcefile%"

move /y "%sourcefolder%\%sourcefile%" "%destinationfolder%\%destinationfile%"

(
  echo %username%
  echo %password%
  echo binary
  echo prompt
  echo lcd "%destinationfolder%"
  echo cd "%remotefolder%"
  echo put "%destinationfile%"
  echo bye
)>"%temp%\ftpscript.tmp"

ftp -s:"%temp%\ftpscript.tmp" %ftpaddress%

del "%temp%\ftpscript.tmp"
exit /b

Open in new window

If we can agree on the copy, filename and datestamp then we can proceed with the FTP part.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37774443
With regard to the FTP part...

What instructions would you normally type at the command prompt to FTP your file?

Is it something like this:

    FTP ftp-address   (or IP Address)

You might then be prompted for a User ID so you enter something like this:

    USERID

You might then be prompted for a Password so you enter something like this:

    PASSWORD

Hopefully, at this point, you are logged into your FTP site.

Then, you need to navigate to where the file is located on your hard drive so you might do something like this:

    LCD "localfolder"

Then you might need to navigate to where the file is to be sent so you might do something like this:

    CD "remotefolder"

(might require '/' instead of '\' path-separators)

Then you might transfer the file like this:

    PUT filename

(Use double-quotes if there are spaces in the filename)

And that might be all that is necessary so you might at this point end the FTP session like this:

    BYE

Which should return you to DOS.

Can you please manually trasnfer a file (something like what I've shown you above) and tell me if you were successful and what commands you had to issue in doing so. I can then code these into the batch file.

(Use: USERID for User ID, PASSWORD for Password and FTPADDRESS for the FTP Address when replying).

NOTE: Other commands might include, ASCII, BINARY, QUIT, PROMPT
0
 

Author Comment

by:POB-IT-DEPT
ID: 37778835
Ok this is where we are...
I ran the script to get the results. I had inserted a pause to capture the results of the script. I see that it connects to the ftp site. Then it states a password is needed for account. Then it says 530 login failed. Then it start putting in the other echo inputs like binary. Needless to say it didn't work.

I did do it manually and this is how it connects to the ftp server:
1. at a command prompt I type ftp
2. Then i type in "open ftpaddress" The ftp address is an ip address
3. then I type in the user name at the prompt
4. then it tells me I must put in a password for the user name
5. I put in the password and hit enter
6. Then i put in the lcd "local filepath"
7. Then I put in "cd remotefolder"
8. Then I "put filename"
9. It tells me I am sucessful
10 then I type bye and the session is over.

BTW on the file name it does the file name like this:
15060P for the time. weird...
0
 
LVL 11

Accepted Solution

by:
paultomasi earned 2000 total points
ID: 37780785
The timestamp '15060P' is unexpected. Given your time '16:53:29.29' in an earlier comment, the result would be '165329' (the '29' is taken from the first '29' in '16:53:29.29' not the '29' after the full-stop). So something is amiss here.

I have rewritten previous lines 13 through 19. See lines 13 through 16 in the code below. This will work with a wider range of date formats.

Here's the modified code (as before, you will need to add you own custom values in lines 4 through 12 again):
@echo off
set "ftpaddress=ftpaddress"
set "remotefolder=folder"
set "username=USERNAME"
set "password=PASSWORD"

set "sourcefile=filename.txt"
set "drive=c:"
set "sourcefolder=%drive%\foldername"
set "destinationfolder=%drive%\archive"

for /f "tokens=2-4 delims=/ " %%a in ('date /t') do set "datestamp=%%c%%a%%b"
for /f "tokens=1-3 delims=:. " %%a in ("%time%") do set "hh=%%a" & set "mmss=%%b%%c"

set "hh=0!hh!"
set "destinationfile=%datestamp%-%hh:~-2,2%%mmss%%sourcefile%"

move /y "%sourcefolder%\%sourcefile%" "%destinationfolder%\%destinationfile%"

(
  echo %username%
  echo %password%
  echo binary
  echo prompt
  echo lcd "%destinationfolder%"
  echo cd "%remotefolder%"
  echo put "%destinationfile%"
  echo bye
)>"%temp%\ftpscript.tmp"

ftp -s:"%temp%\ftpscript.tmp" %ftpaddress%

del "%temp%\ftpscript.tmp"
exit /b

Open in new window

Just a thought. Would you prefer the filename's format to include a hyphen ('-') between the time and orignal filename - like this:

    YYYYMMDD-HHMMSS-filename.ext

instead of:

    YYYYMMDD-HHMMSSfilename.ext

If so, then replace line 16 with the following line:

    set "destinationfile=%datestamp%-%hh:~-2,2%%mmss%-%sourcefile%"
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37781508
For the FTP part, let's concentrate on lines 2, 4 and 5 for the moment so that we can establish a connection.

LINE 2
This should be 'set ftpaddress=' followed immediately by whatever you normally type in after the FTP command in DOS. So for example, if you were logging into 'www.bbc.co.uk' you would change this line to:

    set "ftpaddress=ftp.bbc.co.uk"

So, the prefix 'www' is replaced with 'ftp'.

NOTE: Strictly speaking, you don't need to include double-quotes. The following will work equally well:

    set ftpaddress=ftp.bbc.co.uk

There should be no spaces other than the one separating the 'set' command and the variable-name 'ftpaddress'


LINE 4
This should be 'set username=' followed immediately by whatever username you normally enter when logging into your FTP site. So for example, if your username is 'PobITDept' you should change this line to:

    set "username=PobITDept"

As before, ensure there are spaces either side of the equal ('=') sign.


LINE 5
This should be 'set password=' followed immediately by whatever password you normally enter when logging into your FTP site. So for example, if your password is 'LetMeIn' you should change this line to:

    set "password=LetMeIn"

Again, don't include spaces. Also, remember, you don't strictly need to include the double-quotes however, it does reduce the likelyhood of 'invisible' trailing spaces being present.


Once you have made these changes, try running the batch file without lines 23 through 27. If you are still having problems successfully logging in at this stage then we can try a more diagnostic approach.

Be mindful that the rest of code will still execute in particular, line 18 and line 33 so you may want to 'REM' these out during this phase (insert 'rem ' at the start of the line to prevent them from being executed).

When you run the batch file as instructed above, you should immediately be returned to DOS without any errormessages. Let me know how you get on.
0
 

Author Closing Comment

by:POB-IT-DEPT
ID: 37782189
Between what you have written and some trial and error, it is now transferring the files.
Thanks for the assistance and the follow-through.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37783318
EXCELLENT !!

I'm so pleased we got there in the end...

Thank you too !!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to set permissions to allow a limited-permissions user to start and stop a particular System Service.   It is always best to give users only the permissions that they need to perform their job, so tweaking particular permi…
The password reset disk is often mentioned as the best solution to deal with the lost Windows password problem. In Windows 2008, 7, Vista and XP, a password reset disk can be easily created. But besides Windows 7/Vista/XP, Windows Server 2008 and ot…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question