Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

batch file to iterate and rename files

Hi Experts!

I would like to create a batch file to do the following:

Scenario:

Root folder: Test

Sub-folder’s name:      A_files; B_files … Z_files

Sub-folder’s content: image001.jpg – image999.jpg

Pseudo-code:

Loop through all subfolders of Test
Read sub-folder’s name into %folderName%

     Loop through all files with the sub-folder
          Rename filename by %folderName% + “_” + current filename
          // example: ren image001.jpg by A_files_image001.jpg
     End of Loop

End of Loop

Many thanks

Duke001
0
Duke001
Asked:
Duke001
  • 7
  • 4
1 Solution
 
Bill PrewCommented:
This should do the job, save as a BAT file and adjust the BaseDir if needed.  Also NOTE, it will only display the renames that would occur to the screen, not do them yet.  This will allow you to test it first.  If the REN commands displayed look right then remove the ECHO before the REN command, and then rerun to do the renames.

@echo off
set BaseDir=c:\Test
for /D %%A in ("%BaseDir%\*.*") do (
  for %%B in ("%%A\*.jpg") do (
    ECHO ren "%%~B" "%%~nA_%%~nxB"
  )
)

Open in new window

~bp
0
 
Bill PrewCommented:
If that does work for you would you consider adding the "MS DOS" zone to this question, there are a few of us (myself included) seeking points in that zone, and this question fits.

~bp
0
 
Duke001Author Commented:
Hi billprew!

Thanks for doing this.
I have run the .bat but seem to don't produce effect as the file names still unchanged.
Would you please explain briefly the meaning of the command and its attribute as I could try to spot the problem?

The other thing is that the sub-folder names are like to be as QXXXX_files where the number of characters is variable.

I am going to add this into "MS DOS" zone right now.

Thanks
Duke001
0
Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

 
Bill PrewCommented:
When you ran the script did it display something to the screen, like:

ren "c:\temp\A_files\iimage001.jpg" "A_files_image001.jpg"
ren "c:\temp\A_files\iimage002.jpg" "A_files_image002.jpg"

that's what should have happened, but the files wouldn't have been renamed.

To get them to rename, once the above looks good, you need to change:

    ECHO ren "%%~B" "%%~nA_%%~nxB"

to

    ren "%%~B" "%%~nA_%%~nxB"

as I mentioned above.

~bp
0
 
Duke001Author Commented:
my fault, I forgot to del the "@echo" bit.
it is working fine.
Before I assigned the points would youl please help me to mask the sub-folder name (wildcards!)  which as i mentioned starts by QXXXXX_files where XXXXX varies?

Thanks
0
 
Bill PrewCommented:
This should handle that need.

@echo off
set BaseDir=c:\Test
for /D %%A in ("%BaseDir%\Q*_files") do (
  for %%B in ("%%A\*.jpg") do (
    ECHO ren "%%~B" "%%~nA_%%~nxB"
  )
)

Open in new window

~bp
0
 
Bill PrewCommented:
I still don't see the MS DOS zone listed.  It's located at: Microsoft, Microsoft OS, MS DOS.

~bp
0
 
Duke001Author Commented:
I only see "ADD TAGS" button.
Sorry to ask you, how can I add a new zone?

Thanks
0
 
Bill PrewCommented:
Sorry, I think you have to use the "request attention" link and ask a moderator to add it.

~bp
0
 
Duke001Author Commented:
Very helpfull.
0
 
Bill PrewCommented:
Welcome.

~bp
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now