How to batch transfer rename?

Viewed 75

There are two folders holding equal amounts of files between them. I'd like to apply the names from one set ; to the other set of files in no particular order. Inherit the name, but retain the extension .

Input files are .bmp Output files are .ini ( has a gear symbol ).

Example :

 folder 1-  Flowers.bmp  ; folder 2- blank.ini
. To this:
 folder 1-  Flowers.bmp  ; folder 2- Flowers.ini

There would be more files , but equal . The .ini files are all copies . So they may have a generic name and numbered if that matters to know . Those would all receive one name each from the other .bmp files in the other folder.

Normally I have both folders situated on the Desktop . I make sure both folders have equal number of files between them . That would be a constant .

I'm trying to stream line some menial repetitive daily tasks . I did search and what I have found does not really help.

@ECHO OFF
SET "vers=%~1"
IF "%vers%" == "" SET /P "vers=Enter Vers: "
FOR %%F IN (file_XX_*.*) DO CALL :process "%%F"
GOTO :EOF

:process
SET "name=%~nx1"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "name=!name:_XX_=_%vers%_!"
RENAME %1 "%name%"
ENDLOCAL

Hoping to find a solution to this finally .

2 Answers

Ok, here is a slightly long version, but it makes sure it gets the content of each folder.

Note You must ensure that the path to both folders are correctly specified in the third and fourth line:

@echo off
setlocal enabledelayedexpansion
set "source=%userprofile%\Desktop\folder 1"
set "destination=%userprofile%\Desktop\folder 2"
set /a num=0
for %%a in ("%source%\*") do (
  set /a num+=1
  set "fr!num!m=%%~na"
)
set /a oldn=!num!
set /a num=0
for %%b in ("%destination%\*") do (
  set /a num+=1
  set "to!num!r!=%%~nxb"
  set "ext=%%~xb"
)
pushd "%destination%"
for /l %%i in (1,1,!oldn!) do ren "!to%%ir!" "!fr%%im!%ext%"
popd
pause

You can remove pause at the bottom of the script, once you are happy with the output.

This is not the most effective way, but considering your limited batch experience, I guess an understandable approach is better than an optimized one (using array-like variables).

@echo off
setlocal enabledelayedexpansion

(for %%A in ("folder 1\*.bmp") do echo %%~nA)> "%temp%\names.txt"

<"%temp%\names.txt" (
for %%A in ("folder 2\*.ini") do (
  set /p "name="
  ECHO ren "%%A" "!name!.ini"
))

The first for loop creates a list of the desired names (from Folder 1). The modifier %%~nA returns the name only.

The second for processes each file in Folder 2 and takes a name from the previously generated file for each file. This depends on the fact that the number of files in both folders are the same, else you may get undesired results.

I disabled the actual ren command by just echoing it. Check the output and only when you are satisfied, remove the ECHO.

Related