-3

I have a task to rename a file and place another file with same name in the same folder

Like for e.g. a folder C:/test I have multiple files .txt (suppose test.txt is the one needed rename and replace)
I want to rename test.txt to test_bkp%date% and place new file there.
Need help to start the logic.

@ECHO OFF

SET "sourcedir=C:\Users\test\new"
FOR /f "delims=" %%a IN (
 'dir /b /a-d "%C:\Users\Test\new\%filenames%.txt" '
 ) DO (
 SET "filename=%%a"
  ECHO REN "%C:\Users\Test\new\%%a C:\Users\Test\new\%%a_bk_date.*%"
)

GOTO :EOF

Let me explain what I am trying to achieve. I get file with updated data often. I cannot just go ahead and replace the old file with new. I have to take a backup and place the new file in the folder. This is my first try using batch scripting

Stephan
  • 53,940
  • 10
  • 58
  • 91
Drewdak16
  • 9
  • 3
  • use a `for` loop, look at the `for` loop help (command line and/or web) ? something like `FOR %%G IN (*.txt) DO rename %%G test_bk%date%` – NeronLeVelu Jan 23 '19 at 12:43
  • I tried but this should be automated .another person put a file in different location i move it to the current location and have to rename the existing file and place the new file (its like updating the file with new /additional data) – Drewdak16 Jan 23 '19 at 12:45
  • Please edit the question and paste in the code you are trying to make work. Tell what it is doing and what it should do. – lit Jan 23 '19 at 13:27
  • What's the content of `%filenames%`? `Ren` destination is filename.ext only (no path). Your `ren` command has only one parameter (watch your quotes). – Stephan Jan 23 '19 at 15:08
  • thank you.. I might be lame ..I am new to this and want to give a try. the above one is what i tried by googling ... on x system we rehave files with some configuration data. so we get constant updates and request to replace those files. So assume we have 1.txt,2.txt,3.txt. and recieved an updated file 1.txt. i am trying to automate the process to lto find the file with the name 1.txt and take a copy of it and then replace it with new file. – Drewdak16 Jan 24 '19 at 12:58

1 Answers1

1

As soon as you write a new version of a file into the same folder than the original (with the same name), it's already too late to rename the original - it doesn't exist anymore. You need two folders: one that receives new versions (new) and one where you keep your renamed files plus the new ones (old)

@ECHO OFF
setlocal
set "source=C:\Users\test\new"
set "destin=C:\Users\test\old"
set "files=*.txt"

REM for every matching file in the source folder:
for %%A in ("%source%\%files%") do (
  REM if there is such a file in the destination folder, rename it:
  if exist "%destin%\%%~nxA" ren "%destin%\%%~nxA" "%%~nA-%date%%%~xA"
  REM only then copy the file:
  copy "%%~fA" "%destin%\"
)

This will fail if you run it more than once per day. (More code needed to handle that; for example, adding time too)
It will also fail if your %date% contains characters that are not allowed in file names (my %date% looks like 29.01.2019). (More code needed to handle that; for example %date:/=-%)

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • thanks stephan..here is what my plan is .. move the file to different folder-->extract file name to txt file--> use for to identify that file int he folder and move it to backup folder by adding date format--> then move the incoming file to that folder. need to figure out the for /r to look in to a txt for the file name – Drewdak16 Jan 30 '19 at 10:21