Folder rename wildcard question

October 2, 2009 at 06:32:06
Specs: Windows XP
Hi I have a folder which contains a text file called wireworld.txt. I would like a batch file that can check for the txt file and if it finds it rename the folder to that name, e.g wireworld. The wireworld name can be set in the batch, it doesn't need to read it from the txt file.

The only snag is that the folder has a changing name. It always starts off with UK1012001PC and can have any two numbers on the end.

Example UK1012001PC36
or UK1012001PC49

sometimes there can be 3 or more UK1012001PC** folders, but only one folder will have the wireworld.txt in it. Could the batch file delete the UK101200PC** folders without the wireworld.txt.

Thanks

Mac


See More: Folder rename wildcard question

Report •


#1
October 2, 2009 at 09:57:52
Try this (untested):

setlocal

rem Set up these variables as appropriate
set filename=wireworld.txt
set folder_old_pattern=UK1012001PC??
set folder_new_name=wireworld

for /d %%d in (%folder_old_pattern%) do (
  if exist %%d\%filename% (
    ren %%d %folder_new_name%
  ) else (
    rd /s %%d
  )
)


Report •

#2
October 2, 2009 at 10:37:12
Thank you so much, it works like a charm.

I added /q to the last line so It doesn't ask me to confirm:

setlocal

rem Set up these variables as appropriate
set filename=wireworld.txt
set folder_old_pattern=UK1012001PC??
set folder_new_name=wireworld

for /d %%d in (%folder_old_pattern%) do (
if exist %%d\%filename% (
ren %%d %folder_new_name%
) else (
rd /q /s %%d
)
)

Many, many thanks and my kindest regards to you

Mac


Report •

Related Solutions


Ask Question