ren -- dumy question (as always...)

  • Thread starter Thread starter Matthieu
  • Start date Start date
M

Matthieu

hi there,

I have some files in a directory.
files are named this way :
~xxxx~.xls

Ans I would like to rename the files as :
xxxx - directoryname.xls

Could someone tell how to do that ?

Thanks a lot.
Matthieu
 
hi there,

I have some files in a directory.
files are named this way :
~xxxx~.xls

Ans I would like to rename the files as :
xxxx - directoryname.xls

Assuming that by "directoryname" you mean the
last subfolder name in the fully qualified path to
the file, and assuming that you mean the tilde (~)
characters are constant as the first and final characters
of the base name (excluding extension) of each file, try this:

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
FOR %%F IN ("C:\Path to\Your XLS files\*.XLS") DO CALL :_RENAME "%%F"
GOTO :EOF

:_RENAME
SET BN=%~n1
SET BN=%BN:~0,-1%
SET BN=%BN:~1%
SET STR=%1
:LOOP
FOR /f "tokens=1* delims=\" %%A IN (%STR%) DO (
IF NOT "%%B"=="" SET FLD=%%A
SET STR="%%B"
GOTO :LOOP
)
:: Remove ECHO/{demo} to activate REN command
ECHO/{demo}REN %1 "%BN% - %FLD%.*"

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

Replace the path specification:
"C:\Path to\Your XLS files\*.XLS"
with the correct path to your own XLS files (ensure you "quote"
any path specification that includes [Space]s), or pass it as a
parameter. Remove the ECHO/{demo} where shown to
activate the REN command when you are satisfied that
the demo does what you want.
 
Thanks for your answer !!

Actually ~ are real caracters.
And the real name of files are : ~xxxx~.xls

Do I have to change something to make your script work ?
Matthieu

William Allen said:
hi there,

I have some files in a directory.
files are named this way :
~xxxx~.xls

Ans I would like to rename the files as :
xxxx - directoryname.xls

Assuming that by "directoryname" you mean the
last subfolder name in the fully qualified path to
the file, and assuming that you mean the tilde (~)
characters are constant as the first and final characters
of the base name (excluding extension) of each file, try this:

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
FOR %%F IN ("C:\Path to\Your XLS files\*.XLS") DO CALL :_RENAME "%%F"
GOTO :EOF

:_RENAME
SET BN=%~n1
SET BN=%BN:~0,-1%
SET BN=%BN:~1%
SET STR=%1
:LOOP
FOR /f "tokens=1* delims=\" %%A IN (%STR%) DO (
IF NOT "%%B"=="" SET FLD=%%A
SET STR="%%B"
GOTO :LOOP
)
:: Remove ECHO/{demo} to activate REN command
ECHO/{demo}REN %1 "%BN% - %FLD%.*"

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

Replace the path specification:
"C:\Path to\Your XLS files\*.XLS"
with the correct path to your own XLS files (ensure you "quote"
any path specification that includes [Space]s), or pass it as a
parameter. Remove the ECHO/{demo} where shown to
activate the REN command when you are satisfied that
the demo does what you want.

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
Header email is rarely checked. Contact us at http://www.allenware.com/
 
Thanks for your answer !!

Actually ~ are real caracters.
And the real name of files are : ~xxxx~.xls

Do I have to change something to make your script work ?

The Batch file simply removes the first and last characters
of the base name, so your ~xxxx~ would be changed to xxxx
but since the demo is inactivated with ECHO/{demo} in front
of the REN command, you can simply try it and watch the
REN commands generated to see if they are what you want.

If the ECHOed REN commands appear to be what you want,
you must remove the ECHO/{demo} inactivating prefix where
shown to activate the REN commands.
 
Thanks a lot !!!

Matthieu
William Allen said:
in message

The Batch file simply removes the first and last characters
of the base name, so your ~xxxx~ would be changed to xxxx
but since the demo is inactivated with ECHO/{demo} in front
of the REN command, you can simply try it and watch the
REN commands generated to see if they are what you want.

If the ECHOed REN commands appear to be what you want,
you must remove the ECHO/{demo} inactivating prefix where
shown to activate the REN commands.

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
Header email is rarely checked. Contact us at http://www.allenware.com/
 
Back
Top