[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 406
  • Last Modified:

Need a BATCH file to select file by name then FTP it to location, overwriting what's there

I have a directory holding an MP3 file for each day. (ie: Feb09.mp3, Feb25.mp3 etc.)
I need a batch file that will determine the current date....
   Grab the file for the current date....
     and FTP it to a location OVERWRITING (without confirmation) what is there.
  The destination file name will ALWAYS BE  rw.mp3
I'll run this via the task scheduler each night.
Can someone show me the Batch code that will do that?
0
rbend
Asked:
rbend
  • 7
  • 6
1 Solution
 
Justin_W_ChandlerCommented:
for /f "tokens=2*" %A in ('date /t') do echo %A>%temp%\stepA.tmp&for /f "tokens=1,2* delims=/" %B in (%temp%\stepA.tmp) do set var_day=%C&set var_month=%B

This will give you the variables var_day and var_month. You can then run a for loop to change the var_month from a numeric value to a text value (i.e. 02 = Feb). Once you've done that, concatenate the values into another variable (i.e. set var_todayfile=%var_monthtext%%var_day%.mp3) and use "ftp /s uploadscript.txt") to upload the file via FTP to your server.
0
 
rbendAuthor Commented:
Justin:
I can offer you 50 more (my limit) if you construct the whole code block ?
0
 
t0t0Commented:
This sould do the job for your however, you must first edit it to include your own specific details such as the name of the folder containing your mp3 files ie, c:\my documents\mp3files, etc...


@rem ===================================================================
@rem FTPMP3.BAT - Written by Paul Tomasi 2009
@rem
@rem FTP current day's mp3 file ie, Feb22.mp3 ---> rw.mp3
@rem ===================================================================
@echo off
setlocal enabledelayedexpansion

rem ===================================================================
rem Set the following variables to apporpriate values
rem by replacing everything inside, and including {...}
rem ===================================================================
set ftpsite={replace with address of ftp site}
set username={replace with username}
set password={replace with password}
set mp3folder={replace with source folder}
set ftpfolder={replace with destination folder}
set tempfolder=%TEMP%
set ftpscriptfile=ftpscript.txt

rem ===================================================================
rem Get the name of today's mp3 file
rem ===================================================================
set day=%date:~0,2%
set month=%date:~3,2%
set months=JanFebMarAprMayJunJulAugSepOctNovDec
for /l %%i in (1,1,%month%) do (
   set month=!months:~0,3!
   set months=!months:~3!
)
set mp3file=%month%%day%.mp3

rem ===================================================================
rem prepare mp3file for sending
rem ===================================================================
copy "%mp3folder%\%mp3file%" "%mp3folder%\rw.mp3"

rem ===================================================================
rem Make the ftp script file
rem ===================================================================
echo %username% > "%tempfolder%\%ftpscriptfile%"
echo %password% >> "%tempfolder%\%ftpscriptfile%"
echo lcd "%mp3folder%" >> "%tempfolder%\%ftpscriptfile%"
echo cd "%ftpfolder%" >> "%tempfolder%\%ftpscriptfile%"
echo put rw.mp3 >> "%tempfolder%\%ftpscriptfile%"
echo bye >> "%tempfolder%\%ftpscriptfile%"

rem ===================================================================
rem Ftp the file
rem ===================================================================
ftp -s:"%tempfolder%\%ftpscriptfile%" %ftpsite%

rem ===================================================================
rem Cleanup
rem ===================================================================
del "%tempfolder%\%ftpscriptfile%"
del %mp3folder%\rw.mp3
for %%a in (months month day mp3file username password) do set %%a=
for %%a in (ftpscriptfile ftpsite mp3folder ftpfolder tempfolder) do set %%a=
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Justin_W_ChandlerCommented:
His will work but I recommend using mine for these reasons:
1) His script might not work without deleting the old rw.mp3 in the ftp.
2) Mine uploads the file first, then renames it to the appropriate name.
     (Hence, there is virtually no down time during the upload.)
3) The upload is initiated in a separate minimized window so your work isn't interrupted.

@echo off
title FTPMP3
mode con:cols=15 lines=1
cmd /x

set var_ftpserver=FTP.SERVER.COM
set var_localpath=C:\MP3STORAGE
set var_remotepath=MP3FOLDER\CURRENTMP3
set var_username=FTPLOGON
set var_password=FTPPASSWORD

%var_localpath:~0,2%
cd %var_localpath:~2%
call :proc_setup
for /f "tokens=2*" %%A in ('date /t') do echo %%A>%temp%\stepA.tmp&for /f "tokens=1,2* delims=/" %%B in (%temp%\stepA.tmp) do set var_day=%%C&set var_month=%%B
set /a var_month=%var_month%+0
for /f "tokens=%var_month%" %%A in ("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec") do set var_month=%%A
copy /y /v %var_month%%var_day%.mp3 rwnew.mp3
start /min /low /d%var_localpath% cmd /c ftp -s:ftpmp3.cfg %var_ftpserver%
goto :eof
goto end

:proc_setup
echo %var_username%>ftpmp3.cfg
echo %var_password%>>ftpmp3.cfg
echo lcd %var_localpath%>>ftpmp3.cfg
echo cd %var_remotepath%>>ftpmp3.cfg
echo put rwnew.mp3>>ftpmp3.cfg
echo delete rw.mp3>>ftpmp3.cfg
echo rename rwnew.mp3 rw.mp3>>ftpmp3.cfg
echo quit>>ftpmp3.cfg
goto :eof

:end

FTPMP3.txt
0
 
t0t0Commented:
rbend
Please try the following solution to your problem....

Justin_W_Chandler
I have tried your code and it does not work. The code below seems to do the job nicely.


:: ====================================================
:: FTPMP3.BAT v2 - Written by Paul Tomasi 2009
::
:: FTP current day's mp3 file ie, Feb22.mp3 ---> rw.mp3
:: ====================================================
@ECHO OFF
CALL :initialise_variables
CALL :get_mp3_file
CALL :make_script_file
CALL :send_mp3_file
CALL :reset_variables
EXIT /B

::------------------------------------------------------------------------------
:: INTIALISE VARIABLES
::
:: Set the following variables to apporpriate values
:: by replacing everything inside, and including [...]
::------------------------------------------------------------------------------
:initialise_variables
SET source="[replace with MP3 source folder]"
SET ftpsite="[replace with address of ftp site]"
SET username=[replace with username]
SET password=[replace with password]
SET destination=[replace with FTP destination folder]
EXIT /B

::------------------------------------------------------------------------------
:: GET MP3 FILE
::------------------------------------------------------------------------------
:get_mp3_file
SET day=%DATE:~0,2%
SET month=%DATE:~3,2%
SET monthnames="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
FOR /F "TOKENS=%month%" %%a IN (%monthnames%) DO SET month=%%a
COPY %source%\%month%%day%.mp3 %source%\rw.mp3 >NUL
EXIT /B

::------------------------------------------------------------------------------
:: MAKE SCRIPT FILE
::------------------------------------------------------------------------------
:make_script_file
>>script.ftp ECHO %username%
>>script.ftp ECHO %password%
>>script.ftp ECHO LCD %source%
>>script.ftp echo mkdir %destination%
>>script.ftp ECHO CD %destination%
>>script.ftp ECHO PUT rw.mp3
>>script.ftp ECHO BYE
EXIT /B

::------------------------------------------------------------------------------
:: SEND MP3 FILE
::------------------------------------------------------------------------------
:send_mp3_file
FTP -s:script.ftp %ftpsite%
TYPE NUL >script.ftp
DEL script.ftp
DEL %source%\rw.mp3
EXIT /B

::------------------------------------------------------------------------------
:: RESET VARIABLES
::------------------------------------------------------------------------------
:reset_variables
SET day=
SET month=
SET ftpsite=
SET username=
SET password=
SET ftpfolder=
SET monthnames=
EXIT /B
0
 
t0t0Commented:
Oops!! Sorry, just removed a test line from the Make Script File routine...


:: ====================================================
:: FTPMP3.BAT v2 - Written by Paul Tomasi 2009
::
:: FTP current day's mp3 file ie, Feb22.mp3 ---> rw.mp3
:: ====================================================
@ECHO OFF
CALL :initialise_variables
CALL :get_mp3_file
CALL :make_script_file
CALL :send_mp3_file
CALL :reset_variables
EXIT /B

::------------------------------------------------------------------------------
:: INTIALISE VARIABLES
::
:: Set the following variables to apporpriate values
:: by replacing everything inside, and including [...]
::------------------------------------------------------------------------------
:initialise_variables
SET source="[replace with MP3 source folder]"
SET ftpsite="[replace with address of ftp site]"
SET username=[replace with username]
SET password=[replace with password]
SET destination=[replace with FTP destination folder]
EXIT /B

::------------------------------------------------------------------------------
:: GET MP3 FILE
::------------------------------------------------------------------------------
:get_mp3_file
SET day=%DATE:~0,2%
SET month=%DATE:~3,2%
SET monthnames="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
FOR /F "TOKENS=%month%" %%a IN (%monthnames%) DO SET month=%%a
COPY %source%\%month%%day%.mp3 %source%\rw.mp3 >NUL
EXIT /B

::------------------------------------------------------------------------------
:: MAKE SCRIPT FILE
::------------------------------------------------------------------------------
:make_script_file
>>script.ftp ECHO %username%
>>script.ftp ECHO %password%
>>script.ftp ECHO LCD %source%
>>script.ftp ECHO CD %destination%
>>script.ftp ECHO PUT rw.mp3
>>script.ftp ECHO BYE
EXIT /B

::------------------------------------------------------------------------------
:: SEND MP3 FILE
::------------------------------------------------------------------------------
:send_mp3_file
FTP -s:script.ftp %ftpsite%
TYPE NUL >script.ftp
DEL script.ftp
DEL %source%\rw.mp3
EXIT /B

::------------------------------------------------------------------------------
:: RESET VARIABLES
::------------------------------------------------------------------------------
:reset_variables
SET day=
SET month=
SET ftpsite=
SET username=
SET password=
SET ftpfolder=
SET monthnames=
EXIT /B
0
 
Justin_W_ChandlerCommented:
I'm pretty sure my script should work although I didn't test it (FTP is disallowed here at work) I'll clean it up at home and fix whatever minor syntax problem there is.

EDIT: I think I got it -- remove CMD /X from the file and try again. sorry, BRAIN FART :)

I'll still test it in a couple of hours while I'm at home and have access to an online FTP server.
0
 
t0t0Commented:
Justin_W_Chandler

Will your code going be an improvement on my code though? I mean, if you think my code can be improved then please state how.... afterall, I won't mind splitting points with you.

My code is below:


:: ====================================================
:: FTPMP3.BAT v2 - Written by Paul Tomasi 2009
::
:: FTP current day's mp3 file ie, Feb22.mp3 ---> rw.mp3
:: ====================================================
@ECHO OFF
CALL :initialise_variables
CALL :get_mp3_file
CALL :make_script_file
CALL :send_mp3_file
CALL :reset_variables
EXIT /B

::------------------------------------------------------------------------------
:: INTIALISE VARIABLES
::
:: Set the following variables to apporpriate values
:: by replacing everything inside, and including [...]
::------------------------------------------------------------------------------
:initialise_variables
SET source="[replace with MP3 source folder]"
SET ftpsite="[replace with address of ftp site]"
SET username=[replace with username]
SET password=[replace with password]
SET destination=[replace with FTP destination folder]
EXIT /B

::------------------------------------------------------------------------------
:: GET MP3 FILE
::------------------------------------------------------------------------------
:get_mp3_file
SET day=%DATE:~0,2%
SET month=%DATE:~3,2%
SET monthnames="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
FOR /F "TOKENS=%month%" %%a IN (%monthnames%) DO SET month=%%a
COPY %source%\%month%%day%.mp3 %source%\rw.mp3 >NUL
EXIT /B

::------------------------------------------------------------------------------
:: MAKE SCRIPT FILE
::------------------------------------------------------------------------------
:make_script_file
>>script.ftp ECHO %username%
>>script.ftp ECHO %password%
>>script.ftp ECHO LCD %source%
>>script.ftp ECHO CD %destination%
>>script.ftp ECHO PUT rw.mp3
>>script.ftp ECHO BYE
EXIT /B

::------------------------------------------------------------------------------
:: SEND MP3 FILE
::------------------------------------------------------------------------------
:send_mp3_file
FTP -s:script.ftp %ftpsite%
TYPE NUL >script.ftp
DEL script.ftp
DEL %source%\rw.mp3
EXIT /B

::------------------------------------------------------------------------------
:: RESET VARIABLES
::------------------------------------------------------------------------------
:reset_variables
SET day=
SET month=
SET ftpsite=
SET username=
SET password=
SET ftpfolder=
SET monthnames=
EXIT /B
0
 
Justin_W_ChandlerCommented:
No I'm not saying your code has anything wrong with it. Both solutions should work -- the changes I made were listed in my above comment.

QUOTE:
His will work but I recommend using mine for these reasons:
1) His script might not work without deleting the old rw.mp3 in the ftp.
2) Mine uploads the file first, then renames it to the appropriate name.
(Hence, there is virtually no down time during the upload.)
3) The upload is initiated in a separate minimized window so your work isn't interrupted.

Here are some points that I did take note of -- give them a thought:

* I did notice you already replaced your code for getting the MP3 name with mine. Check the other improvements above.

* I noticed you're grabbing some parts of an environment variable DATE that is not being set anywhere in the script. It won't work like that. You could initialize the DATE variable with for /f  "tokens=*" %%A in ('date /t') do set DATE=%%A

* There is no need to reset the variables, as they will be gone when the script ends.
0
 
t0t0Commented:
Justin_W_Chandler

1) good point. rw.mp3 should copy to the remote PC over-writing the prevoius rw.mp3 file.
2) hmmm... interesting, however, it means more 'up' time... best to do all the work locally.
3) unecessary.

Furthermore... you are right about the improved FOR loop however, programming is both a learning process and a creative process. i give credit to you for the monthly name extraction. perhaps you could learn something from my date extraction.

%DATE% is a system variable and is set automatically by the system at the time it is used as is %TIME% which will always return the precise time. try the following at the command prompt:

   ECHO %TIME%

See what i maen?

Finally, i felt it may be necessary to reset the variables if my code is to be used as part of a larger batch file or CALLed from within another batch file. I'm not going to be picky about this detail as the user will judge whether or not he requires variables to be reset. anyway, it's there for completeness incase the user continues to work from the same console.

there's no right or wrong here. what is interesting though is that by taking the best bits from everyone's code, we end up with a best-all-round solution. and i'm sure you'll agree the user would like nothing less.
0
 
Justin_W_ChandlerCommented:
It doesn't mean any more "UP" time... apart from the fraction of a second it takes to send the "rename" command to the FTP server. By uploading and overwriting the existing file, you make that file inaccessible for however long the upload takes [which could take a while].

We don't know the system use. If the system is being used or monitored, it's a hassle for it to pop up maximized. It's just a nice feature that is not necessary and takes no additional lines of code. The question here isnt' "why?" but "why not?".

I know very well how to use environment variable cropping. I choose not to use those dynamic variables because the TIME variable does not produce double digit hours less than 10. Hence, I keep a uniform style of using the date and time command instead of the time command and the date environment variable.

As I stated, your script could definitely work with some corrections. Mine is just a bit more polished in it's function. Grab the improvements and put them in your script and call it a day.
0
 
t0t0Commented:
Chandler

>> "...the TIME variable does not produce double digit hours less than 10"

I don't for one moment buy the fact you don't use %TIME% for the reasons you state (ID: 23745591) as you had no idea about the mechanics of %DATE%, and possibly %TIME%, when you stated (ID: 23739842):

>> "I noticed you're grabbing some parts of an environment variable DATE that is not being set anywhere in the script"

As far as I can make out, you are NOT a programmer and you have VERY little knowledge of DOS batch files so please don't critisise my code unless you are qualified to do so.

My advice to you is: Stick to something you know something about.

Anyway, what's wrong with:

   SET t=0%time: =%
   SET t=%t:~-11%

Obviously you're not familiar with this.

>> "As I stated, your script could definitely work with some corrections"

Refering to my batch file (ID: 23739538), what corrections??

>> "Mine is just a bit more polished..."

I've looked at your code and there is nothing there that I find particularly inspiring. In fact, it's quite sloppy in places and it's overall appearance makes it difficult to read.

>> "Grab the improvements and put them in your script and call it a day"

You are an arrogant and abnoxious person and I personally dislike your attitude.

I have searched EE and read through every comment of yours under your current ID and your previous ID of chemao.

You are not a programmer. Your style of code suggests you are s novice and you are meddling in a subject you have very little experience in. Please leave the programming to the real experts and stick to something you know.
0
 
Justin_W_ChandlerCommented:
"You are not a programmer. Your style of code suggests you are s novice and you are meddling in a subject you have very little experience in. Please leave the programming to the real experts and stick to something you know."

Neither of us has been put in any situation that requires advanced batch scripting. You haven't seen anything yet. Just one of the scripts I've built is a 1000+ line batch script. It does a lot of really fun stuff, including automatic update, DNS queries, modificaitons, remote scheduling, window manipulation and file transfer monitoring. It also has a full menu system, with an options page that allows you to customize how reports are formatted, active directory is queried, and data is validated or invalidated. Not only that, but it also launches child batches with multi-threaded network scans (number of threads is also configurable). The batch file is permissions-aware and will/will not perform functions based on group membership. Last but not least, the batch file is encrypted by another batch that pulls an encryption key from a permissions secured share. Both are encrypted with random names and double-archived in two solid, locked SFX executables packaged with a custom icon.

"Obviously you're not familiar with this."
SET t=0%time: =%
SET t=%t:~-11%

The first is a substitution to remove spaces and the second simply takes the data counting from the rear 11 characters.

"I've looked at your code and there is nothing there that I find particularly inspiring. In fact, it's quite sloppy in places and it's overall appearance makes it difficult to read."

Just because I don't comment everything in my batch file doesn't mean it's hard to read. If it's difficult for you to read then by all means make an inquiry and I'll clarify what it does for you.

My mistake about %date%, I was moving very fast and it completely slipped my mind. I rarely use the date and time variables -- as I prefer to keep uniform style, and since I rarely use the %time% variable, I avoid the %date% variable as a matter of consistency. Nice idea.
Justin Chandler
0
 
t0t0Commented:
rbend,
I cannot believe you chose the tripe that was submitted by Justin_W_Chandler - especially as it did not work when I myself tested in a REAL working and fully simulated environment.

You were obviously blinded by Justin_W_Chandler's comments which he made slating my code. This was a common practice of Justin_W_Chandler which he used during his brief period as an 'expert' (and I use that term VERY loosely) and which had him kicked off Experts-Exchange.

The code I presented in my reply (ID: 23739538) is about the most structured and professionally written code you're ever likely to see anywhere on EE - or anywhere else for that matter. Believe me, they don't come better than that!

You obviously failed to acknowledge my reply to Justin_W_Chandler in my later comment (ID: 23815764).

PLEASE READ THIS

If your decision to accept Justin_W_Chandler's code is based purely on the need to close this question (without having weighed up the merits of my effort) then I urge you to change your decision and to contact a moderator with a request to amend your decision.

If, on the other hand, you can put your hand on your heart and say that Justin_W_Chandler's code (which you awarded points for) ACTUALLY works (which I know does NOT) then I ask you to reopen the question and at least award me some credit for my extensive time and effort by pronouncing me as ASSISTED in the solution.

Finally, if you can put your hand on your heart and say unequivocably that the code which you accepted as your solution actually works (without having to make any corrections to it) AND that your working solution is actually based on that code, then I'll leave it there and hang my head in shame at the thought of what this country must be coming to....

Please, rbend, show me that you have given this your careful attention.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now