0

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.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
4est
  • 3,010
  • 6
  • 41
  • 63

3 Answers3

2

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%.*"
Compo
  • 36,585
  • 5
  • 27
  • 39
1

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
  • I got message as: sqlplus is not recognized as an internal or extarnal command, operable program or batch file – 4est Jun 13 '18 at 09:45
  • 1
    My inserted command just sets an environment variable, it doesn't change dir or path. Looks like you are doing something differently than in your batch. –  Jun 13 '18 at 09:51
0

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?

M61Vulcan
  • 337
  • 3
  • 7