How can I output weekday and month in batch (log files)? - batch-file

I'm trying to setup 7zip for automated backups but I'm having trouble with output file names.
I tried using the %date% command but it just made 2 directories within my backup.
C:\Users\Desktop\Sun 11\07\2010.7z
How can I make it just log the day and month?
C:\Users\Desktop\Sun 11-07-2010.7z

Try
7z a %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%.7z *.* for (YYYY-MM-DD)
or
7z a %DATE:~7,2%-%DATE:~4,2%-%DATE:~-4%.7z *.* for (DD-MM-YYYY)
(*.* is the mask for the files to back up)

Are you using a bat-file? Look here http://www.tech-recipes.com/rx/956/windows-batch-file-bat-to-get-current-date-in-mmddyyyy-format/

You can use WMI to get your date details in a specific format. The problem with the output from date (and the %date% environment variable) is that it's very locale-specific.
If you execute:
wmic path win32_localtime get day^,month^,year^ /format:csv
you will see the output you need to process.
The following script will get you the yyyy-mm-dd format that you need (using the day of the week as the primary sort key is not a good idea):
#echo off
for /f "skip=2, tokens=2-4" delims=," %%a in ('wmic path win32_localtime get day^,month^,year^ /format:csv') do (
set /a ymd = 10000 * %%c + 100 * %%b + %%a
)
set ymd=%ymd:~0,4%-%ymd:~4,2%%ymd:~6,2%
echo %ymd%

Related

Writing the week number in the name of a file in a batch File

I want to include the number of the previous week at the end of the name of the file when copying a file to a different folder with a scrip in a batch file.
I'm trying the following>
echo f | xcopy /f /y "\\Mypath\MyFile.xlsb" "\\Mypath\MyFile WK"%WEEK%".xlsb"
But the result I'm getting is
\Mypath\MyFile WK.xlsb
Desire result is \Mypath\MyFile WK12.xlsb
Thank you so much
Maybe not the best answer but for those who need to get the week, this is the way I was able to finally get the week and accomplish what I needed.
WIN32_LOCALTIME GET /FORMAT:LIST') DO #(SET %%a>NUL)
SET /A a=(14-%Month%)/12,y=%Year%-a,IsoWeek=(%Day%+(153*(%Month%+12*a-3)+2)/5+365*y+y/4)/7-(365*%Year%+(%Year%+3)/4)/7+9
echo f | xcopy /f /y "\\Mypath\MyFile.xlsb" "\\Mypath\MyFile WK"%IsoWeek%".xlsb"

Write Windows batch script to duplicate folder with date appended as new file name

I am trying to write a windows batch script that allows me to duplicate a directory [foldername] with a new file name with date appended to it [foldername.date01]. But it should also check if a duplicate has already been made for current date and create a new one like [foldername.date02] and so on..
xcopy /s/e c:\source d:\target
allows me to copy but I dont know how to retrieve date and append it.
This will backup your directory structure to another drive with all copied folders named SourceFolderName_CurrentDate_BackupTime. Let me know, if any errors:
#ECHO OFF
SET "source=C:\Source" & SET "target=D:\Target"
FOR /F %%a IN ('WMIC OS GET LocalDateTime ^| FIND "."') DO SET DTS=%%a
SET DT=%DTS:~0,8%_%DTS:~8,6%
FOR /F "tokens=*" %%G in ('DIR /b /a:d "%source%"') DO (
XCOPY /s/e/y/b/q/r/k/i "%source%\%%G" "%target%\%%G_%DT%" )
EXIT /B
Note, wmic is supported starting from WinXP Pro, and the above wmic command outputs OS locale independent time stamp. You can adjust DT variable format in its SET statement as you wish.

curl - add variable in url batch programming

I have a batch file where I have a variable which prints date in YYYYMMDD - 20160403
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%
echo Local date is %ldt%
then I am downloading a file which has the date a the end of the url
curl "http://www.example.com/cgi-bin/abc.xyz/%ldt%.txt" -o file-name-%ldt%.txt
where %ldt% has the date for eg: 20160403
but now I am not able to print the date. Any suggesstions ?
%date% is an internal windows variable. It is not wise to overwrite it. Use another variable name (for example %myDate%).
By the way: you can restore the original windows variable by (no joke) deleting it: set "date="

Compare current date with file modified date in batch file

I have a requirement to create a batch file and that should work in windows 2003 server.
I have source folder "c:\Source" and destination folder "c:\Destination".
I want to check all the file modified dates in source folder:
If the file modified date is less than current date then that file will move into destination folder
Otherwise, do nothing.
Note: as my server is production server so, I am unable to install Resource kit and robocopy.exe. The only way is write the batch script.
Robocopy and forfiles are not working in Windows2003 server.
Update
Since you have forfiles on your server, this is easy. To check for files older than 1 day old, just use forfiles /D -1. For files over 2 days old, /D -2.
forfiles /D -2 /M *.log /P C:\Source /C "cmd /c move #file c:\Destination"
Enter forfiles /? in a console window for full syntax.
Original answer
Consider revising your requirement. Instead of saying "If file modified date is less than the current date", you should say, "If file modified date does not equal current date". That absolves you from having to do date math, and makes your task profoundly simpler. After all, the last modified date is not going to be a date in the future, right?
After that, it's a simple matter of scraping today's date from %date% and comparing it with the date from each file's %%~tX substitution property in a for loop. Type help for in a console window and see the last two pages for more information about this syntax.
I don't think there will be any locale date formatting issues with this. As long as your system's %date% variable is in the format of dayOfWeek Date and %%~tX is formatted as Date Time etc. then this script should work regardless of whether you handle dates locally as MM/DD/YYYY or YYYY/MM/DD or DD/MM/YYYY or something else. I hope.
#echo off
setlocal enableextensions
set "source=c:\Source"
set "destination=c:\Destination"
:: store today's date in %today%
for /f "tokens=2" %%I in ('echo %date%') do set "today=%%I"
for %%I in ("%source%\*") do (
rem :: scrape MM/DD/YYYY from %%~tI
for /f %%a in ('echo %%~tI') do (
rem :: compare the two dates
if "%%a" neq "%today%" (
echo %%~nxI: %%a does not equal %today%. Moving.
>NUL move /y "%%~fI" "%destination%"
) else (
echo %%~nxI: %%a equals %today%. Skipping.
)
)
)

batch file which uses windows OS date

I every month a get a folder named using the year and month (which am not allowed to change). For instance DB201401 (DB = database, 2014 = the year and 01 =the month). I was wondering if there was a way to make a batch file, which finds the correct folder of the same year and month based on windows OS Date?
or maybe just find the folder by the month, ignoring the "DB2014"?
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "curdt=%%a"
set "Year=%curdt:~0,4%"
set "Month=%curdt:~4,2%"
echo DB%YEAR%%MONTH%
Change the echo to whatever you want to do with the foldername.
This depends on WMI - so it won't work on very very old Windows versions without WMI.
Wouldn't that just be the latest "DB2*" directory date?
for /f "delims=" %%a in ('dir /b /ad DB2*') do set currdir=%%a&goto done
:done
echo %currdir%
but that assumes that the only DB2* directories are of format DBccyymm

Resources