Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Batch files - Outputting filenames from within a FOR loop

Batch files - Outputting filenames from within a FOR loop

Batch files - Outputting filenames from within a FOR loop

I am wishing to rename files with a batch command and capture the original and renamed files in a list in order to provide a cross reference (doing a DIR /OS >file both before and after the batch file is not rigorous enough.

The file below carries out the renaming fine, but my understanding is not good enough to get the output bit working (if that is possible even!). Also the output in the example below is different depending on whether output is to file or screen.

Any help would be appreciated to increase my understanding of this.

@echo off
set /a c=%1


For %%G in (*.tif) do (
echo %%G >infile.txt
ren %%G abc.!c!.tif
set /a c=c+1
echo %%G >outfile.txt


RE: Batch files - Outputting filenames from within a FOR loop

I don't think you can do this in the current format because

1) I don't think you can run a series of commands in a for loop (I could be wrong on this) and

2) if you are looping in a folder and renaming in said folder, it will loop onto itself

What I did was create two batch files and have it copy the files to a different location. Mine were called test1.bat and test2.bat

test1.bat contained


@echo off
set /A c=1
For %%G in (*.jpg) do test2.bat %%G

test2.bat contained


echo %1 >> infile.txt
@copy %1 d:\test\abc.%c%.jpg
@if exist "abc.%c%.jpg" @echo abc.%c%.jpg >> outfile.txt
@set /A c=c+1

RE: Batch files - Outputting filenames from within a FOR loop

Thanks 'tootiredtocare' for your input. I am trying to avoid the need to copy the files to rename them as they are about 50+Gb of tif images.
In answer to your point re multiple commands in For loop, yes you can do it if you use ( ), as in the fragment.

After sleeping on it I found the answer to the problem - so often the way - see fragment below. I only have to write the  name of the file that is about to be renamed as I 'know' what I am about to call it! Obvious in the end.
set /a c=%1
set param=%2

FOR  %%i in (*.tif) do (
        echo %%i >>in.txt
        ren %%i %param%!c!.tif
        echo %param%!c!.tif >>out.txt
        set /a c=c+1
All I need to do now is put in a test to stop it renaming again the first file it renamed (can happen depending on the number being used in var 'c' and the original file names).

Aren't Batch commands wonderful!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close