vendredi 19 janvier 2018

Batch to rename files and remove the date in filename

I have multiple directories and subdirectories and need to remove the date part of all filenames and keep the rest of the file name.

Ex.

Input: Dummy-Filename-V0001-01202018.txt

Output: Dummy-Filename-V0001.txt

Basically, I need to remove the "-01202018" part.

Notes:

  1. It should also cover other file format like ".pdf" ".csv" and others (not sure of all file types available)
  2. The date could be in different formats like DDMMYYYY, MMDDYYYY, YYYYMMDD, or other combination.
  3. So far, I've seen the date only at the end part of the filenames.
  4. I have files under /Sample/00AA/0001/file.txt up to /5000/ and there's also /Sample/00BB/ with sub folders.

I currently have the code below, I'm already able to loop through all the subdirectories and show files with dates, but the rename commands that I tried wont work. (see comments)

FOR /R "C:\Users\Dummy\Desktop\UAT\Sample\" %%G in (.) DO (
Pushd %%G
Echo now in %%G
echo before
dir
dir "*-01202018*"

REM ren *-01202018.txt *.txt
REM ren ???-01202018.txt ???.txt
REM rename 's/-01202018//' *
REM rename "*-01202018*" "*/////////*"

echo after
dir
Popd )
Echo "back home"

PAUSE

Aucun commentaire:

Enregistrer un commentaire