Computing.Net > Forums > Disk Operating System > Batch File Madness

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch File Madness

Reply to Message Icon

Name: Jen Cheng
Date: May 4, 2003 at 07:00:12 Pacific
OS: Win XP Pro SP1
CPU/Ram: P4 2.53 GHz / 512 MB DDR
Comment:

Okay, so I have a few questions regarding some issues I am having right now:

1) I have a huge list of WinRAR files that were originally named in the format:

A.B.C.D.part001.rar
A.B.C.D.part002.rar
.
.
.

I want to rename them to:

A.part001.rar
A.part002.rar
.
.
.

2) Now my other problem is in the same stream of thought. I have a bunch of files in the format:

A.r01
A.r02
.
.
.

Now I would like to rename them in the format:

A.001
A.002
.
.
.

BUT, I would like to do in a manner that will make .r01 become .002, .r03 become .004. Is this possible?

Thanks For the Help,
JC




Sponsored Link
Ads by Google

Response Number 1
Name: Secret_Doom
Date: May 4, 2003 at 10:04:53 Pacific
Reply:

That's all possible with batch scripting. You just didn't mention if all the files from each issue are in a same directory, or if they're just listed on a file, whith various paths, spread over the computer. I'll assume they're all on the same directory (if that doesn't apply, don't use the following batch files!)

This should solve the first issue:

@echo off
for %%F in (*.rar) do (
    for /F "tokens=1,5,6 delims=." %%S in ("%%F") do (
:: To activate script, remove the string
:: "echo.{demo}" from the next line
        echo.{demo}rename "%%F" "%%S"
    )
)

That script is de-activated. If you run it like that, it will just display the actions it would take if active. If those are the desired actions, activate the script (read commented lines for doing so).

And this other batch file should solve the second issue:

@echo off
if "%1"=="GoTo" goto process

for %%F in (*.r*) do (
    set file=%%F
    set name=%%~nF
    set ext=%%~xF
    call %0 GoTo process
)
goto eof

:process
set ext=%ext:~2%
for /F "delims=0123456789" %%C in ("%ext%") do goto eof
:loop1
if "%ext:~0,1%"=="0" (set ext=%ext:~1%&goto loop1)
set /A ext += 1
:loop2
if "%ext:~2,1%"=="" (set ext=0%ext%&goto loop2)
:: To activate script, remove the string
:: "echo.{demo}" from the next line
echo.{demo}rename "%file%" "%name%.%ext%"

:eof

That script is also de-activated.

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br

__________________________________________________________________


0

Response Number 2
Name: Jen Cheng
Date: May 4, 2003 at 12:18:21 Pacific
Reply:

Wow, much more complex than I thought. Out of my league. I will try them out, the next time I need them and let you know. After looking at your site, I thought I'd mention that I am running XP, will these commands still work? All of the files in question are in the same directory.

Taking it a step further, how would I make the first set of WinRAR files output in the second manner,

A.B.C.D.Part001.rar --> A.Part001.rar (earlier post)

Can I make this become:
A.B.C.D.Part001.rar --> A.001 (where the extension of the output is the part number of the first one.)

I really appreciate your help with this.

Thanks,
JC


0

Response Number 3
Name: Jen Cheng
Date: May 4, 2003 at 12:26:16 Pacific
Reply:

After testing the first one:

I get the error:
"The system cannot find the drive specified."

I counted it up and there isn't four parts to the file name, it's more like:
A.B.C.part001.rar

I thought maybe that could be the error.


0

Response Number 4
Name: Jen Cheng
Date: May 4, 2003 at 12:32:58 Pacific
Reply:

After running the second batch file I get the error:

{demo}rename "A.r06" "A.007"
{demo}rename "A.r07" "A.008"
{demo}rename "A.r08" "A.009"
The syntax of the command is incorrect.

The directory had .r00 through .r55 and the whole output was that error.

Thanks,
JC


0

Response Number 5
Name: Secret_Doom
Date: May 5, 2003 at 14:58:50 Pacific
Reply:

Jen Cheng wrote:
> After looking at your site, I thought I'd mention that
> I am running XP, will these commands still work?

I've developed the batch scripts based on the OS you reported (Windows XP Pro). In fact, those batch files will only work on NT systems (Windows NT/2000/XP), they will not work on DOS/Win9x.

from response #2:
> Can I make this become:
> A.B.C.D.Part001.rar --> A.001 (where the extension of the
> output is the part number of the first one.)

from response #3:
> I counted it up and there isn't four parts to the file name, it's more like:
> A.B.C.part001.rar

About the first script, it was written wrong in the first place. Making the necessary corrections and adaptations according to your last inputs about that from responses #2 and #3, this is how it gets:

@echo off
for %%F in (*.rar) do (
    for /F "tokens=1,4 delims=." %%A in ("%%F") do (
        set file=%%F
        set part0=%%A
        set part1=
        set part2=%%B
        if not "%%B"=="" call %0 GoTo process
    )
)
goto eof
:process
for /F "delims=0123456789 tokens=*" %%C in ("%part0:~0,1%!") do (
    if "%%C"=="!" set part1=%part1%%part0:~0,1%
)
set part0=%part0:~1%
if defined part0 goto process

:: To activate script, remove the string
:: "echo.{demo}" from the next line
echo.{demo}rename "%file%" "%part1%.%part2%"

:eof

However, the modifications I've made aren't related to that error you mentioned. I can't find any reason for that error to come up (so, it might come up again). You must cut-paste that code into a text file, save it with a .bat extention and run it from the folder where the files are. You've done that, right? You should not try to make any modifications on the script (appart from the ones described on the commented lines) and report errors generated by altered versions of my original code.

And for the second script, I see no reason at all why it shouldn't work. It has been tested throughly on Windows XP Professional and it's working just fine.

Make sure you're not screwing up the codes when cut-pasting them.

If you did nothing wrong on the cut-pasting process, if the problem is in the codes, I don't know how to solve either of the issues, since both codes work just fine here. Can anybody see any errors on those scripts?

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br

_________________________________________________________________


0

Related Posts

See More



Response Number 6
Name: Secret_Doom
Date: May 5, 2003 at 15:05:04 Pacific
Reply:

Hey, I posted the wrong script once more. I'm sorry about that. This is the script for the first task mentioned, which will get a file like the following;

something.anything.whatever.characters001.rar

and rename it to;

something.001

And it will apply that process to all files *.rar from the current directory, whenever possible. Designed and tested under Windows XP Professional, won't work out of NT systems.

@echo off
if "%1"=="GoTo" goto %2
for %%F in (*.rar) do (
    for /F "tokens=1,4 delims=." %%A in ("%%F") do (
        set file=%%F
        set part0=%%B
        set part1=%%A
        set part2=
        if not "%%B"=="" call %0 GoTo process
    )
)
goto eof
:process
for /F "delims=0123456789 tokens=*" %%C in ("%part0:~0,1%!") do (
    if "%%C"=="!" set part2=%part2%%part0:~0,1%
)
set part0=%part0:~1%
if defined part0 goto process

:: To activate script, remove the string
:: "echo.{demo}" from the next line
echo.{demo}rename "%file%" "%part1%.%part2%"

:eof

The script for the second task remains (I don't see any errors).

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br

_________________________________________________________________


0

Response Number 7
Name: Jen Cheng
Date: May 5, 2003 at 16:09:19 Pacific
Reply:

Wonderful the first method works:

A.B.C.D.Part001.rar --> A.001

I think I may have told you wrong in the first post, that's why it's not working for the second method. Here it is in more detail:

A.rar --> A.001
A.r00 --> A.002
A.r01 --> A.003
A.r02 --> A.004

In the first post I did not include A.r00. Does that change the coding around?


0

Response Number 8
Name: Secret_Doom
Date: May 6, 2003 at 18:41:34 Pacific
Reply:

Jen Cheng wrote:
> Wonderful the first method works:
> A.B.C.D.Part001.rar --> A.001

You mean "A.B.C.Part001.rar", right? Good that it's working.

As for the second script, I might have found the reason for that error message you've mentioned. I would correct the bug, but now I've been confused by last message:

> I think I may have told you wrong in the
> first post, that's why it's not working
> for the second method. Here it is in more
> detail:
>
> A.rar --> A.001
> A.r00 --> A.002
> A.r01 --> A.003
> A.r02 --> A.004
>
> In the first post I did not include A.r00.
> Does that change the coding around?

A.r00 becomes A.002? From what you first described, it would actually become A.001 (add 1 to the number, not two). You hadn't mentioned A.rar either. So, what is the process you want?

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br


0

Response Number 9
Name: Jen Cheng
Date: May 7, 2003 at 12:49:10 Pacific
Reply:

My first post was wrong, what I was trying to type was:

A.rar --> A.001
A.r00 --> A.002
A.r01 --> A.003
A.r02 --> A.004

and yes I meant to say A.B.C instead of A.B.C.D

Thanks,
JC


0

Response Number 10
Name: Secret_Doom
Date: May 7, 2003 at 15:59:06 Pacific
Reply:

Oh, it's clearer now. Try this batch file for that task:

@echo off
if "%1"=="GoTo" goto process

for %%F in (*.r*) do (
    set file=%%F
    set name=%%~nF
    set ext=%%~xF
    call %0 GoTo process
)
goto eof

:process
if "%ext%"==".rar" (set ext=001&goto makeren)
set ext=%ext:~2%
if not defined ext goto eof
for /F "delims=0123456789" %%C in ("%ext%") do goto eof
:loop1
if "%ext:~0,1%"=="0" (set ext=%ext:~1%&goto loop1)
set /A ext += 2
:loop2
if "%ext:~2,1%"=="" (set ext=0%ext%&goto loop2)
:makeren
:: To activate script, remove the string
:: "echo.{demo}" from the next line
echo.{demo}rename "%file%" "%name%.%ext%"

:eof

That script is de-activated. If you run it like that, it will just display the actions it would take if active. If those are the desired actions, activate the script (read commented lines for doing so).

Tell me the results, ok?

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br

__________________________________________________________________


0

Response Number 11
Name: Jen Cheng
Date: May 7, 2003 at 19:00:58 Pacific
Reply:

I get the following error:
The syntax of the command is incorrect.


0

Response Number 12
Name: Secret_Doom
Date: May 8, 2003 at 16:29:08 Pacific
Reply:

Hmmm... That's probably due to a difference between the Windows 2000 environment and the Windows XP's (which are very few), since the script still works on my system, Windows XP. Try this other version:

@echo off
if "%1"=="GoTo" goto process

setlocal
for %%F in (*.r*) do (
    set file=%%F
    set name=%%~nF
    set ext=%%~xF
    call %0 GoTo process
)
endlocal
goto eof

:process
if "%ext%"==".rar" (set ext=001&goto makeren)
set ext=%ext:~2%
if not defined ext goto eof
set NaN=
for /F "tokens=* delims=0123456789" %%C in ("%ext%") do set NaN=%%C
if defined NaN goto eof
:loop1
if "%ext:~0,1%"=="0" (set ext=%ext:~1%&goto loop1)
set /A ext += 2
:loop2
if "%ext:~2,1%"=="" (set ext=0%ext%&goto loop2)
:makeren
:: To activate script, remove the string
:: "echo.{demo}" from the next line
echo.{demo}rename "%file%" "%name%.%ext%"

:eof

That script is de-activated. If you run it like that, it will just display the actions it would take if active. If those are the desired actions, activate the script (read commented lines for doing so).

Tell me the results, ok?

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br

_______________________________________________________________________


0

Response Number 13
Name: Jen Cheng
Date: May 9, 2003 at 15:03:41 Pacific
Reply:

Getting the same error with both batch files.


0

Response Number 14
Name: Secret_Doom
Date: May 10, 2003 at 21:32:17 Pacific
Reply:

I don't know what's wrong, and it's taking me a while to find out. Try asking on the newsgroup alt.msdos.batch.nt.

It might be useful to include that script I wrote on the message.

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br


0

Sponsored Link
Ads by Google
Reply to Message Icon

ideas for .bat files Setup DOSes with no SETUP...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Disk Operating System Forum Home


Sponsored links

Ads by Google


Results for: Batch File Madness

Help!! Batch File www.computing.net/answers/dos/help-batch-file/13525.html

Batch files - - Mapping network drive www.computing.net/answers/dos/batch-files-mapping-network-drive/3809.html

Pause batch file www.computing.net/answers/dos/pause-batch-file/2810.html