Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch script to prefix file names

I am trying to rename files in a batch script like this:

rename %FOLDER%\* 1-*

but when I run the script It overwrites the first two characters of the original names with the prefix "1-" instead of adding it to the beginning of the file names. How can I work around this?

like image 630
user1110477 Avatar asked Feb 22 '23 01:02

user1110477


2 Answers

Rename will just rename the file, you would need to call the file name as a variable after the prefix. The below is what ended up working.

cd %folder%
for %%a in (*) do rename "%%a" "1-%%a"
like image 159
MaskedPlant Avatar answered Mar 08 '23 04:03

MaskedPlant


try this as a starting point

@echo off 
for %%a in (%folder%\*) do (
  echo ren "%%~fa" "1-%%~nxa"
)
like image 43
PA. Avatar answered Mar 08 '23 05:03

PA.