Curious
Extraordinary Member
- Joined
- Mar 2, 2010
- Messages
- 146
- Thread Author
-
- #1
How do I rename many files in a directory using a .bat file instead of doing it one by one with File Explorer?
I would appreciate the complete command because I haven't found anything good online and nothing I've seen has worked. It always gives the error: "Invalid syntax"
Thank you
I would appreciate the complete command because I haven't found anything good online and nothing I've seen has worked. It always gives the error: "Invalid syntax"
Thank you
Solution
The problem is your command line syntax: filenames with spaces (and special characters) must be in quotes. Also, if you literally typed the
(or
** (bold markers), those are not valid in CMD and will break the command.Correct command in Command Prompt (CMD)
Run this exactly:ren "001 - APOSTA EUROMILHÕES 056 - 15-07-2022 - SEM PRÉMIOS.pdf" "001.pdf"(or
rename instead of ren — same thing)Why yours failed
CMD splits parameters by spaces. So without quotes it reads it like:- old name =
001 - extra tokens =
-,APOSTA,EUROMILHÕES, …
…which makes the syntax invalid.
If it still errors, check these 3 common causes
1)...
- Joined
- Mar 14, 2023
- Messages
- 97,340
Here are working, copy‑paste batch (.bat) templates to rename many files at once. The #1 reason you see “Invalid syntax” is usually one of these:
Example result:
Example:
1) Add prefix/suffix? (what text)
2) Replace a word/character? (old → new)
3) Rename to sequential numbers only? (e.g.,
4) Filter by extension only? (e.g., only
Post 2–3 example “before → after” filenames and I’ll tailor a single final .bat that matches exactly.
- In a .bat file you must use double percent:
%%F(not%F). - If filenames/paths have spaces you must quote them:
"%%F". rencan’t rename across folders in one command (new name must be just a name, not a full path).
0) The simplest bulk rename (add a prefix to every file)
Save this asrename_prefix.bat and run it inside the folder:
Code:
[USER=35331]@echo[/USER] off
setlocal rem Add a prefix to every file in the current folder:
for %%F in (*.*) do ren "%%F" "NEW_%%~nxF" endlocal
report.docx → NEW_report.docx1) Change extension for many files (e.g., .txt to .log)
Code:
[USER=35331]@echo[/USER] off
ren "*.txt" "*.log"
2) Replace text in filenames (e.g., replace spaces with underscores)
This one uses delayed expansion (needed for string replace):
Code:
[USER=35331]@echo[/USER] off
setlocal EnableDelayedExpansion rem Replace OLD with NEW in filenames:
set "OLD= "
set "NEW=_" for %%F in (*.*) do ( set "name=%%~nxF" set "newname=!name:%OLD%=%NEW%!" if /I not "!name!"=="!newname!" ren "%%F" "!newname!") endlocal
3) Rename with numbering (01, 02, 03... keeping original extension
Code:
[USER=35331]@echo[/USER] off
setlocal EnableDelayedExpansion set /a n=1
for %%F in (*.*) do ( set "num=00!n!" set "num=!num:~-2!" ren "%%F" "!num!_%%~nxF" set /a n+=1) endlocal
photo.jpg → 01_photo.jpg, next becomes 02_...4) Run it against a specific folder (drag & drop a folder onto the .bat)
This version renames files in the folder you pass as the first argument:
Code:
[USER=35331]@echo[/USER] off
setlocal EnableExtensions set "TARGET=%~1"
if not defined TARGET set "TARGET=%cd%" pushd "%TARGET%" || ( echo Cannot open folder: "%TARGET%" exit /b 1) for %%F in (*.*) do ren "%%F" "NEW_%%~nxF" popd
endlocal
Safety tip (dry-run first)
To preview without renaming, changeren to echo ren, e.g.:for %%F in (*.*) do echo ren "%%F" "NEW_%%~nxF"Quick question (so I give you the exact final command)
What rename pattern do you want?1) Add prefix/suffix? (what text)
2) Replace a word/character? (old → new)
3) Rename to sequential numbers only? (e.g.,
IMG_0001.jpg)4) Filter by extension only? (e.g., only
.pdf)Post 2–3 example “before → after” filenames and I’ll tailor a single final .bat that matches exactly.
Curious
Extraordinary Member
- Joined
- Mar 2, 2010
- Messages
- 146
- Thread Author
-
- #3
Hello,
He does'n wok fo any options you gave to me.
Hee is one example of a command I used in command Prompt:
First I put, befoe the command in the diectoy I have the files: "E:\DOWNLOADS\TEMP1>"
rename **001 - APOSTA EUROMILHÕES 056 - 15-07-2022 - SEM PRÉMIOS.pdf** 001.pdf
Response "The syntax of the command is incorrect"
Thank you
He does'n wok fo any options you gave to me.
Hee is one example of a command I used in command Prompt:
First I put, befoe the command in the diectoy I have the files: "E:\DOWNLOADS\TEMP1>"
rename **001 - APOSTA EUROMILHÕES 056 - 15-07-2022 - SEM PRÉMIOS.pdf** 001.pdf
Response "The syntax of the command is incorrect"
Thank you
- Joined
- Mar 14, 2023
- Messages
- 97,340
The problem is your command line syntax: filenames with spaces (and special characters) must be in quotes. Also, if you literally typed the
(or
Confirm you can see the file listed exactly.
3)
If a file called
** (bold markers), those are not valid in CMD and will break the command.Correct command in Command Prompt (CMD)
Run this exactly:ren "001 - APOSTA EUROMILHÕES 056 - 15-07-2022 - SEM PRÉMIOS.pdf" "001.pdf"(or
rename instead of ren — same thing)Why yours failed
CMD splits parameters by spaces. So without quotes it reads it like:- old name =
001 - extra tokens =
-,APOSTA,EUROMILHÕES, …
…which makes the syntax invalid.
If it still errors, check these 3 common causes
1) You’re not in the right folder
Run:
Code:
cd /d "E:\DOWNLOADS\TEMP1"
dir
2) The filename is not exactly what you typed
To avoid typos, use TAB completion:- Type:
ren "001 - Press Tab until the full filename appears.
- Finish with:
" "001.pdf"
3) 001.pdf already exists
If a file called 001.pdf is already there, rename will fail (usually with a different message). Check:dir 001.pdfIf you want a .BAT that renames many files like that
Tell me the rule you want, for example:- “Rename each PDF to just the first 3 digits” (001.pdf, 002.pdf, …), or
- “Remove everything after the first ‘ - ’”, etc.
Curious
Extraordinary Member
- Joined
- Mar 2, 2010
- Messages
- 146
- Thread Author
-
- #5
.BAT FILE
@echo on
setlocal enableDelayedExpansion
pushd e:\Downloads\Temp1
ren "013 - APOSTA EUROMILHÕES 036 - 05-05-2023 - SEM PRÉMIOS.pdf" "013.pdf"
endlocal
WITH THIS BAT DOES NOT WORK AND I CAN’T SEE THE ERROR DISPLAYED ANYWHERE
CMD PROMPT
E:\DOWNLOADS\TEMP1>ren "013 - APOSTA EUROMILHÕES 036 - 05-05-2023 - SEM PRÉMIOS.pdf" "013.pdf"
THIS COMMAND RUNS PERFECTY
@echo on
setlocal enableDelayedExpansion
pushd e:\Downloads\Temp1
ren "013 - APOSTA EUROMILHÕES 036 - 05-05-2023 - SEM PRÉMIOS.pdf" "013.pdf"
endlocal
WITH THIS BAT DOES NOT WORK AND I CAN’T SEE THE ERROR DISPLAYED ANYWHERE
CMD PROMPT
E:\DOWNLOADS\TEMP1>ren "013 - APOSTA EUROMILHÕES 036 - 05-05-2023 - SEM PRÉMIOS.pdf" "013.pdf"
THIS COMMAND RUNS PERFECTY
- Joined
- Mar 14, 2023
- Messages
- 97,340
What you’re seeing is very typical: the command works in an interactive CMD window, but the .BAT “does nothing” because:
1) You copied forum formatting into the BAT
This line is not valid batch syntax:
That’s WindowsForum BBCode. In a real
(or
2) When you double‑click a BAT, the window often closes immediately, so you don’t see the error.
You need a
Now double-click it. You will see what fails (folder not found, file not found, access denied, etc..
When you save the
If it errors, you’ll see the message and it won’t vanish.
If you paste here the output shown by that BAT (it will list the file, then show the rename attempt + ErrorLevel), I’ll tell you the exact reason it fails on your machine.
1) You copied forum formatting into the BAT
This line is not valid batch syntax:
[USER=35331]@echo[/USER] onThat’s WindowsForum BBCode. In a real
.bat it must be exactly: [USER=35331]@echo[/USER] on(or
@echo off)2) When you double‑click a BAT, the window often closes immediately, so you don’t see the error.
You need a
pause or run it from an already-open CMD.Fix: use this exact BAT (copy/paste)
Open Notepad, paste this exactly, save as e.g.test-rename.bat:
Code:
[USER=35331]@echo[/USER] on
setlocal pushd "E:\Downloads\Temp1" || ( echo FAILED: cannot open folder pause exit /b 1) echo Current folder is:
cd echo Listing the exact filename:
dir /b "013 - APOSTA EUROMILHÕES 036 - 05-05-2023 - SEM PRÉMIOS.pdf" echo Renaming...
ren "013 - APOSTA EUROMILHÕES 036 - 05-05-2023 - SEM PRÉMIOS.pdf" "013.pdf"
echo ErrorLevel=%errorlevel% popd
endlocal
pause
Very important: save the BAT with the right encoding
Because your filenames contain á/õ/É etc., Windows 11 Notepad often saves as UTF‑8, and CMD batch files can behave oddly with accented characters.When you save the
.bat:- Notepad → File → Save As
- Save as type: All files
- Encoding: choose ANSI (or “Windows-1252” if your Notepad offers it)
- File name:
test-rename.bat
Also verify you really saved a .BAT (not .BAT.txt)
In File Explorer:- View → Show → File name extensions
Make sure it istest-rename.batand nottest-rename.bat.txt.
One more quick test (best diagnostic)
Open CMD, go to the folder, and run your BAT from there:
Code:
cd /d "E:\Downloads\Temp1"
test-rename.bat
If you paste here the output shown by that BAT (it will list the file, then show the rename attempt + ErrorLevel), I’ll tell you the exact reason it fails on your machine.
Curious
Extraordinary Member
- Joined
- Mar 2, 2010
- Messages
- 146
- Thread Author
-
- #7
Hello,
Anothe question now, please,
1 - I have some files in an Excel spreadsheet to rename, such as: STUNNING CLIFFS AT SUNSET.
2. How can I make an automatic change in Excel without doing it one by one to make it look like this?
3. ffmpeg -I "STUNNING CLIFFS AT SUNSET.MP4" " STUNNING CLIFFS AT SUNSET.MP4.AVI"
4 - Then I would just copy it to the .BAT file to RUN “ffmpeg -i” to convert everything at once.
5 - I have 113 files to do this.
Anothe question now, please,
1 - I have some files in an Excel spreadsheet to rename, such as: STUNNING CLIFFS AT SUNSET.
2. How can I make an automatic change in Excel without doing it one by one to make it look like this?
3. ffmpeg -I "STUNNING CLIFFS AT SUNSET.MP4" " STUNNING CLIFFS AT SUNSET.MP4.AVI"
4 - Then I would just copy it to the .BAT file to RUN “ffmpeg -i” to convert everything at once.
5 - I have 113 files to do this.
- Joined
- Mar 14, 2023
- Messages
- 97,340
You don’t actually need Excel for this (a
If you want, paste 2–3 example rows from your Excel and confirm: are the names in Excel without extension (like your example) or do they already include
.bat can convert all .mp4 in a folder automatically), but I’ll show both ways:Important corrections first
- In ffmpeg the input option is lowercase:
-i(not-I). - Your example output has a leading space before the filename:
" STUNNING..."→ remove that.
ffmpeg -i "STUNNING CLIFFS AT SUNSET.mp4" "STUNNING CLIFFS AT SUNSET.avi"Option A (Recommended): one BAT converts all MP4 in the folder
Createconvert_all_mp4_to_avi.bat like this:
Code:
[USER=35331]@echo[/USER] off
setlocal
pushd "E:\Downloads\Temp1" || (echo Folder not found & pause & exit /b 1) rem (Optional) overwrite output without asking:
rem add -y
for %%F in (*.mp4) do ( echo Converting: "%%F" ffmpeg -y -i "%%F" "%%~nF.avi") popd
endlocal
pause
- It will convert all
*.mp4inE:\Downloads\Temp1 - Output will be
same name.avi
Code:
mkdir "AVI" 2>nul
for %%F in (*.mp4) do ffmpeg -y -i "%%F" "AVI\%%~nF.avi"
Option B: Use Excel to generate 113 ffmpeg command lines
Step 1 — Put your base names in column A
ExampleA2:STUNNING CLIFFS AT SUNSETStep 2 — In B2, generate the ffmpeg line
Paste this formula into B2:="ffmpeg -y -i """ & TRIM(A2) & ".mp4"" """ & TRIM(A2) & ".avi"""TRIMremoves accidental leading/trailing spaces (very common).- The
"""pattern is how Excel outputs a literal quote".
Step 3 — Copy Column B into a .BAT
- Copy the cells in column B (the generated commands).
- Paste into Notepad.
- Add at the top:
Code:
[USER=35331]@echo[/USER] off
pushd "E:\Downloads\Temp1"
- Add at the bottom:
Code:
popd
pause
- Save as:
convert_list.bat
One more caution (AVI is an old container)
MP4 → AVI usually requires re-encoding and can increase file size. Your simple command will work, but if you get playback problems, tell me what codec the MP4 uses (H.264? H.265/HEVC? and I’ll give you the best ffmpeg settings for AVI.If you want, paste 2–3 example rows from your Excel and confirm: are the names in Excel without extension (like your example) or do they already include
.mp4?Similar threads
- Replies
- 5
- Views
- 3K
- Replies
- 5
- Views
- 2K
- Solved
- Replies
- 2
- Views
- 3K