Link to home
Start Free TrialLog in
Avatar of isames
isames

asked on

Batch File Code

I am using Windows 2008R2. I would like to write a batch file that grabs the latest(date) text file created in the directory, and uploads it to a FTP site. I would then schedule the batch file to run at the end of business everyday.

So I would like to go to this directory "C:\Invoice\Invoicedata" and grab the newest file. Then I would like it be uploaded to a FTP site that requires username and password. Then I would like to schedule it to run at 6pm everyday.
Avatar of Bill Prew
Bill Prew

Can you install a free third party utility?

~bp
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Il rather use Winscp it can do that , since with time you may need more complexity on the script , but anyway here is some code

open 8.8.8.8
username
password
set /p DIRR="C:\Invoice\Invoicedata"
cd %DIRR%
FOR /F "delims=|" %%I IN ('DIR "*.*" /B /O:D') DO SET Newfile=%%I
get %Newfile%
quit
close()

Open in new window


Replace the Ip , user , pass and try it out
@oBdA that's Excellent heh^^

If i may ask a question ,what does the
'type "%~f0"'

Open in new window

do ?
That types the script itself to find the start of the embedded ftp script.
Avatar of isames

ASKER

@oBdA Do I need the REM part?
Are you asking about the "REM Only functions after this line!"? Yes, you'll need that part and everything below it as well; the actual ftp script used is at the end. Save all of it as Whatever.cmd, and change the variables at the beginning to your linkings.
If you can leverage a utility, then here is a simple approach that should work well.  Download the NCFTPPUT executable from the link below and place it in a location the BAT file can access (either in a folder in the PATH, or in the folder where the BAT runs out of, etc)..

http://www.ncftp.com/download/

@echo off
setlocal

rem Define folder to search and FTP information
set BaseDir=C:\Invoice\Invoicedata
set FtpHost=ftp.xxxx.com
set FtpUser=myusername
set FtpPass=mypassword

rem Locate newest file in folder
set NewestFile=
for /f "delims=" %%A in ('dir /a:-d /o:-d /b "%BaseDir%"') do (
  if not defined NewestFile (
    set NewestFile=%%~A
  )
)

rem If we found a file, FTP it to the desired host
if defined NewestFile (
  ncftpput -u %Ftpuser% -p %Ftppass% -a -V -F %FtpHost% . "%BaseDir%\%NewestFile%"
)

Open in new window

~bp
Avatar of isames

ASKER

@obda

Thanks for your help thus far. I don't know the first thing about Batch code, so I need a lot of "hand Holding". I saved this as a .bat file, ran it as admin, but I don't think nothing happened.

This is what I put in so far:

@echo off
setlocal enabledelayedexpansion
[b]set SourceFolder=  I changed this to the directory of the  file I want uploaded to Billtrust.
set FileMask=*.txt -- I changed this to *.csv because this is the file type that I want to upload to Billtrust.
set FtpSite=ftp.billtrust.com
set FtpUser=I put in username here
set FtpPass=I put in password here
[/b]
set FtpFile=%~dpn0.ftp
set LogFile=%~dpn0.log

for /f "delims=" %%a in ('dir /a:-d /o:d /b "%SourceFolder%\%FileMask%"') do set UploadFile=%SourceFolder%\%%a
call :ExportDataSection FTP "%FtpFile%"

echo Content of the ftp file:
type "%FtpFile%"

echo.
echo [%Date% %Time%] Starting ftp upload
ECHO ftp.exe -i -v -s:"%FtpFile%" >"%LogFile%"
echo [%Date% %Time%] ftp upload done.

REM ================================================================================
REM Only functions after this line!
REM ================================================================================
goto :eof
:ExportDataSection
REM *** Reads all lines listed in %1 (section) and writes them to %2 (file name).
REM *** Environment variables will be expanded.
set Section=%~1
set FileName=%~2
if exist "%FileName%" del "%FileName%"
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| C:\Windows\system32\find.exe /n "[%Section%]"') do set DataStart=%%a
for /f "skip=%DataStart% delims=" %%a in ('type "%~f0"') do (
	call :Expand Line "%%a"
	if "!Line:~0,1!"=="[" (goto :eof) else (>>"%FileName%" echo !Line!)
)
goto :eof

:Expand
set %1=%~2
goto :eof

REM ================================================================================
REM Only data sections after this line!
REM ================================================================================
[FTP]
open %FtpSite%
%FtpUser%
%FtpPass%
put %UploadFile%

Open in new window

That's the test mode I mentioned above. This is there so you can verify that the ftp script that will be generated by the script is correct.
Remove the uppercase ECHO in line 20 (in front of ftp.exe) to run it for real.