Batch script to create a date time folder - date

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.

Related

How can I compare two dates in batch?

I have a batch script to get two dates; one of a folder, and the current system date.
I want to specifically compare the two by seeing if the date of the folder is 10 minutes or less older than the current date. This essentially checks if the user has modified this folder at the most 10 minutes ago.
Here's my current code (not complete, but the base):
#echo off
for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%a/%%b/%%c)
for /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a:%%b)
setlocal enabledelayedexpansion
if exist "C:\$Recycle.Bin" (
pushd "C:\$Recycle.Bin"
for /F "delims=" %%a in ('dir /S /b S-1-*-1001 /AD') do (set "{recycle-bin-date}=%%a")
for %%a in ("!{recycle-bin-date}!") do (
Set "data=%%~ta"
)
popd
)
set date1=%mydate% %mytime%
set date2=!data!
echo Date 1 (Current): %date1%
echo Date 2 (Recycle): %date2%
pause
::We have the dates above, how do I achieve what I'm trying to do?
If anyone could help me here, I'd really appreciate it.
You CAN do date time math in pure batch but it is quite cumbersome.
(See Ritchie Lawrence batch function library)
I recommend to use PowerShell as a tool for this
PowerShell one liner:
[int]([datetime]::Now - (gci 'c:\$Recycle.BIN\S-1-*1001' -Force).LastWriteTime).TotalMinutes
Wrapped in a batch
#Echo off
For /f "usebackq" %%A in (`
Powershell -NoP -C "[int]([datetime]::Now - (gci 'c:\$Recycle.BIN\S-1-*1001' -Force).LastWriteTime).TotalMinutes"
`) Do Set "AgeMinutes=%%A"
Echo Age in minutes %AgeMinutes%

Batch file for listing all files from a date interval

I need to do a batch file which order all files by a date interval, example:
#echo off
echo Input the date(dd/mm/yyyy):
set /p compDate=
::After that I will compare from the actual day (%date%), example:
set interval = %compDate% - %date%... *Something like that*
::After that I need to list all files from a specific directory, example:
echo Input the directory:
set /p directory=
SET Exit= %UserProfile%\Desktop\test.txt
::After that I might need dir /tc to get the creation date, example:
pushd "%directory%"
dir /s /tc /a-d > %Exit%
::After that I don't know how to get only the lines which are in date interval, example:
Today is 19/08/2014, but I want to search all files created from day 10/07/2014.
So I have to copy all lines which have the date 10/07/2014, 11/07/2014, 12/07/2014 and so on until stop on today created files.
I tried with findstr, but I can't set the date interval, just a specific date to search in the .txt created.
Somebody know how to do that?
If I correctly understood the request, you really don't want files created in a given interval, but files created after a given date. The Batch file below assume that the date used by the system appear in DD/MM/YYYY order:
EDIT: Some modifications as reply to the comments
#echo off
setlocal EnableDelayedExpansion
echo Input the date(dd/mm/yyyy):
set /p compDate=
for /F "tokens=1-3 delims=/" %%a in ("%compDate%") do set compDate=%%c%%b%%a
echo Input the directory:
set /p directory=
SET Exit=%UserProfile%\Desktop\test.txt
pushd "%directory%"
(for /F "tokens=1-5*" %%a in ('dir /s /od /tc /a-d') do (
set "fileDate=%%a"
if "!fileDate:~2,1!!fileDate:~5,1!" equ "//" (
for /F "tokens=1-3 delims=/" %%x in ("!fileDate!") do set fileDate=%%z%%y%%x
if !fileDate! geq %compDate% (
set "fileSize= %%e"
echo %%a %%b %%c %%d !fileSize:~-16! %%f
)
)
)) > %Exit%
popd
A solution that uses WMIC and is independent from time/date settings:
#echo off
setlocal
set /p compDate=Input the date(yyyymmdd):
set /p directory=Full directory path (with no slash at the end):
set exit_file= %UserProfile%\Desktop\test.txt
break>%exit_file%
for /f "tokens=1,2 delims=:" %%a in ("%directory%") do (
set "dir_drive=%%~a:"
set "dir_path=%%~b\"
)
set dir_path=%dir_path:\=\\%
setlocal enableDelayedExpansion
for /f "useback skip=1 tokens=1,2* delims=:" %%f in (`" wmic datafile where (drive='!dir_drive!' and path like '%dir_path%') get CreationDate^,name"`) do (
set creation_date=%%f
set creation_date=!creation_date:~0,8!
set "file_name=%dir_drive%%%~g"
if 1!creation_date! GTR 1%compDate% (
echo !file_name!>>%exit_file%
)
)
exit /b 0

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

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

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%