Buddy Sievers
Buddy Sievers

Reputation: 57

Select a directory by checking its name for date and time in Windows Batch

i have a batch file scripted which makes a backup of some of my mysql database tables and saves them in a new created folder with this pattern as name: "date_time" so as example "23.06.2016_1050". So i have another batch file which imports every *.sql file in a specific folder back to my database. What i want to do is, writing a batch which automaticly selects the latest folder and then runs my part of code in that.

Here is my batch code:

cd c:\server\backup\character_data
FOR %%X IN (*.sql) DO ECHO Importing %%X & "C:\Program Files\MariaDB 10.1\bin\mysql" dspdb -h localhost -u root -p 123456789 < %%X

So i need to cd a %variable% which contains the name of the folder with the latest date and time pattern as its name.

So this:

cd c:\server\backup\character_data\%variable%

would be:

cd c:\server\backup\character_data\23.06.2016_1050

Does anyone knows how to get the desired folder name as variable somehow?

I really hope that i explained it good enough and someone can help me with that. Thx in advance! :)

Upvotes: 3

Views: 110

Answers (1)

Drew
Drew

Reputation: 4030

This will get the most recent folder in c:\server\backup\character_data with the full file path, so no need to do cd c:\server\backup\character_data\%variable%, instead it would be cd %a%

If you don't want the full directory of the folder then remove the /s from the dir command

@echo off
cd /d "c:\server\backup\character_data"
FOR /F "delims=" %%i IN ('dir /b /s /ad-h /t:c /od') DO SET a=%%i
::Do what you want with your folder, call it with %a%

Upvotes: 2

Related Questions