Shed some light on me

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
renzlo
Posts: 116
Joined: 03 May 2011 19:06

Shed some light on me

#1 Post by renzlo » 29 Oct 2011 08:00

Hi All,

Shed some light.

Here's the scenario:

I have a folder with lots of folders inside and inside those folders are .tif files. I want to create a program that will rename those images based from the list from my text file.

Here's the content of my text file:

Filename: woutbar.txt

Code: Select all

1.tif
2.tif
2_001.tif
3.tif
3_001.tif
4.tif
4_001.tif
5.tif
5_001.tif
6.tif
6_001.tif
7.tif
7_001.tif
8.tif
8_001.tif


Filename: withbar.txt

Code: Select all

1.tif
1_001.tif
2.tif
2_001.tif
3.tif
3_001.tif
4.tif
4_001.tif
5.tif
5_001.tif
6.tif
6_001.tif
7.tif
7_001.tif
8.tif
8_001.tif


if the count of tif files is equal to 16, the tif files will be renamed based from the contents of withbar.txt but if it's not 16 the tif files will be renamed based from the contents of woutbar.txt.

I've used for loop but it says duplicate file. Kindly help me. Thanks in advance.

jeb
Expert
Posts: 1059
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Shed some light on me

#2 Post by jeb » 29 Oct 2011 10:14

If you used a for loop, it would be nice to see what you tried.
Then we can explain why it fails and how to correct it.

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: Shed some light on me

#3 Post by renzlo » 29 Oct 2011 16:32

jeb wrote:If you used a for loop, it would be nice to see what you tried.
Then we can explain why it fails and how to correct it.


Thanks for the reply jeb,

Here it is:

Code: Select all

setlocal enabledelayedexpansion
for /f "tokens=1 delims=" %%a in ('dir /b /on "*."') do (
set "folder=%%a"
set "count=0"
for /f "tokens=1 delims=" %%b in ('dir /b /on "!folder!\*.tif"') do (
set "tif=%%b"
set /a "count+=1"
)
if "!count!"=="16" (
call :withbar
) else (
call :woutbar
)
)

:withbar
for /f "tokens=1 delims=" %%c in (withbar.txt)  do (
ren "!folder!\!tif!" "%%c"
)
goto :eof


:woutbar
for /f "tokens=1 delims=" %%d in (woutbar.txt)  do (
ren "!folder!\!tif!" "%%d"
)
goto :eof

aGerman
Expert
Posts: 4715
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Shed some light on me

#4 Post by aGerman » 30 Oct 2011 05:12

You have to read the output of DIR and the text file at the same time. Jeb shared a new SET /P technique which you can use synchronously with a FOR loop.
Untested:

Code: Select all

@echo off
setlocal enabledelayedexpansion
REM *** find each subfolder
for /d %%i in (*) do (
  REM *** change the working directory
  pushd "%%~i"
    echo current folder: !cd!
    REM *** count all .tif files
    set /a n=0
    for %%j in (*.tif) do set /a n+=1
    echo number of found files: !n!
    REM *** if 15 then use woutbar.txt
    if !n!==15 (
      REM *** read DIR output line by line
      for /f "delims=" %%j in ('dir /a-d /b /on *.tif') do (
        REM *** read text file line by line
        set /p "new="
        REM *** rename the files
        ECHO ren "%%j" "!new!"
      )
    )<"%~dp0woutbar.txt"
    REM *** if 16 then use withbar.txt
    if !n!==16 (
      REM *** read DIR output line by line
      for /f "delims=" %%j in ('dir /a-d /b /on *.tif') do (
        REM *** read text file line by line
        set /p "new="
        REM *** rename the files
        ECHO ren "%%j" "!new!"
      )
    )<"%~dp0withbar.txt"
  popd
  echo _______________________________________
)

pause

Remove the ECHO commands in front of REN if you think it would work.

Regards
aGerman

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: Shed some light on me

#5 Post by renzlo » 30 Oct 2011 06:02

as always,many thanks aGerman.

Post Reply