How to create . BAT to rename multiple files into multiple folders by inserting date and time of creation?

Asked

Viewed 3,400 times

5

Someone knows how to create .bat to rename multiple files into multiple folders by inserting date and time of creation, not the current date?

Example:

Nome                        Data e hora de Criação

SCP Dados.mdb               03/06/2016 10:26

After renaming it is to stay like this:

Nome                        Data e hora de Criação

SCP Dados030616-1026.mdb    03/06/2016 10:26

Deleting any character before and after the created name leaving the extension .mdb "<-SCP Dados030616-1026->.mdb"

I already did that:

@echo   Renomeando arquivo SCP Dados.mdb da BAHIA (BKP Manha)

dir \\S1WKSPCRCI\B$\CIBackups\Backup_BA\"SCP Dados.mdb" [AQUI ME MOSTRA A DATA]

set /p dt_arq_ba= Digite a DATA de Criacao do Arquivo da BAHIA: [AQUI INSIRO A DATA]

ren \\S1WKSPCRCI\B$\CIBackups\Backup_BA\SCP?Dados.mdb "SCP Dados%dt_arq_ba%.mdb" [AQUI ME DA O ARQUIVO RENOMEADO]

It’s working, but only manually renaming one by one, wanted to automate with the file creation date.

  • 1

    Edith the question and put what you have already done.

  • I’m waiting for suggestions, thanks for your attention!

  • It needs to be in . bat ?

  • See if this helps http://stackoverflow.com/questions/30079757/rename-file-with-creation-date-time-in-windows-batch

  • You will need to loop the root directory using the "for" command combined with the "/D" command. Read more in this article:https://ss64.com/nt/for_d.html

1 answer

1

:: 1) List the files and redirect the output to "%temp%\_file_origem.txt"

:: 2) Use a loop for to get the paths and port to the command WMIC "\"="\\"

:: 3) Use another loop for to get the creation date and save to a variable to rename.

:: 4) Adjust the creation date output layout to the desired format:

:: 5) Set the new file name and rename by removing the undesirable characters:


  • Obs.: On the line: echo/ ren "!_file_!" "SCP Dados!_date_create!.mdb" && timeout /t 3 2>nul >nul

    Added only to debug, remove after verify functioning.


@echo off && cd /d "%~dp0" & setlocal enableextensions enabledelayedexpansion
echo/  Renomeando arquivo SCP Dados.mdb da BAHIA ^(BKP Manha^)

rem  :: 1 ::
for %%i in (target origem) do echo/>"%temp%\_file_%%i.txt"
dir /s /b /a-d "\\S1WKSPCRCI\B$\CIBackups\Backup_BA\*.mdb" >>"%temp%\_file_origen.txt">>"%temp%\_file_origem.txt"

rem :: 2 ::
for /f "tokens=* delims= " %%i in ('type "%temp%\_file_origem.txt"') do ( 
     for /f "tokens=* delims= " %%I in ('dir /w /b "%%~i"') do set "_file_=%%~fI"&& call :_rename
     )  
goto :eof 

rem :: 3 ::
:_rename
for /f "tokens=1 delims=." %%r in ('wmic datafile where name^='!_file_:\^=\\!' get "creationdate" ^| findstr /v "CreationDate"') do ( 

rem :: 4 ::                            
     set "_date_=%%r"
     set _date_create=!_date_:~6,2!!_date_:~4,2!!_date_:~2,2!-!_date_:~8,2!!_date_:~10,2!

rem :: 5 ::  
     echo/ ren "!_file_!" "SCP Dados!_date_create!.mdb"  && timeout /t 3 2>nul >nul 
     for %%d in (file date_create) do set _%%d=<nul
     exit /b
    )
  • 4

    It was blocked by over-posting (already removed) using the response field improperly. When this occurs frequently, it is a sign that the question tends to "attract" other casual users who do not understand the operation of the site. Taking advantage, for doubts like this, we have the Site Meta which is "the site for questions about the site"

Browser other questions tagged

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