Computing.Net > Forums > Disk Operating System > Renaming files in DOS

Renaming files in DOS

Reply to Message Icon

Original Message
Name: john
Date: March 14, 2003 at 14:23:16 Pacific
Subject: Renaming files in DOS
OS: Windows 98/2000
CPU/Ram: 64MB
Comment:

How do I create a batch file that would rename files from a directory based on this format: 12345.67a (from original file of 2001234567-A1.txt) - the 1st 3 characters, the last character, and extension were dropped. Comprising the new filename: 4th-8th character. Extension is 9th-11th char).


Report Offensive Message For Removal


Response Number 1
Name: mark
Date: March 14, 2003 at 18:43:35 Pacific
Subject: Renaming files in DOS
Reply: (edit)

You cannot handle a filename like that in Dos. It would have to be done in windows. Dos is limited to an 8 character filename.


Report Offensive Follow Up For Removal

Response Number 2
Name: Secret_Doom
Date: March 14, 2003 at 21:41:18 Pacific
Subject: Renaming files in DOS
Reply: (edit)

Mark wrote:
> You cannot handle a filename like that in Dos.

And who mentioned DOS? John stated his OS as Windows 98/2000, so no problem!

John, it's much easier to do it on Win2000, so I'll show a solution for that OS. If you need it to work on both Win 2000 and 98, I can make it, but it takes more work.

I see that you tryed to be specific on the way the filename should be changed. That's very good, I need a criterion, a certain rule to work with. However, you've contradicted yourself. Look at the example:

2001234567-A1.txt -> 12345.67a

The 3rd character before the end ('-') of the filename got dropped too. Besides, later on the post you state another rule:

"Comprising the new filename: 4th-8th character. Extension is 9th-11th char"

Even disregarding the '-' character issue I mentioned above, that rule's different from the previous one. Getting the 4th-8th characters from a string is different then just dropping the first 3 characters, if the string has 9 or more characters. Anyway, here's the rule I assumed should apply to your needs:

To determine new filename and extention, get original filename, drop extention, exclude any "-" characters on the counting. Then, drop the first 3 characters and 4 characters before the end to find the new filename. The extention will be the 4th-2th characters before the end. Example:

200123456789-C1.fz -> 1234567.89C

Does that rule apply to your needs? If so, the following batch script will do the job, on Win 2000:

@echo off
if "%1"=="GoTo" goto %2
if not "%OS%"=="Windows_NT" goto eof
%comspec% /v:on /c %0 GoTo start
goto eof

:start
echo.
echo This program will rename all files from the current directory,
echo and possibly from its subdirectories, following a certain rule.
echo.
echo Press any key to VIEW the renaming process . . .
pause > nul
echo Processing . . .

type nul> %temp%.\process.bat
:: To include the files under subdirectories from current
:: directory on the renaming process, add the string ' /R'
:: after the string 'for' on the next line, so it becomes
:: 'for /R' (don't forget the space between the strings)
for %%F in (*.*) do (
    set ORIG=%%~nF
    set ORIG=!ORIG:-=!
    set FILE=!ORIG:~3,-4!
    set EXT=!ORIG:~-4,-1!
    if not "!FILE!"=="" if not "!EXT!"=="" (
        echo REN "%%F" "!FILE!.!EXT!">> %temp%.\process.bat
    )
)

echo.
type %temp%.\process.bat |MORE
echo The above process is NOT reversible.
echo.
set CH=
:loop
set /P CH=To you wish to execute the above process [Y/N]?
if /i "%CH%"=="Y" (
call %temp%.\process.bat
echo.
echo Operation done.
del %temp%.\process.bat
goto eof
)
if /i "%CH%"=="N" (
echo.
echo Operation aborted
del %temp%.\process.bat
goto eof
)
goto loop

:eof

The script will rename all files from the current directory (the dir you are when you run it). It will not rename the files under subdirectories. There are instructions on the source saying how to include the subdirs. The filenames which don't have enough characters to fulfill the rule and yet have at least one chacter on the filename and one other on the extention will be excluded from the process.

-- Leonardo Pignataro - Secret_Doom --

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

____________________________________________________________________________


Report Offensive Follow Up For Removal

Response Number 3
Name: mark
Date: March 15, 2003 at 00:30:45 Pacific
Subject: Renaming files in DOS
Reply: (edit)

"Mark wrote:
> You cannot handle a filename like that in Dos."

"And who mentioned DOS? "

Is this the DOS forum?


Report Offensive Follow Up For Removal

Response Number 4
Name: x86
Date: March 15, 2003 at 01:58:20 Pacific
Subject: Renaming files in DOS
Reply: (edit)

Yes agree it would be nice if PURE DOS users would post on this forum. Windows DOS and NT Command Prompt is sort of DOS but acts differently. I was wondering what all the other foruns were for, maybe we should post in a different forum??????????


Report Offensive Follow Up For Removal

Response Number 5
Name: Miskva
Date: March 15, 2003 at 07:03:23 Pacific
Subject: Renaming files in DOS
Reply: (edit)

Mark, this is a DOS forum, and you have noticed that modern day Windows also run DOS, not the same of course, but they do run it


Report Offensive Follow Up For Removal


Response Number 6
Name: Secret_Doom
Date: March 15, 2003 at 07:45:38 Pacific
Subject: Renaming files in DOS
Reply: (edit)

I think that batch scripting under Win9x is absolutely on-topic for this forum. Though the behavior of the command prompt and so of the batch scripts is a little bit different if you're inside or outside Windows, it's Dos 7.xx after all, even inside Windows (let's please not enter in the pointless discussion if Windows 98 runs on top of DOS or not).

Now, about NT batch scripting, well... There is no DOS inside NT systems at all, that's for sure. However, though the behavior of the command prompt (cmd.exe) is quite different from DOS's command prompt (command.com), it's still batch scripting when it comes to it.

On top of that all, I think we should not stick to what's fundamentally on-topic or not. The goal here is to help people. Most NT users think they're on DOS just because it regards the command prompt, and so they come to this forum. Many people with DOS knowledge are able to help these NT users with the command prompt, since their knowledge is not limited do DOS/Win9x (that's my case). That is even more valid for batch scripting. So, it's actually convenient that such messages are posted here, since accurate answers will come.

Besides all that, when NT-command-prompt-related questions are not posted here, they get split up among the Win NT,2K and XP forums, and the difference between such OS's when it comes to all this command-prompt-batch-scripting thing is very very little.

I hang on this forum and answer all the batch scripting-related threads that I can, no matter the OS. I will not stop doing that, nor will instruct the users I help in that field to post further questions on other forums. Less work for me and for other batch guys, that have to check less forums.

What about the script, John? Did it work as intended?

-- Leonardo Pignataro - Secret_Doom --

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


Report Offensive Follow Up For Removal

Response Number 7
Name: johndacs
Date: March 15, 2003 at 15:20:11 Pacific
Subject: Renaming files in DOS
Reply: (edit)

yes, yes. this is exactly the script that i'm looking for.

thanks leonardo/a.k.a secret doom!

regards,

john


Report Offensive Follow Up For Removal

Response Number 8
Name: Miskva
Date: March 16, 2003 at 02:37:47 Pacific
Subject: Renaming files in DOS
Reply: (edit)

as said before, this is a DOS forum, intended to help people problems with DOS

and DOS runs under different operating system; amongst others: Windows 3.1, windows 95/98, windows NT, windows 2000 .. and maybe Windows XP

if you wanne bitch about the fact DOS is not the same on those DIFFERENT operating systems, you are absolutely right, and you are also off-topic as long as you dont mention WHAT exactly is different in the code

saying Windows NT hasnt got DOS is a lie, only wankers and noobs make such statements


Report Offensive Follow Up For Removal

Response Number 9
Name: Secret_Doom
Date: March 16, 2003 at 09:34:07 Pacific
Subject: Renaming files in DOS
Reply: (edit)

Miskva, I don't agree. It's not exately like that. You probably continue to state that there IS DOS under NT systems because you're able to open some kind of DOS-BOX on your Windows 2000, so what are these people saying? Are they blind, dumb?

However, what we're saying is that "DOS-BOX" is not DOS. It's just a command interpreter.

Let's take another angle of view: DOS is an entire OS, with all booting and file accessing thing, among other aspects. And it has a command intepreter, which happens to be its main (only) interface. That command interpreter is COMMAND.COM.

NT systems are totally different OS's, which don't even use DOS for booting, as Win9x does. Windows' main interface is GUI, the famous 'desktop'. But even on NT systems, there's another interface, which is command-line based. That's the "DOS-BOX" I mentioned. Just another way for the OS to interact with the user. But no sign of that OPERATING SYSTEM named DOS.

Even if microsoft sometimes names this command line based interface as "MS-DOS environment", it's not exately that.

That's what we're saying.

-- Leonardo Pignataro - Secret_Doom --

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


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Renaming files in DOS

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge