0

I'm writing to you because I need to create a batch file that allows me to rename many files in .pdf keeping only the first 50 characters.

I've tried this:

for %i in (*.txt) do (set fName=%i)
ren %fName% %fName:~0,-11%.txt

But this removes only the last 11 characters. I've tried to modify this code but I can in no way do what I need.

I think it's a very easy thing for someone who is a little more expert than me so I ask you if you can help me.

Thanks in advance.

I insert below the errors that come to me following the corrections:

C:\Users\---\Desktop\Conversione PDFA>for %fname:~0,50% in (*.pdf) do (set fname=%~ni)
graeiojpoghnpiofsdnpibnwapobnslnsdlekpfèwekrgpjbm was unexpected at this time.

C:\Users\---\Desktop\Conversione PDFA>for %fname:~0,50% in (*.pdf) do (set fname=%~ni)
graeiojpoghnpiofsdnpibnwapobnslnsdlekpfèwekrgpjbm was unexpected at this time.

C:\Users\---\Desktop\Conversione PDFA>for %fname:~0,50% in (*.pdf) do (set fname=%i)
graeiojpoghnpiofsdnpibnwapobnslnsdlekpfèwekrgpjbm was unexpected at this time.



C:\Users\---\Desktop\Conversione PDFA>ren %fName% !fname:~0,50!.pdf
The syntax of the command is incorrect.

C:\Users\---\Desktop\Conversione PDFA>ren %fName% %!fname:~0,50!.pdf
The syntax of the command is incorrect.

  • 4
    remove the last 11 chars is exactly, what you coded. Read the output of `set /?` to learn more about substring processing. – Stephan Nov 13 '19 at 20:14
  • Hint #1: `%fname:~0,50%`. Hint #2 `set fname=%%~ni`. Hint #3 `setlocal enabledelayedexpansion` combined with `!fname:~0,50!.txt` inside the for loop, not outside. – avery_larry Nov 13 '19 at 20:26
  • ...not forgetting to `endlocal` before ending the loop too! Please note that from a batch file, you'd also need to change `%i` to `%%i`, and then I'd suggest for better portability to change `!fname:~0,50!.txt` to `!fname:~0,50!%%~xi`. – Compo Nov 14 '19 at 01:01
  • Hi, thanks for your feedback. I tried to make the corrections you recommended but I only get syntax errors. Most likely it is not clear to me where to make the change. Can you please support me? Thanks – Umberto Roselli Nov 14 '19 at 09:52

1 Answers1

0

for %fname:~0,50% ...: just no.
for %i in (*.pdf) do set fname=%i: yes.

You want to rename several PDF's, so the ren command should be within the loop. That's possible but ugly and error-prone (especially when you are not (yet) firm in cmd syntax) in a single command line, so I wouldn't' suggest it. Your best approach is the use of a batch file. It's much more readable and maintainable.

@echo off
setlocal enabledelayedexpansion
for %%i in (*.pdf) do (
  set "fname=%%~ni"
  ECHO ren "%%~fi" "!fname:~0,50!%%~xi"
)

See for /? for an explanation of the modifiers %%~ni etc.
See delayed expansion for the usage of !fname! instead of %fname%

NOTE: I "disarmed" the ren command for security reasons by just echoing it. When you have verified, it's exactly what you want, just remove the ECHO

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thank you very much it worked, but now I would like to cut the first part not the last, may you please tell me what to change? Thanks – Umberto Roselli Nov 20 '19 at 16:38
  • `!fname:~0,50!` means "take a substring starting at position `0` (the first character) and take `50` characters. `!fname:~50` takes the string starting at position `50` (the 51st char) until the end of the string (because the second parameter (length) is missing). To extract the last 50 chars, use `!fname:~-50!`(start at position `50` from the end; length is missing again, so until the end of the string) It's all described in `set /?` with examples. – Stephan Nov 20 '19 at 16:46
  • It worked, thank you! the final code is this `@echo off setlocal enabledelayedexpansion for %%i in (*.pdf) do ( set "fname=%%~ni" ren "%%~fi" "!fname:~-46!%%~xi" )` – Umberto Roselli Nov 21 '19 at 14:40