Renaming files using another file name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MonicaOM12
Posts: 3
Joined: 12 Dec 2011 13:21

Renaming files using another file name

#1 Post by MonicaOM12 » 12 Dec 2011 13:26

Here is what I want to do:
I have HTML files named as follows:
/content
diagram113660819130 - PREPAY AUDIT-content.html
diagram126591269210 - CAS MAIN-content.html

Image files named as follows:

/images
diagram1136608191.pdf
diagram1265912692.pdf

I want to rename the .pdf using the HTML file names
REN diagram1136608191.pdf 30 - PREPAY AUDIT.pdf
REN diagram1265912692.pdf 10 - CAS MAIN.pdf

The pdf name may be 16 or 17 characters long.

I am stuck with the following code

@echo off
del digfiles.txt
dir diagram*-content.html /B /O :N >digfiles.txt
FOR /F "tokens=1, 2, 3*" %%i in (digfiles.txt) do (
echo %%i %%j %%k %%l
echo.%i:~0,16%
dir ..\images\%%i:~0,16%*.pdf
)

I cannot get it to echo the first 16 characters
neither can I get the directory listing of the giveb pdf.
I am far from doing the rename if I cannot even seperate the first 16 characters...please help.
:?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Renaming files using another file name

#2 Post by dbenham » 12 Dec 2011 14:03

You can't use substring or search/replace operations on FOR variables. That only works with expansion of named environment variables.

Trying to use a substring operation will be difficult if the length you need varies.

As long as the pdf files do not contain <space> or <dash> in the filename, then this should work (untested): edit - fixed file location issue within rename

Code: Select all

for /f "tokens=1* delims=- " %%A in ('dir "diagram*-content.html" /b /o :n') do ren ..\images\%%A.pdf "%%~nB.pdf"

You should be able to run the command directly from the command line if you change each double %% into a single %

Dave Benham
Last edited by dbenham on 12 Dec 2011 14:28, edited 2 times in total.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Renaming files using another file name

#3 Post by orange_batch » 12 Dec 2011 14:06

Code: Select all

%i:~0,16%
%%i:~0,16%

This isn't acceptable syntax :)

Code: Select all

@echo off&setlocal enabledelayedexpansion
del digfiles.txt
dir diagram*-content.html /B /O :N >digfiles.txt
FOR /F "tokens=1, 2, 3*" %%i in (digfiles.txt) do (
echo %%i %%j %%k %%l
set "i_var=%%i"
echo.!i_var:~0,16!
dir ..\images\!i_var:~0,16!*.pdf
)

Delayed expansion lets you use ! ! to expand variables after character processing, unlike % %. So you can use it within parenthesized code with expected results.

Or, just some different conventions we use around here:

Code: Select all

@echo off&setlocal enabledelayedexpansion
del digfiles.txt
dir "diagram*-content.html" /b /on >digfiles.txt
for /f "tokens=1-3*" %%i in (digfiles.txt) do (
echo(%%i %%j %%k %%l
set "i_var=%%i"
echo(!i_var:~,16!
dir ..\images\!i_var:~,16!*.pdf
)

May be a better idea to listen to dbenham since I didn't actually check what your code is trying to do.

MonicaOM12
Posts: 3
Joined: 12 Dec 2011 13:21

Re: Renaming files using another file name

#4 Post by MonicaOM12 » 13 Dec 2011 09:38

Thanks so much for your response.
I have to understand the delayedexpansion better. I am trying this little code with no success.

My Code
Set var1=Hello ABC
Set var2=ABC
Set result=!var1:%var2%=World!
echo [!result!]

Here is what I get
[!result!]

What am I doing wrong. I had hoped to get Hello World.

MonicaOM12
Posts: 3
Joined: 12 Dec 2011 13:21

Re: Renaming files using another file name

#5 Post by MonicaOM12 » 13 Dec 2011 09:42

sorry ..had a spelling mistake in expansion..it works now. :)

Post Reply