Page 1 of 1

Shed some light on me

Posted: 29 Oct 2011 08:00
by renzlo
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.

Re: Shed some light on me

Posted: 29 Oct 2011 10:14
by jeb
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.

Re: Shed some light on me

Posted: 29 Oct 2011 16:32
by renzlo
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

Re: Shed some light on me

Posted: 30 Oct 2011 05:12
by aGerman
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

Re: Shed some light on me

Posted: 30 Oct 2011 06:02
by renzlo
as always,many thanks aGerman.