V
V
Vladimir2021-06-03 12:28:30
cmd/bat
Vladimir, 2021-06-03 12:28:30

How to bulk rename numbered PDF files?

Good time of the day!!!
In my work, I have to make registers with a list of files in a folder.
The sequence of files must be saved in the registry regardless of the numbering (for example: 1. AOCP_#1 2.AOCP_#2 3.AOCP_#2), if you simply manually delete the numbering, the order will be corrected (AOOK_#2, AOCP_#1, AOCP_# 2). It is also necessary to replace any existing numbering such as 1,2,3 or 01,02,03 with 001,002,003, again preserving the sequence of files after renaming.

I throw off my experience on renaming files by type 001,002,003. Batch file
This batch file numbers pdf files in a folder by type 001,002,003

@echo off
setlocal enableextensions EnableDelayedExpansion

set n=1000
for %%a in (*) do if /i "%%~xa"==".pdf" (
set /a n+=1
set num=!n:~1!
call ren "%%a" "!num!.%%a"
)
pause


If possible, a batch file is needed, since there is no possibility to install third-party programs on a working computer.
I hope my question is as clear as possible.
In fact, you need to rename the files from 1,2,3 or 01,02,03 to 001,002,003 while maintaining the order of the files in the folder.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2021-06-03
@Equess

The difficulty is that you need to extract its number from the file name and add the required number of zeros.
You can extract the number by driving the filename into a for /f loop and dividing it into tokens. Of the tokens, we are only interested in 1 token (number):

for %%a in (*) do if /i "%%~xa"==".pdf" for /f "tokens=1,* delims=." %%b in (%%a) do (
  echo %%b
)

In the inner loop, the %%b variable will contain the file number.
In order to add the required number of zeros, you need to know the length of the number.
Take the function of determining the length of a string on the gray forum .
Haven't used it once myself.
Well, then just add 3 zeros in front to the file name, and then cut off the extra ones. The set command can select substrings.
For reference:
for /?
set /?

V
Vladimir Korotenko, 2021-06-03
@firedragon

Take a closer look at vbs or PowerShell, the first one is generally everywhere. The second on more or less modern machines. It's just that cmd is somewhat limited by default.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question