Create a . bat <source>"filename_2021_01_10_code" <target> another server

Asked

Viewed 35 times

1

I created a that was functional and there was the need to modify to select and copy files by the month of each current year for a Storage.

File name: AMARGOSA_GCTRANS_backup_2021_01_10_003001_3995647.bak

@echo off

cd \

echo -------------------------------------

echo      Iniciando o backup

echo -------------------------------------

echo

echo -------------------------------------

echo   Pasta Backup_SQL_Mensal

echo -------------------------------------

xcopy /y /d F:\Backup\gyc\Amargosa\Backup Banco de Dados\AMARGOSA_teste\AMARGOSA_teste_backup_2021_01_10_003001_3995647.bak \\192.168.0.120\Backup_SQL_Mensal\Amargosa\Backup Banco de Dados\AMARGOSA_teste

echo .....

echo -------------------------------------

echo      Backup concluído

echo -------------------------------------

echo

pause
  • Hello Adriano, I did not understand your problem, could explain better?

1 answer

0

1. For each execution, take the date:

> wmic OS Get localdatetime

20210128183243.207000-180

2. Save to a variable using a for /f loop

> for /f %i in ('wmic OS Get localdatetime ^|findstr /r [0-9]')do set "_data=%i"

set "_data=20210128183243.207000-180"

3. Use substrings to compose the date/strings layout of your file names:

> set "_data=20210128183243.207000-180"
obter string da posição 0 até a posição 4   ==> 2021 0128183243.207000-180
                              %_data:~4,2%  ==> 2021 obter o ano corrente 


> set "_data=20210128183243.207000-180"
obter string partindo da posição 4 use as 2 posições seguintes   ==> 2021 01 28183243.207000-180
                                                   %_data:~4,2%  ==>      01 obter o mês vigente

4. Concatene the character "_" with the substrings of the variable %date% for year and also for the month current, thus composing a selection for the names of your files .bak of origin.

AMARGOSA_teste_backup_2021_01_10_003001_3995647.bak
                  *_%_data:~0,4%_%_data:~4,2%_*.bak

5. Select "...by the date / month of each current year...", knowing that there will not be (supposedly) month files 10 in current month 06, just use the current year, so your month (in sub-strings) can be replaced by _*_:

.\*_%_data:~0,4%_*_*.bak

6. Gather it all in your bat:

@echo off

echo -------------------------------------

echo Iniciando o backup

echo -------------------------------------

echo

echo -------------------------------------

echo . Pasta Backup_SQL_Mensal

echo -------------------------------------

cd /d "F:\Backup\gyc\Amargosa\Backup Banco de Dados\AMARGOSA_teste\"

xcopy /y /d ".\*_%_data:~0,4%_%_data:~4,2%_*.bak" "\\192.168.0.120\Backup_SQL_Mensal\Amargosa\Backup Banco de Dados\AMARGOSA_teste\"

echo .....

echo -------------------------------------

echo Backup concluído

echo -------------------------------------

echo

pause

Browser other questions tagged

You are not signed in. Login or sign up in order to post.