Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch File to Rename Files In Subfolders 1

Status
Not open for further replies.

bmacbmac

IS-IT--Management
Jan 26, 2006
392
US
Hello,

I am looking to rename a file: Group1.ini to Group1.txt for each user in c:\documents and settings.

I'd like to do this as a batch file to run each day.

I tried:

Code:
 rename /s Group1.ini Group1.txt

While in c:\documents and settings.

I didn't think it would work but what the heck?

Does anyone know of a batch file or small .vbs script that will accomplish this?

Thanks!

Bri
 
Something like this would probably work. (untested)
Code:
@echo off
set basepath=c:\documents and settings
set oldname=Group1.ini 
set newname=Group1.txt

for /f %%a in ('dir "%basepath%" /a:b /b') do (
  if exist "%basepath%\%%a\%oldname%" (
       REM if the Group1.txt file already exists, delete it
       REM This will error if it doesn't, but who cares?
       del "%basepath%\%%a\%newname%"
       ren "%basepath%\%%a\%oldname%" %newname%
       )
  )
 
Thanks smah. I like where you are going with this. I would like to run on several machines so a 3rd party application is not preferred.

In your script I am getting an error: Parameter Format Not Correct - "b".

In command line I tried running dir /a:b and received the same error so I assume that's where the problem is. I tried a different parameter: /a:d to find the folder attributes. The script ran without errors, but my file was not renamed.

I also removed the %basepath% after the DIR command and it seemed to work!

Code:
@echo off

set basepath=c:\documents and settings
set oldname=Group1.ini
set newname=Group1.txt

for /f %%a in ('dir "%basepath%" /a:d /b /s') do (
      if exist "%%a\%newname%" (
       DEL "%%a\%newname%" 
       ren "%%a\%oldname%" %newname%
       )
  )

This works great when my basepath has no spaces in it. Testing in c:\temp works great. But, when trying a folder with spaces, it doesn't work. By taking the echo off I can see that it is trying to:

Del c:\documents\group1.txt (not c:\documents and settings\USERNAME\group1.txt)

I have tried all combinations of quotes, but can't get it to recognize that full path. Any ideas?
 
I believe double quotes should work like:
c:\> dir "documents and settings"

sam
 
Thanks mscallisto.

That works too. I think the script is running fine in the 'dir "%basepath% part to build the %%a variable. If I manually run dir "c:\documents and settings" OR c:\> dir "documents and settings" I get the same results.

I believe the problem comes lower in the script when it using the %%a variable as part of the path. Not sure how to make it include the entire path with spaces.
 
I couldn't quite come up with a full solution, but this may help; The default delimiter for the FOR statement is a space... It looks like adding "DELIMS=" tells it "don't use a delimiter".

for /f "DELIMS=" %%a in . . .
 
You could use the 8.3 notation for the folder names and just avoid the spaces all together:

C:\[red]Docume~1[/red]\...



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
or he could do this:
Code:
@echo OFF
SETLOCAL enabledelayedexpansion

set basepath="c:\documents and settings"
set oldname=Group1.ini
set newname=Group1.txt

for /f %%a in ('dir "%basepath%" /a:b /b') do (
  if exist "%basepath%\%%a\%oldname%" (
       REM if the Group1.txt file already exists, delete it
       REM This will error if it doesn't, but who cares?
       del "%basepath%\%%a\%newname%"
       ren "%basepath%\%%a\%oldname%" %newname%
       )
  )


Ben
"If it works don't fix it! If it doesn't use a sledgehammer..."
How to ask a question, when posting them to a professional forum.
Only ask questions with yes/no answers if you want "yes" or "no"
 
SETLOCAL enabledelayedexpansion didn't work for me. Still had the same problem. However, the 8.3 method worked perfectly!

Thanks for everyone's help.
 
I'm back.... I see that you've found some of the reasons for my 'untested' warning. Try this:

Code:
@echo off
set basepath=c:\documents and settings
set oldname=Group1.ini
set newname=Group1.txt
for /F "delims=," %%a in ('dir %basepath% /a:d /b') do (
	if exist "%basepath%\%%a\%oldname%" (
		REM if the Group1.txt file already exists, delete it
		REM This will error if it doesn't, but who cares?
		del "%basepath%\%%a\%newname%"
		ren "%basepath%\%%a\%oldname%" %newname%
		)
	)

So, yes you are correct. The original /a:b should have been /a:d. Secondly, guitarzan found the delimiter problem. One of the standard delimiters for a 'for' loop is the space character. As you can see, in this version I've specified the variable delimiter to be a comma character instead. Your %%a variable should never encounter a comma character, so that should take care of it. Alternatively, we could specify multiple 'tokens' to pick up both the first token (before the space), second token (after the first space), and potentially other tokens after additional space characters. See [!]for /?[/!] for more info. In a production situation, you might want to add some additional error checking logic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top