Home > Back-end >  How can I restructure file names on bulk via batch script
How can I restructure file names on bulk via batch script

Time:04-29

I have 324 files on a Windows 10 machine which are named in the following pattern:

[7 Numbers] [Space] [Last name] [Space] [First name]

And I need them to be:

[Last name] [Space] [First name] [Space] [7 Numbers] 

I have done some quick research and found that I could write a batch script utilizing the 'rename' function:

@echo off
rename “y:\*.txt” “???-Test1.*”

However, I was unable to find out how I can program the script to take the first 7 chracters and put them to the end.

Any help is appreciated.

CodePudding user response:

Given the little detail on the exact formatting of your structures, i.e what happens in the event a surname has a split like van Halen which also now contains a space: anyway, this will cater for the situation as you've mentioned only and not for names/surnames containing spaces.

@echo off
for /f "tokens=1-3*" %%i in ('dir /b /a-d *.txt ^| findstr /R "^[1-9]"') do echo ren "%%i %%j %%k" "%%~nk %%~nj %%~ni%%~xk"

Note this example will simply echo the command and not perform the actual rename. You need to test it first before you do the renaming. Once you are happy with the result printed to console, then remove echo from the line.

Note. findstr is important here as we need to only perform the action if the file starts with numbers. You can define the findstr filter even more if you want to be more specific. Here I just focused on numbers in the beginning of any .txt file considering no name or surname should start with a number., Unless you're 50cent or some other random rapper.

CodePudding user response:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION 
rem The following setting for the source directoryis a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files\t w o"

FOR %%b IN ("%sourcedir%\??????? * *.txt") DO FOR /f "tokens=1*delims= " %%u IN ("%%~nb") DO SET /a junk=0&SET /a "junk=1%%u" 2>nul&IF !junk! geq 10000000 IF !junk! leq 19999999 ECHO REN "%%b" "%%v %%u.txt"

GOTO :EOF

Please note that this routine uses delayedexpansion

The first for puts the absolute filename of each file matching the mask into %%b

The second partitions the name part of the filename (%%~nb) into that part before the first space (token 1) to %%u and the remainder (token *) to %%v

junk is then set to 0 and then reset to the value of 1%%u. set /a will leave junk unchanged (therefore, 0) if %%u is not a numeric string (the 2>nul suppresses the error message) so if %%u is numeric, junk will be set to 10000000 ... 19999999.

Use !junk! to access the run-time value of junk, check it is within range and if so, echo the ren required.

Remove the echo keyword before the ren after checking the resultant report to actually rename the files.

  • Related