-2

I have a folder called TEST. Inside there are 30 files.

Example:

DIM1_UPI_20170102.TXT

DIM2_UPI_20170908.TXT

DIM3_UPI_20180101.TXT

...

I have to rename them by removing the date tag Exapmple:

DIM1_UPI.TXT

DIM2_UPI.TXT

DIM3_UPI.TXT

Can you please help me writing this in batch file?

Community
  • 1
  • 1
Jocky
  • 25
  • 4
  • Before you ask questions, take the [tour], read [Ask] and [MCVE]. Advice you study the output from `help for`, `help set` and `help dir`. Also search the site, this specific question has been answered quite well, multiple times. – jwdonahue Feb 08 '18 at 06:19

1 Answers1

0

Assuming your files are all starting with DIM

@echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir "*.TXT" /b /a-d') do (
 set "var=%%~ni"
 echo ren !var!%%~xi !var:~0,-9!%%~xi
)

Once you can confirm that it does what you want, and ONLY then, remove the echofrom the last line to actually rename the files.

Important Note. If you have files with similar names, but different date entries, this will not work as you think. as Example:

DIM2_UPI_20170910.TXT

DIM2_UPI_20170908.TXT

The names are the same, but dates differ, making each filename Unique. If you rename them, there can be only 1 DIM2_UPI.TXT So as long as you understand this, you will be fine.

Edit: based on Amazon drive question. Note you need to change the directory portion to how you access amazon drive.

@echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir "DIM*" /b /a-d') do (
 set "var=%%~ni"
 echo ren !var!%%~xi !var:~0,-16!%%~xi
)
Community
  • 1
  • 1
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Echo from the last line? You have `ren`. – jwdonahue Feb 08 '18 at 06:21
  • Hi - This is really helpful. There are few cases where we have FACT_UIP_20170908.TXT. So if I want to say that I only want to replace files- (STAR)_UIP_(STAR).TXT into (STAR)_UIP.TXT. For this to achieve how can I modify this code ? (Note STAR is asterisk) – Jocky Feb 08 '18 at 06:35
  • @Jocky Give me more info. Do all these files exist in a single directory? are they all TXT files? – Gerhard Feb 08 '18 at 06:36
  • Yes they are all TXT files – Jocky Feb 08 '18 at 08:32
  • @Jocky then simply use *.txt, see my edited answer. but you can make it whatever suits your need. `*UIP*` or `*_UIP*` or `*_UIP_*` or `*_UIP_*.TXT` etc – Gerhard Feb 08 '18 at 08:46
  • Okay Thank you.. I have one more similar requirement. I have access to amazon command line tool. I have a folder called TEST in S3 bucket. There I have four files. DIM1_UPI_20180101_091010.TXT, DIM1_TOP_20180101_091010.TXT, DIM2_UPI_20180101_091010.TXT and DIM2_TOP_20180101_091010.TXT. I have to rename those files such as DIM1_UIP.TXT,DIM1_TOP.TXT,DIM2_UIP.TXT,DIM2_TOP.TXT. Using For loop how should my code look like ? Also there is one more issue. Sometimes instead of *_TOP_*, I get files in this format *_TOOP_*. So basically I have to check both the conditions while renaming. – Jocky Feb 22 '18 at 08:53
  • @Jocky You would need to ask a new question. Copy the content of the comment, and add a link to this page for clarity purpose. – Gerhard Feb 22 '18 at 09:11
  • @Jocky I will have an answer ready for as soon as you posted you question. – Gerhard Feb 22 '18 at 09:22
  • Thank you.. I am not able to raise a new question as I have already crossed a limit. Can you please help ? – Jocky Feb 22 '18 at 09:44
  • @Jocky which limit? You only posted a question on 15 Feb, so you are allowed to post a new question by [clicking here](https://stackoverflow.com/questions/ask) – Gerhard Feb 22 '18 at 09:45
  • Not sure But I am not able to raise. I tried it. Okay could you please spot this error ? I have two statements written on Amazon command line. aws s3 mv s3://TEST/DIM1_.csv s3://TEST/DIM1.csv aws s3 mv s3://TEST/DIM2_.csv s3://TEST/DIM2.csv (note i am not able to see asterisk or start printed here, hence have written in a word) – Jocky Feb 22 '18 at 11:45