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
Related
So I got my answer to the
How to create a folder with current date in its name and copy to it all files and folders from some path on Windows 10
question, but as it was a duplicated issue and I am a newbie on this forum- it was closed and I am unable to ask additional question in similar topics
By using this How to create a subfolder based on current date? solution I was able to achieve what I wanted [thank you very much user compo]
And so this code is working A-OK:
#Echo Off
Set "sd=C:\Users\YOUR USER NAME\AppData\Roaming\Mozilla\Firefox\Profiles\fsj89244"
Set "dd=D:\Backup Copies\Firefox\Profile"
Set "ds="
If Not Exist "%sd%\" Exit /B
For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null'
) Do If Not Defined ds Set "ds=%%A %%B %%C"
If Not Defined ds Exit /B
RoboCopy "%sd%" "%dd%\%ds%" /E
It creates a sub-folder with a date [on drive D] in its name and then copies to it all content from a specified path [from the drive C]. This speeds up my manual creation of backup copies
But if the script is executed again I would need it to check first if such sub-folder exists. And if yes then leave it alone and create a folder with name in date format >>YYYY MM DD v2<<; and the next time >>YYYY MM DD v3<< etc. which would stop happening when the date in the operating system would change to a next day
Set "ds="
For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null'
) Do If Not Defined ds Set "ds=%%A %%B %%C"
If Not Defined ds Exit /B
set ver=0
for /f "tokens=3,4 delims=v " %%a in ('dir /ad /on /b "%ds%*"') do set /a ver=%%b+1
set "newFolder=%ds% v%ver%"
md "%newFolder: v0=%"
dir /ad /on will sort the folders by name and the for ends with the last version plus one (The obvious for /d %%a in ("%ds%*") do set /a ver+=1 just counts the found folders and so will be incorrect if a folder is missing for any reason and probably will use an already "occupied" folder)
The first folder of a day will be in the format YYYY MM DD, all further folders will be in the format YYYY MM DD vX. If you want the first folder to be the same format (... v0), use just md "%newFolder%.
I have been banging my head against the wall trying to figure this out. First, to answer what I'm sure will be asked, yes, I need to use a batch script. I'm sure that this is much easier in PowerShell or another language, but it needs to be a Windows Batch Script (CMD).
I am trying to re-design this script to be used on new systems and older systems within our company. The newer systems use Windows Server 2012, while the older systems use Windows Server 2008. Both systems will have 2 hard drives, but the difference between the 2 is the use of the second hard drive. On the older systems, the 2nd drive is used as a backup drive. On the newer system it is not.
In layman's terms, here is what I am looking to do:
IF ANY DISK VOLUME includes "Backup" in it's name (Caption)
SET %buletter% to the Drive Letter
SET other variables
ELSE
SET the backup location to something else
SET other variables
I've been able to pretty easily able to find how to find the name of the disk volume with the following:
FOR /f %%b IN ('wmic volume get DriveLetter^, Label ^| find "%lookfor%"') DO Stuff
But I haven't figured out how to wrap the FOR statement in an IF statement.
Any help is appreciated.
Thanks
Try this:
for /f "usebackq skip=1" %%p in (`wmic volume where "label like '%%Backup%%'" get Driveletter`) do echo Backup Drive is %%p
The only issue - it will give an empty string on a last iteration.
You don't have to use an IF initially,
you can use find and use conditional execution on success && or fail || or
initializing a var and using a for /f to eval the output - and afterwards check if the var is defined.
I'd exclude the first disk by checking for BootVolume in an extended where clause
:: Q:\Test\2019\01\25\SO_54366874.cmd
#Echo off
Set "BackUpDrive="
for /f "usebackq tokens=1,2*" %%A in (`
wmic volume where 'DriveType^="3" AND BootVolume^="FALSE" AND Label like "%%Backup%%"' get DriveLetter^,Label 2^>NUL ^| find ":"
`) Do Set "BackUpDrive=%%A"
If defined BackUpDrive (
Echo Drive with label Backup found: %BackUpDrive%
Rem other commands
) Else (
Echo no drive with label Backup found
Rem other commands
)
Sample output here:
> Q:\Test\2019\01\25\SO_54366874.cmd
Drive with label Backup found: D:
Using the idea from #mirGantrophy of checking the OS Version, this is what I came up with.
SET Version=
FOR /F "skip=1" %%v IN ('wmic os get version') DO IF NOT DEFINED Version set Version=%%v
FOR /F "delims=. tokens=1-3" %%a in ("%Version%") DO (
SET Version.Major=%%a
SET Version.Minor=%%b
SET Version.Build=%%c
)
IF %Version.Major% LSS 10 GOTO Win2008
IF %Version.Major%==10 GOTO Win2012
:Win2008
FOR /F %%p IN ('wmic logicaldisk where "VolumeName='%lookfor%'" get Caption ^| find ":"') DO (
SET buletter=%%p
SET bootdrvltr=C:
)
GOTO SetLocalDrv
:Win2012
SET buloc=C:\Archive
SET bootdrvltr=D:
GOTO SetLocalDrv
:SetLocalDrv
SET localdrv=%bootdrvltr%\Bootdrv
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.
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="
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%