How can I introduce a data based file name request to an FTP bat file process? - date

I've been tasked with updating an existing windows scheduled task. The task simply calls a windows console based FTP command using an existing script text file using the -s:ftpScript.txt syntax.
The problem is that the filename has changed and will now be based on the current date such as filename20110101.txt.
How can I get the -s:ftpScript.txt to understand that there is a dynamic filename now required? Do I have to recreated the "ftpScript.txt" file dynamically each time the task fires to then include a new static file containing the current date based filename?

I've now since followed my initial suggestion and it works perfectly. I've posted the following below in case it helps anyone else;
echo Generating New FTP Request
set request=Request.txt
FOR /F "TOKENS=1,2 DELIMS=/ " %%A IN ('DATE /T') DO SET month=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('DATE /T') DO SET day=%%B
FOR /F "TOKENS=3,4 DELIMS=/ " %%A IN ('DATE /T') DO SET year=%%B
set /A day=%day%-1
set yesterday=%year%%month%%day%
set file=<filename>
(
echo open <server>
echo <pass>
echo get %file%%yesterday% %file%%yesterday%
) > %request%
ftp -i -s:%request%

Related

Batch script to create a date time folder

I used the following to create a folder with a date and timestamp
#echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a-%%b)
set mydir="%mydate%-%mytime%"
mkdir %mydir%
Instead of created a folder named 2020-5-19-11-30 AM, I get one folder named 2020-5-19 and another folder called AM.
Any idea? I just want to run a batch file that will create a date and time including am or pm for a folder.

Batch file copy with date and time

I'm trying to figure this out need some help. I need to make a program that copy's my Log file and rename with date and time to different directory. Prior to loading the program not sure how to do it if any help would be great.
Here's a batch file that will copy all log files to an archive folder appending the date to each:
#Echo Off
#For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do #(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%%C%%B%%A
)
#For %%a in ("*.log") do copy %%a "archive\%%~na_%All%.LOG"

Command Prompt/Bat file - Create new folder named with today's date

I use the following code to create a new folder that is named with todays date:
for /f "tokens=1* delims=" %%a in ('date /T') do set datestr=%%a
mkdir c:\%date:/=%
Now the format is as follows:
20130619
How do I change the format to?:
2013_06_19
Thank you
%date% depends on your computer settings, and locale. Here is a reliable way to get a date and time stamp. Win XP pro and above.
If you need to use your batch file on unknown machines then this is worth using.
:: time and date stamp YYYYMMDD, HHMMSS and YYYY-MM-DD_HH-MM-SS
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
echo stamp: "%stamp%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
pause
for /f "tokens=1-3 delims=/" %%a in ("%date%") do md "%%a_%%b_%%c"
Do this:
for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set yyyymmdd1="%%k"_"%%i"_"%%j"
mkdir %yyyymmdd1%
or simply
SET Today=%Date:~10,4%_%Date:~7,2%_%Date:~4,2%
echo %today%
outputs
2013_06_19
Press any key to continue . . .
Then you could easily use the variable today for directory creation e.g.:
mkdir %today%
EDIT: YYYY_MM_DD format

Batch file to rename all files in a folder by adding current date/time to beginning of filename

I can't seem to understand how batch files add yyyy/mo/dd/hh/mm/ss to the beginning of filenames. (Using Windows 7) Accuracy to the second is important.
It doesn't actually have to be a batch file, it just has to be a small program which can be executed by Directory Monitor whenever I add files to a folder: http://brutaldev.com/page/Directory-Monitor.aspx
I only imagine that a batch file would be the simplest and most efficient approach, but any other suggestions are welcome.
I work with many sequentially numbered files with overlapping filenames and I need a quick way to rename them whenever I add them to a folder such that there will never be any file with the same name yet they will still remain in sequential order. This is how I thought of adding the current date and time to the beginning of the filename and why seconds are important, since I can easily add multiple sets to a folder in under a minute but certainly not under a second. It would be ideal if the batch file could ignore file extensions and simply add the current date/time to the beginning of any file added to the folder.
The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
:: this line will rename the files in the current folder which haven't already
:: been renamed by checking for the fullstamp format at the start of the line
:: but it will skip this batch file
for /f "delims=" %%a in ('dir /b /a-d ^|findstr /v "^[0-9]*-[0-9]*-[0-9]*_[0-9]*-[0-9]*-[0-9]*" ') do if /i not "%%a"=="%~nx0" ren "%%a" "%fullstamp% - %%a"
pause
#ECHO off
SETLOCAL
IF [%1] NEQ [] goto s_start
:: Author - Simon Sheppard, July 2003
:: Tested for Windows NT, 2K, XP
ECHO STAMPME.cmd
ECHO REName a file with the DATE/Time
ECHO.
ECHO SYNTAX
ECHO STAMPME TestFile.txt
ECHO.
ECHO STAMPME "Test File.txt"
ECHO.
ECHO STAMPME "c:\docs\Test File.txt"
ECHO.
ECHO In a batch file use CALL STAMPME ...
:: To change the filename format just change around the last line below
GOTO :eof
:s_start
SET _file=%~n1%
SET _pathname=%~f1%
SET _ext=%~x1%
::Get the date
:: note ISO 8601 date format would require 4 digit YYYY Year)
FOR /f "tokens=6-8 delims=/ " %%G IN ('NET TIME \\%computername%') DO (
SET _mm=%%G
SET _dd=%%H
SET _yy=%%I
)
:: Get the time
FOR /f "tokens=2-4 delims=:." %%G IN ('cmd /c "time<nul"') DO (
SET _hr=%%G
SET _min=%%H
SET _sec=%%I
GOTO :done
)
:done
ECHO Today is Year: [%_yy%] Month: [%_mm%] Day: [%_dd%]
ECHO The time is: [%_hr%]:[%_min%]:[%_sec%]
REN "%_pathname%" "%_hr%-%_min%-%_sec%#%_file%%_ext%"
This seems to work for me
I'd prefer solutions, that are not dependent to local settings (wmic gives always the same format):
#echo off
setlocal EnableDelayedExpansion
for %%a in (a.*) do (
for /f %%i in ( 'wmic os get localdatetime /value ^|find "Local"' ) do set %%i
set ldt=!LocalDateTime:~0,4!-!LocalDateTime:~4,2!-!LocalDateTime:~6,2!-!LocalDateTime:~8,2!-!LocalDateTime:~10,2!-!LocalDateTime:~12,2!-!LocalDateTime:~15,3!
echo seconds ### ren %%a !LocalDateTime:~0,14!%%a
echo milliseconds ### ren %%a !LocalDateTime:~0,18!%%a
echo with separator ### ren %%a !ldt!-%%a
)

Command Line: Create File Named With Date/Time

I have seen questions of these sorts listed on the site, and I've tried to work through them but haven't been successful.
What I want to do is make .bat file that creates a file (probably just a txt file, but doesn't matter) which has the Date and Time in the name.
I can use something like "TYPE File1.txt>"File2 - %DATE% - %TIME%".txt" to make the file (and without the date/time it works fine, and in fact if I change the regional settings to use - or . instead of /and: it works fine, but having the time on the PC with : is really annoying).
So I've tried the following which I got from other questions / the internet, and a few other things, but they don't seem to change the format for the date/time
M:\>FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
%%A was unexpected at this time.
M:\>FOR /F "TOKENS=1* DELIMS= " %A IN ('DATE/T') DO SET CDATE=%%B
M:\>SET CDATE=%20/03/2012
M:\>FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %A IN ('DATE/T') DO SET mm=%%B
M:\>SET mm=%20
M:\>FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %A IN ('echo %CDATE%') DO SET dd=%%B
M:\>SET dd=%03
M:\>FOR /F "TOKENS=2,3 DELIMS=/ " %A IN ('echo %CDATE%') DO SET yyyy=%%B
M:\>SET yyyy=%2012
M:\>SET date=%mm%%dd%%yyyy%
M:\>date/t
Tue 20/03/2012
M:\>
As you can see, it's not working for me, even when I just call on the date I can see it's wrong...
How can I get this working?
(To be clear, the end product I want is a shortcut that if clicked at 8:13am on March 20 would makes a file "20120320 0813.txt". Or something very close)
(FYI I'm on a corporate PC running XP)
Thanks in advance for any help!