I am struggling to grasp the concept of writing batch files. I am hoping to schedule a .bat file to download a file with a timestamp on a daily basis. Setting up the scheduled task etc. is straight forward, my problem arises when I try execute the code to take the fixed part of the filename and append a datestamp to identify the appropiate file.
From the research I have done I still cannot get my code to pick up yesterday date in a YYYYMMDD format. eg. today is the 15th of August, I would like my batch to identify the file with 20130814. My exisitng (static) ftp batch code is
option confirm off
option batch abort
open Ftp_name
lcd "U:\XXXX\YYYY\ZZZZ"
get "LoanSummary__Daily.xlsx"
Whereas I would like the batch to consider..
get "LoanSummary__Daily_" & YYYYMMDD & ".xlsx"
Thanks.
I don't think you can dynamically build file names within an FTP script. But you can dynamically build the ftp script with a batch file prior to invoking it.
The simplest way to do this is to create an ftp script template that has the variable portion(s) represented as an environment variable with delayed expansion. For example !yesterday! could refer to an environment variable that gets expanded into yesterday's date. A simple FOR /F loop reads the template, and writes each line to a new ftp script file. The variables are automatically expanded in the process as long as delayed expansion is enabled.
Getting yesterday's date in a Windows batch file is a non-trivial excercise. There have been many methods posted on SO as well as other sites. Most methods have various limitations, the most common of which is susceptability to problems due to differences in locale date formatting. I use a hybrid batch/JScript utility called getTimestamp.bat that will work on any Windows platform from XP onward, regardless of the locale. It is pure script, so no .exe download is needed. getTimestamp.bat is avaiable here.
Assuming getTimestamp.bat is in you current directory, or better yet, somewhere within your PATH, then the following should work.
getYesterday.ftp.template
option confirm off
option batch abort
open Ftp_name
lcd "U:\XXXX\YYYY\ZZZZ"
get "LoanSummary__Daily_!yesterday!.xlsx"
getYesterday.bat
#echo off
setlocal enableDelayedExpansion
:: Get yesterday's date in YYYYMMDD format
call getTimestamp -od -1 -f {yyyy}{mm}{dd} -r yesterday
:: Create a temporary ftp script that uses the "yesterday" date
>temp.ftp (for /f "delims=" %%L in (getYesterday.ftp.template) do echo %%L)
:: Now simply invoke your ftp client using the temp script, something like
ftp -s:temp.ftp ...
:: Delete the temporary ftp script
del temp.ftp
Related
hope i can find an answer for what i'am trying.
i've a file named JOFAACKR.WSDFHER1.08092012.GILLJOCAR.J8RJXV2Y_SumRpt.JOBAACKR on a remote server, i need to get this file onto my server. i wrote a download program that does get me the file perfectly when i mention exact filename. Now i'am trying to pass a date value through a variable.
set mmddyyyy=%date:~4,2%%date:~7,2%%date:~10,4%
so, can i write something like *.%mmddyyyy%.*.JOBAACKR to get the files named with current date?
C:\> set mydate=%date:~4,2%%date:~7,2%%date:~10,4%
C:\> echo %mydate%
03082013
Currently, I have a batch file that uses wget to read a file from the server. Is there any way for wget to save the contents of that file to a variable and then for the batch file to take a certain action based on the value of the variable?
The peseduo-code would probably look something like this. I am very new to batch files and am still learning the semantics:
SAVE RESULT OF wget http://www.theserver.com/instruction TO VARIABLE: the_variable
IF %the_variable% == 'restart' <DO SOME ACTION HERE>
I will base this answer on the assumption that your downloaded file contains text strings.
If this is the case then it is possibile to use the FOR command in this way:
for /F %I IN (instruction.txt) DO if %I==restart #echo RESTART FOUND
This command opens the file "instruction.txt" and parse it assigning each word to the variable %I
Then for each value of variable %I executes the command specified after the keyword DO.
In this case I have compared the variabile %I to the string "restart" and if the result is true the batch execs the command #echo RESTART FOUND
You could use the GOTO: function in your batch file. So that if the variable is equal to a certain value/string it jumps to a particular section in the batch file and carries out the code in that section.
Almost like using methids in Object Orientated Programming.
CHeck this link out :-
http://www.robvanderwoude.com/goto.php
I have a batch script setup to automatically retrieve a file from a remote FTP server. Part of the requirement is the file will be named with a new datestamp each day, such as "File_90611.csv." I have a command line tool that generates the filename; which is then supposed to be set to a variable using the line below:
for /f "delims=" %a in ('C:\BIN\YesterdayDateStamp.exe') do #set DATESTAMP=%a
The problem is this. This line works fine when run from the command line directly. However, when I put this exact same line in a batch script and run it; I get this error:
\BIN\YesterdayDateStamp.exe') was unexpected at this time.
I REM'ed everything out in the script except the FOR ... IN commands to make sure there wasn't some sort of conflict; but even with this I still get an error.
Been Googling for an answer but have no leads. Any ideas? Any constructive input is greatly appreciated.
Thanks,
Frank
When for is used in a batch file, you need to double the percent signs in front of the variable name.
From for /?:
To use the FOR command in a batch program, specify %%variable instead
of %variable. Variable names are case sensitive, so %i is different
from %I.
Right now i have a batch job I wrote that calls another file and passes in the variables that executable needs to run (password and filename).
Ex:
> cd f:\test\utils
> admin import-xml -Dimport.file=f:\DB\file1.xml -Dadmin.db.password=test123
I wrote a job that does this, but found out that there would be multiple files.
The username and password never change but the filename differs for like 15 different xml files--with maybe more coming soon.
The files will always be located in the same folder. Instead of ending up with like 15-20 jobs (one for each file), can I write something that will process each file located in this directory. And either wait till one is completed before the next or I can add a 3 min sleep before it starts the next file.
pushd C:\test\utils
for %%F in (F:\DB\*.xml) do (
admin import-xml "-Dimport.file=%%~dpnxF" -Dadmin.db.password=test123
)
popd
The %%~dpnxF expands to drive, path, basename and extension of the current file.
If you intend to set and use environment variables (%foo%) in that loop, read help set first before you get into trouble.
You can use the for command. Something like this in a batch file:
for %%f in (*.xml) do call myotherbatch.bat %%f
Assuming that the admin command you are running doesn't return until the job is finished, the above loop would process them sequentially.
If you run the command at the prompt (as opposed to in a batch file), only use a single %.
for file in f:\DB\*
do
admin import-xml -Dimport.file="$file" -Dadmin.db.password=test123
done
Can a batch file change the system date; save file with attributes; change date back to current date?
Goal to save MYFILE.TXT with the date of 01-01-2010
using Batch commands.
I have tried to set date=01-01-2010
and then save the file, but it didn't work.
Is this impossible?
#echo off
rem to Run this Batch file as administrator
date 01-01-2010
echo %date%
pause
echo Hello World > test.txt
date 09-08-2010
echo %date%
pause
goto :eof
Note: If we didn't "Run as Administrator"
It creates an error message of "A required privilege is not held by the client."
Your best bet is to probably grab touch from GNUWin32 and use that to change the timestamps. Doing this by changing the system date is like using a sledgehammer to crack a nut.
Yes, a batch file can do it, but I wouldn't recommend it. To set the date in Windows, you use the DATE command. To set the date to 01/01/2010, you would execute this command at the command prompt:
date 01-01-2010
However, you will need administrator privileges in order for that to work.
To change the time, the command is TIME.
You can look up both commands by using the HELP facility. i.e.
help date
help time
It's also possible to get the current date using a batch file so that you can re-set the date after making your change. That's somewhat more complicated. Here's one way to do it.
All that said, I agree with Jack Kelly: get a touch utility.