Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Renaming Files  (Read 2408 times)

0 Members and 1 Guest are viewing this topic.

MMBBRWD

  • Guest
Renaming Files
« on: February 06, 2008, 09:40:20 AM »
Hi,

  I am new to DOS programming and have another renaming question.  I realize that there are a lot of renaming posts out there, but I have not found one which addresses my issue.

  I basically need to rename all files in a folder by pre-pending them with the folder name, i.e. C:\Test\File.tif becomes C:\Test\Test-File.tif.

  I have come up with the following code and it comes close to working

for %%a in ("%CD%") DO SET CurrentFolder=%%~na

for %%B in (*.tif) do rename %%B %currentFolder%-%%B

Run against the following files I get the following results:

C:\CSCAN\FILE1.TIF  becomes C:\CSCAN\CSCAN-CSCAN-FILE1.TIF
C:\CSCAN\FILE2.TIF  becomes C:\CSCAN\CSCAN-FILE2.TIF
C:\CSCAN\FILE3.TIF  becomes C:\CSCAN\CSCAN-FILE3.TIF

Does anybody have an explanation for what is going on here?

Thanks in advance.

Bob



gpl



    Apprentice
  • Thanked: 27
    Re: Renaming Files
    « Reply #1 on: February 06, 2008, 11:23:53 AM »
    Somehow the first file has the directory added twice, I think that the first rename 'moves' the file so that it is picked up a second time.

    I suggest you use your loop to output the directory list into a file then process the file, that will guarantee that it is not seen twice by the loop.

    Graham

    MMBBRWD

    • Guest
    Re: Renaming Files
    « Reply #2 on: February 06, 2008, 12:25:22 PM »
    Took your advice and used a temporary file.  Worked like a charm.  Thanks for your help.

    rwd

    sss0

    • Guest
    Re: Renaming Files
    « Reply #3 on: February 07, 2008, 09:18:14 PM »
    It's funny that I stumbled across this.

    Just recently I have the exact same need. Could you post your solution?