Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Rename Files Problem

32 views
Skip to first unread message

novice

unread,
Jun 4, 2017, 5:53:38 PM6/4/17
to
If have a lot of files, what is named about these examples;

autoplay12_vs_chessman6_2017-05-01.pgn
autoplay12_vs_chessman6_2017-05-01_2.pgn
chessman6_vs_notlose72_2016-01-15_4.pgn
chessman6_vs_notlose72_2016-02-25_5.pgn
chessman6_vs_special6__2017-01-01.pgn
_apple5_vs_chessman6_2017-04-04.pgn

How to make batch, what rename these files like this?;

2017-05-01_autoplay12_vs_chessman6.pgn
2017-05-01_autoplay12_vs_chessman6_2.pgn
2016-01-15_chessman6_vs_notlose72_4.pgn
2016-02-25_chessman6_vs_notlose72_5.pgn
2017-01-01_chessman6_vs_special6_.pgn
2017-04-04__apple5_vs_chessman6.pgn

These files are chess games files. Players name could be 2-30 character long
and player name could include _-character(s) anywhere in the name (see the
last two cases).

Is it possible to make batch, what can handle this kind of renames?

-Novice



Herbert Kleebauer

unread,
Jun 5, 2017, 10:52:59 AM6/5/17
to
On 04.06.2017 23:53, novice wrote:

> autoplay12_vs_chessman6_2017-05-01.pgn
>
> How to make batch, what rename these files like this?;
>
> 2017-05-01_autoplay12_vs_chessman6.pgn

> Is it possible to make batch, what can handle this kind of renames?

If the "-" is only in the date part but not in the name
part of the file name, try this:



echo off
for /f %%i in ('dir /b *pgn') do call :sub %%i
goto :eof

:sub
set a=%1
set n=-1

:loop
set /a n=n+1
call set s=%%a:~%n%,1%%
if [%s%]==[] goto :eof
if not [%s%]==[-] goto :loop
set /a n=n-4
set /a i=n-1
set /a j=n+10
call set s=%%a:~%n%,10%%_%%a:~,%i%%%%%a:~%j%%%
echo ren %a% %s%



novice

unread,
Jun 5, 2017, 4:33:49 PM6/5/17
to
"Herbert Kleebauer" <kl...@unibwm.de> wrote in message
news:oh3r88$1kk4$1...@gioia.aioe.org...
Wau, it's magic to me, how this work, but it's work great! Thank you very
much! I understund basic batch and simple for-loops, but this is so clever.
Before I try this, I try to understund, how it's work, but I don't
understand it.. Second last row is something, what is too complicated to me;
(call set s=%%a:~%n%,10%%_%%a:~,%i%%%%%a:~%j%%%).

>If the "-" is only in the date part but not in the name part of the file
>name, try this:
I found some files, where player name include "-" character. I can handle
these cases manually, but I wonder, could batch also handle these cases,
after some modification?

Second problem in my files. Some of files is named like this;

Player1_vs_Player2_2010_05_05.pgn
Player2_vs_Player1_2011_06_06_2.pgn
Player2_vs_Player1_2011_06_06_3.pgn

I need a second batch, what rename these files like this;

Player1_vs_Player2_2010-05-05.pgn
Player2_vs_Player1_2011-06-06_2.pgn
Player2_vs_Player1_2011-06-06_3.pgn

-Novice


Herbert Kleebauer

unread,
Jun 5, 2017, 6:30:13 PM6/5/17
to
On 05.06.2017 22:33, novice wrote:

> Wau, it's magic to me, how this work, but it's work great! Thank you very
> much! I understund basic batch and simple for-loops, but this is so clever.
> Before I try this, I try to understund, how it's work, but I don't
> understand it.. Second last row is something, what is too complicated to me;
> (call set s=%%a:~%n%,10%%_%%a:~,%i%%%%%a:~%j%%%).

"call" just substitutes one level of %. If n=20, i=19 and j=30 then after
the call is executed you get:

set s=%a:~20,10%_%a:~,19%%a:~30%

Type "set /?" for an explanation of this command.

>>If the "-" is only in the date part but not in the name part of the file
>>name, try this:
> I found some files, where player name include "-" character. I can handle
> these cases manually, but I wonder, could batch also handle these cases,
> after some modification?

Then don't search for the first but for last "-" in the file name:

echo off
for /f %%i in ('dir /b *pgn') do call :sub %%i
goto :eof

:sub
set a=%1
set n=-1

:loop
set /a n=n+1
call set s=%%a:~%n%,1%%
if [%s%]==[] goto :end
if [%s%]==[-] set /a m=n
goto :loop
:end
set /a n=m-7
set /a i=n-1
set /a j=n+10
call set s=%%a:~%n%,10%%_%%a:~,%i%%%%%a:~%j%%%
echo ren %a% %s%



> Second problem in my files. Some of files is named like this;
>
> Player1_vs_Player2_2010_05_05.pgn
> Player2_vs_Player1_2011_06_06_2.pgn
> Player2_vs_Player1_2011_06_06_3.pgn
>
> I need a second batch, what rename these files like this;
>
> Player1_vs_Player2_2010-05-05.pgn
> Player2_vs_Player1_2011-06-06_2.pgn
> Player2_vs_Player1_2011-06-06_3.pgn

Separate these files from the others

mkdir a
mkdir b
move *_20??_??_??_* a\
move *_20??_??_??* b\

Then you can also use the above code, but don't search for
the last "-" but for the last "_".




novice

unread,
Jun 6, 2017, 3:29:01 PM6/6/17
to

"Herbert Kleebauer" <kl...@unibwm.de> wrote in message
news:oh4m18$12ro$1...@gioia.aioe.org...
Both new batch working great! Thank you very very much.

-Novice




0 new messages