0

I am renaming a bunch of files that begin with TB-Practice to begin with P-.

So far I have this batch

pushd %~dp0
for %%i in ("TB-Practice*.pdf") do (set fname=%%i) & call :rename
goto :eof
:rename
::Cuts off 1st eleven chars, then appends prefix
::P- is put in front of the file name
ren "%fname%" "P-%fname:~11%"
goto :eof

Unfortunately instead of ending up with P-Filename I end up with P- Filename which is incompatible with the current format I'm using for these files.

What am I doing wrong? There isn't a space anywhere when declaring the variables.

2 Answers 2

0

If the original file names all contain a space trailing TB-Practice, it is much easier to split on them.

On cmd line:

@for %A in ("TB-Practice *.pdf") do @for /f "tokens=1*" %B in ("%A") do @echo Ren "%A" "P-%C"

or in a batch file:

pushd %~dp0
for %%A in ("TB-Practice *.pdf") do for /f "tokens=1*" %%B in ("%%A") do Ren "%%A" "P-%%C" 
popd
0

Stupid me realized after I posted that the batch doesn't remove that space after TB-Practice and that's why a space was left over. Solved.

You must log in to answer this question.