0

I use Windows 7 Ultimate. I have many folders in a directory which I need to rename with a fixed base name and a progressive number starting from 1 to infinite.

Path of folders have space and base folder is D:\Programmi Installati.

Example of folders to rename:

log_04-01-2019 15-15-11,51
log_03-01-2019 21-18-16,22
log_02-01-2019 6-21-17,34
log_01-01-2019 8-22-14,19
log_27-12-2018 14-23-18,28

Example of wanted folder names:

log_1
log_2
log_3
log_4
log_5

The numbers of folder to rename can be large, but the structure is the same.

Mofi
  • 46,139
  • 17
  • 80
  • 143
placidomaio
  • 111
  • 1
  • 13
  • Assuming an ntfs file system your above ordering is unlikely. In **what order** shall the incrementing number be applied? –  Jan 04 '19 at 18:57
  • The order of the folders is not required, actually the original will named with timestamp but i need to have it with part fixed (log_) and a progressive number (log_1, log_2, log_50, log_100 ...) – placidomaio Jan 04 '19 at 19:11

3 Answers3

1

Assuming you don't want to sort by folder dates based on log_04-01-2019 15-15-11,51 or 04-01-2019. Then the bellow solution will work. Here we are using the an FOR loop to get each of the folder names using dir %folder% /S /b /AD And expanding %%~nA for the raw folder names.

In the future if you wanted to sort by dates in the folder names, you can add another loop to grab the dates then use an IF to sort them. Feel free to search this site as there are multiple examples that can help you on that.

Rename.bat:

@ECHO OFF
@setlocal enabledelayedexpansion

Rem | Folder Path & CD To Location
Set "Folder=C:\Folder"
CD %Folder%

Rem | Get Raw File Name
Set "Number=1"
for /F "tokens=*" %%A in ('dir "log_*" /S /b /AD') do (

    Rem | Rename Folder || Raw Name - %%~n1
    rename "%%~nA" "log_!Number!"

    Rem | Add One To Number
    set /a "number+=1"

)

Goto :EOF

For help on any of the commands do the following:

  • call /?
  • set /?
  • for /?
  • if /?
  • find /?
  • So on.
John Kens
  • 1,615
  • 2
  • 10
  • 28
  • Please note that `%%~nA` is incorrect as it is used for files. It is odd here. However, your code is working because folders do not contain a dot (`.`). Please replace it with `%%A`. – double-beep Jan 05 '19 at 07:59
1

The batch file code for this folder renaming task:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "Counter=1"
for /F "delims=" %%I in ('dir "D:\Programmi Installati\log_*" /AD /B /ON 2^>nul') do ren "D:\Programmi Installati\%%I" "log_!Counter!" & set /A Counter+=1
endlocal
pause

The command FOR runs with cmd.exe /C (more precise %ComSpec% /C) in a separate command process in background the command line:

dir "D:\Programmi Installati\log_*" /AD /B 2>nul

DIR outputs to handle STDOUT of this background command process

  • just the names of all directories because of option /AD (attribute directory)
  • in bare format because of option /B without folder path
  • matching the wildcard pattern log_*
  • in specified directory D:\Programmi Installati
  • ordered by name because of option /ON.

DIR would output an error message to handle STDERR if it can't find any directory entry matching these criteria. This error message is redirected to device NUL to suppress it.

Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.

FOR captures all lines output to handle STDOUT of started command process and processes those lines after started cmd.exe terminated itself. It is very important for this folder renaming task that FOR runs on a list of folder names captured before doing the folder renames as otherwise the directory entries would change while FOR is accessing them. For that reason for /D can't be used in this case because of for /D would process the list of log_* directory entries while this list changes on each successful folder rename. The result would be directories not renamed or renamed multiple times or even an endless running loop depending on file system (NTFS or a FAT file system like FAT32 or ExFAT).

FOR with option /F ignores empty lines which do not occur here. FOR ignores also lines starting with a semicolon because of end of line option eol=; is the default. But all lines output by DIR start with log_ and for that reason the default end of line definition can be kept for this task.

FOR with option /F splits up a line by default to substrings using normal space and horizontal tab as delimiters and assigns just first space/tab separated string to specified loop variable. This line splitting behavior is not wanted here because loop variable I should hold directory name with all spaces. Therefore delims= is used to define an empty list of delimiters to disable the line splitting behavior.

The directory name assigned to loop variable I is referenced with %%I and full path not output by DIR as source name of command REN. The new folder name is log_ with the counter variable value referenced using delayed environment variable expansion.

The counter variable is incremented by one using a simple arithmetic expression after renaming the directory independent on directory renaming was successful of failed for various reasons.

The command PAUSE at end is added to see the error message output by command REN if renaming a directory failed. There is nothing output except the prompt by PAUSE on all directories could be renamed successfully.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • pause /?
  • ren /?
  • set /?
  • setlocal /?

See also single line with multiple commands using Windows batch file for an explanation of operator &.

PS: I recommend the usage of the shareware file manager Total Commander which has a built-in multi-rename tool for renaming files and folders for people with no coding experience. Download, install and start Total Commander, navigate to the folder containing all these folders, press Ctrl+A to select the folders, press Ctrl+M to open multi-rename tool window and the rest is self-explaining. If you need nevertheless help, press key F1 to open the help page for multi-rename tool.

Mofi
  • 46,139
  • 17
  • 80
  • 143
0

A powershell script which formats the number with leading zeroes to 3 places:

$iref = [ref]0 
Get-ChildItem 'D:\Programmi Installati\log_[0-9][0-9]-*' -Dir|
   Rename-Item -NewName {'log_{0:D3}' -f (++$iref.Value)}

To be on topic wrapped in cmd line/batch

powershell -NoP -C "$iref=[ref]0;Get-ChildItem 'D:\Programmi Installati\log_[0-9][0-9]-*' -Dir|Rename-Item -NewName {'log_{0:D3}' -f (++$iref.Value)}"

Sample result:

log_001
log_002
log_003
log_004
log_005

If no leading zeroes desired remove the :D3 part.