4est
4est

Reputation: 3168

Rename files with current date stamp (bat files)

I have bat file to run sql procedure which is creating 2 files:

TEST_SCHEMA.dmp

TEST_SCHEMA.log

Then I used xcopy to copy files. After copy I want to change the file name to:

TEST_SCHEMA.dmp -> TEST_CURRENT_DATE.dmp

TEST_SCHEMA.log -> TEST_CURRENT_DATE.log

where Current_Date = YYYYMMDDHHMM

This is what I had so far:

set OWNER=myOwner
set FILE_NAME=TEST_SCHEMA
set NLS_LANG=AMERICAN_AMERICA.WE8ISO8859P1

sqlplus %OWNER%/%OWNER%@host:1521/SID @run_test.sql %OWNER% %FILE_NAME%
xcopy "\\host\c$\abc\def\test\%FILE_NAME%.*" /C

set "_year=%MyDate:~0,4%"
set "_month=%MyDate:~4,2%"
set "_day=%MyDate:~6,2%"

ren FILE_NAME.dmp "TEST (%_year%%_month%%_day%).txt"
ren FILE_NAME.log "TEST (%_year%%_month%%_day%).txt"

pause 0

Result should be like:

TEST_201806131006.dmp

TEST_201806131006.log

But I don't know how to correctly use rename and put date to file name.

Upvotes: 0

Views: 8526

Answers (3)

M61Vulcan
M61Vulcan

Reputation: 337

The above answers seem a little complicated to me. I have some files that I am constantly updating as I develop a program. In case I break something and don't realize immediatly I needed a way to backup several times a day. This is what I put in my batch program (modified for your file name) which I then control via Windows Task Scheduler. (I actually xcopy the folder I'm working on and then rename it with the date/time adjustment.)

ren TEST_SCHEMA.dmp "TEST_%date:~6,4%%date:~3,2%%date:~0,2%_%time:~0,2%%time:~3,2%.dmp"

My thanks to Mofi for providing his answer to another user here...

What does %date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% mean?

Upvotes: 0

user6811411
user6811411

Reputation:

Create the file including the date stamp.

set OWNER=myOwner
for /f %%A in ('
  powershell -nop -c "get-date -f yyyyMMdd"
') do SET "FILE_NAME=TEST_SCHEMA_%%A"
set NLS_LANG=AMERICAN_AMERICA.WE8ISO8859P1

sqlplus %OWNER%/%OWNER%@host:1521/SID @run_test.sql %OWNER% %FILE_NAME%
xcopy "\\host\c$\abc\def\test\%FILE_NAME%.*" /C

Upvotes: 1

Compo
Compo

Reputation: 38579

Using WMIC to create the DateTime stamp:

For /F %%A In ('WMIC OS Get LocalDateTime') Do If Not "%%~xA"=="" Set "ds=%%~nA"
Ren "TEST_*.*" "TEST_%ds:~,-2%.*"

Upvotes: 2

Related Questions