.bat script to rename files

Solved
DelNC Posted messages 2360 Status Membre -  
barnabe0057 Posted messages 14431 Registration date   Status Contributeur Last intervention   -
Hello everyone

I have been looking for two days on how to rename files.
They are named like this
file name - author name.txt

So far, I can remove spaces and replace them with an _
Replace é, è, ê with e
Replace à and â with a
...

I can't seem to delete the part that starts from the -
I tried using a token but I can't get it to work (I think it's a possible solution)

NB I know how to use the token for the content of a file.

Here is my current code
@echo off setlocal enabledelayedexpansion chcp 1252 for /r "C:\Users\mougi\Desktop\documents" %%a in (*.txt) do ( set "path=%%~nxa" set "path=!path: =_!" set "path=!path:é=e!" REM Remove part after the - set "path=!path:-*=!" move /y "%%~a" "%%~dpa!path!" ) pause


Thank you in advance for the help you will provide.

3 réponses

barnabe0057 Posted messages 14431 Registration date   Status Contributeur Last intervention   4 929
 
Good evening,

Try it like this:

@echo off
setlocal enableextensions enabledelayedexpansion
chcp 1252 >nul

cd /d "C:\Users\mougi\Desktop\documents" || exit /b 1

for /f "tokens=1,* delims=-" %%A in ('dir /b /a-d "*.txt"') do (
set "name=%%~A"
set "name=!name: =_!"
set "name=!name:é=e!"
ren "%%~A-%%~B" "!name!.txt"
)
pause


“Artificial intelligence is defined as the opposite of natural stupidity.”
2