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

DOS Prompt

26 views
Skip to first unread message

digdug

unread,
May 28, 2008, 3:18:00 PM5/28/08
to
I am trying to write a batch file to strip the leading characters from
the name of all files in a directory - leaving the second part of the file
name.
I have tried using 'rename' but cannot get it to work.

Pegasus (MVP)

unread,
May 28, 2008, 4:02:05 PM5/28/08
to

"digdug" <dig...@discussions.microsoft.com> wrote in message
news:321950DC-0A25-4503...@microsoft.com...

Can you show an example?


Todd Vargo

unread,
May 28, 2008, 5:59:07 PM5/28/08
to

Unfortunately, we can not explain what the problem is if you do not show
what you tried.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Herb Martin

unread,
May 28, 2008, 6:10:59 PM5/28/08
to

"digdug" <dig...@discussions.microsoft.com> wrote in message
news:321950DC-0A25-4503...@microsoft.com...

What Pegasus said, it is best to show an example what you have
and what you want.

I am pretty good with the command prompt batch language but
there are others who can pretty much anything.

For me it often easier and quick to either introduce UNIX-like
tooks such as the UnxTools for Win32 (say "cut" or "sed") or
just switch to Perl to get the job done.

UnxTools for Win32 are here:
http://sourceforge.net/project/showfiles.php?group_id=9328

Of course the problem with this is adding software to the machine
but I tend to do this for all our machines, along with Perl.

On the other hand if there exist some specific character(s) that separate
your current names you might be able to play with ForInDo loops
and the "delimiter" characters.

Also useful sometimes is to just dump the output (or use a for loop)
into a text file, use a decent editor (e.g., Notepad++) with either
column cut and paste or with Regular Expressions.

You can of course use VBScript or the newer Powershell but
these are far more awkward than the traditional Unix tools
and languages.


foxidrive

unread,
May 29, 2008, 1:54:48 AM5/29/08
to

I use this - it uses Gnu SED

@echo off
if %2.==. echo Remove Leading Characters from filenames.
if %2.==. echo.
if %2.==. echo. Syntax: %0 filespec.ext 5
if %2.==. echo.
if %2.==. echo. where it will remove 5 characters from the
if %2.==. echo. beginning of each filename in the filespec
if %2.==. echo.
if %2.==. echo. ENSURE THAT ALL FILENAMES EXCEED x CHARACTERS!
if %2.==. goto :end
set t="%temp%.\rlc.bat"
echo. @echo off>%t%
echo echo renaming files...>>%t%
dir %1 /b/on|sed "s/^.\{%2\}\(.*\)$/ren \x22&\x22 \x22\1\x22/">>%t%
call %t%
:end

Herb Martin

unread,
May 29, 2008, 2:57:54 PM5/29/08
to

"foxidrive" <got...@woohoo.invalid> wrote in message
news:u7hs34d2g5bo7ton5...@4ax.com...

More complete answer than mine. <nice>

FYI: Sed means "Script EDitor".

There a version of SED (and many other tools) in those UnxTools
I mentioned:

http://sourceforge.net/project/showfiles.php?group_id=9328

Timo Salmi

unread,
May 31, 2008, 7:05:11 PM5/31/08
to
Herb Martin <ne...@learnquick.com> wrote:
> FYI: Sed means "Script EDitor".
> There a version of SED (and many other tools) in those UnxTools
> I mentioned:
> http://sourceforge.net/project/showfiles.php?group_id=9328

The version updating many of the UnxTools (including SED) is
http://garbo.uwasa.fi/pub/win95/unix/UnxUpdates.zip

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.htm

Herb Martin

unread,
Jun 1, 2008, 12:37:55 AM6/1/08
to

"Timo Salmi" <t...@uwasa.fi> wrote in message
news:OAV9IL3...@TK2MSFTNGP05.phx.gbl...

> Herb Martin <ne...@learnquick.com> wrote:
>> FYI: Sed means "Script EDitor".
>> There a version of SED (and many other tools) in those UnxTools
>> I mentioned:
>> http://sourceforge.net/project/showfiles.php?group_id=9328
>
> The version updating many of the UnxTools (including SED) is
> http://garbo.uwasa.fi/pub/win95/unix/UnxUpdates.zip
>
> All the best, Timo

Thank you -- very much.

Always love new tools.


Harlan Grove

unread,
Jun 1, 2008, 1:26:06 AM6/1/08
to
foxidrive <got...@woohoo.invalid> wrote...
...

>I use this - it uses Gnu SED
>
>@echo off
>if %2.==. echo Remove Leading Characters from filenames.
>if %2.==. echo.
>if %2.==. echo. Syntax: %0 filespec.ext 5
>if %2.==. echo.
>if %2.==. echo. where it will remove 5 characters from the
>if %2.==. echo. beginning of each filename in the filespec
>if %2.==. echo.
>if %2.==. echo. ENSURE THAT ALL FILENAMES EXCEED x CHARACTERS!
>if %2.==. goto :end
>set t="%temp%.\rlc.bat"
>echo. @echo off>%t%
>echo echo renaming files...>>%t%
>dir %1 /b/on|sed "s/^.\{%2\}\(.*\)$/ren \x22&\x22 \x22\1\x22/">>%t%
>call %t%
>:end

Why do you need sed?


@REM ---- begin batch file ----
@setlocal enableextensions
@echo off

if not "%1" == "" if not "%2" == "" goto :BEGIN
echo usage: %0 filemask number

:BEGIN
REM not checking that %2 is a positive integer
set nc=%2

for %%f in (%1) do call :PROC %%~ff
goto :EOF

:PROC
set fn=%~n1
call set fn=\%%fn:~%nc%%%

if %fn% == \ (
echo/can't rename %1
) else (
REM delete echo in next line to rename files
echo ren "%1" "%~d1%~p1%fn:~1%%~x1"
)
@REM ---- end batch file ----

foxidrive

unread,
Jun 1, 2008, 1:38:06 AM6/1/08
to
On Sat, 31 May 2008 22:26:06 -0700 (PDT), Harlan Grove <hrl...@gmail.com>
wrote:

>Why do you need sed?

It was written before XP. :)

You'll have to clean that up to handle an & Harlan.

BTW, %%~ff ?

Harlan Grove

unread,
Jun 1, 2008, 1:39:01 AM6/1/08
to
Timo Salmi <t...@uwasa.fi> wrote...
...

>The version updating many of the UnxTools (including SED) is
>http://garbo.uwasa.fi/pub/win95/unix/UnxUpdates.zip

If these are updates of GNU software, where's the source code? There
are no source code files in this zip file nor any README file with
urls for the source code files. Are you sure you're complying with GNU
licensing terms?

Also, while the EXE files in this zip file may be updates of the files
in UnxUtils.zip, they're still 5 years old, so only slightly less out
of date. For example, the gawk version in UnxUpdates.zip is 3.1.3
whereas the current version is 3.1.6.

Better to use either GnuWin32 or the DJGPP ports. Both are more
actively maintained.

Harlan Grove

unread,
Jun 1, 2008, 1:41:55 AM6/1/08
to
foxidrive <got...@woohoo.invalid> wrote...
...

>You'll have to clean that up to handle an & Harlan.

Nah. If I really need to do something like this, I'd use zsh.

>BTW, %%~ff ?

Full pathnames, but probably unnecessary.

Harlan Grove

unread,
Jun 1, 2008, 2:36:51 AM6/1/08
to
Harlan Grove <hrln...@gmail.com> wrote...

>foxidrive <got...@woohoo.invalid> wrote...
>
>...
>
>>You'll have to clean that up to handle an & Harlan.
>
>Nah. If I really need to do something like this, I'd use zsh.
...

Then again, why not?


@REM ---- begin batch file ----
@setlocal enableextensions
@echo off

if not "%~1" == "" if not "%~2" == "" goto :BEGIN


echo usage: %0 filemask number

:BEGIN
REM not checking that %2 is a positive integer
set nc=%2

for %%f in (%1) do call :PROC "%%~ff"
goto :EOF

:PROC
set fn="%~n1"
set fn=%fn:&=:%
set fn=%fn:"=%
call set fn=%%fn:~%nc%%%

if "%fn%" == "" (
echo/can't rename "%~1"


) else (
REM delete echo in next line to rename files

echo ren "%~1" "%~d1%~p1%fn::=&%%~x1"

foxidrive

unread,
Jun 1, 2008, 4:24:57 AM6/1/08
to
On Sat, 31 May 2008 23:36:51 -0700 (PDT), Harlan Grove <hrl...@gmail.com>
wrote:

It ain't gunna work. REN doesn't support paths in the target name.

>@REM ---- begin batch file ----
>@setlocal enableextensions
>@echo off
>
>if not "%~1" == "" if not "%~2" == "" goto :BEGIN
>echo usage: %0 filemask number

I assume there's an invisible goto :EOF here. :)

>:BEGIN
>REM not checking that %2 is a positive integer
>set nc=%2
>
>for %%f in (%1) do call :PROC "%%~ff"
>goto :EOF
>
>:PROC
>set fn="%~n1"
>set fn=%fn:&=:%
>set fn=%fn:"=%
>call set fn=%%fn:~%nc%%%
>
>if "%fn%" == "" (
> echo/can't rename "%~1"
>) else (
> REM delete echo in next line to rename files
> echo ren "%~1" "%~d1%~p1%fn::=&%%~x1"
>)
>@REM ---- end batch file ----

This'll do & characters too.


:PROC
set "fn=%~n1"
call set "fn=%%fn:~%nc%%%"

Timo Salmi

unread,
Jun 1, 2008, 5:29:18 AM6/1/08
to
foxidrive <got...@woohoo.invalid> wrote:
> It ain't gunna work. REN doesn't support paths in the target name.

Right. Even the REN /? documentation warns about it:

"Note that you cannot specify a new drive or path for your destination
file"

But it should be easily remedied by substituting with MOVE.

Stefan Kanthak

unread,
Jun 1, 2008, 10:18:01 AM6/1/08
to
"Harlan Grove" <hrl...@gmail.com> wrote:

> foxidrive <got...@woohoo.invalid> wrote...

[ strip leading blanks ]

> Why do you need sed?
>
>
> @REM ---- begin batch file ----
> @setlocal enableextensions
> @echo off
>
> if not "%1" == "" if not "%2" == "" goto :BEGIN
> echo usage: %0 filemask number
>
> :BEGIN
> REM not checking that %2 is a positive integer
> set nc=%2
>
> for %%f in (%1) do call :PROC %%~ff

Are you sure that the parameter passed to :PROC is correct?
%1 has to be enclosed in quotes if it has ANY spaces in it;
and there are leading spaces, thats the precondition.

~f expands %1 to the full filename, so :PROC may be called
with an arbitrary number of parameters.
At least %%~ff has to be enclosed in quotes too...

> goto :EOF
>
> :PROC
> set fn=%~n1

... which get stripped again here.

Stefan

Harlan Grove

unread,
Jun 1, 2008, 9:53:26 PM6/1/08
to
foxidrive <got...@woohoo.invalid> wrote...

>Harlan Grove <hrln...@gmail.com> wrote:
>It ain't gunna work. REN doesn't support paths in the target name.
...

Oops.

>>@REM ---- begin batch file ----
>>@setlocal enableextensions
>>@echo off
>
>>if not "%~1" == "" if not "%~2" == "" goto :BEGIN
>>echo usage: %0 filemask number
>
>I assume there's an invisible goto :EOF here. :)

Another oops.

>This'll do & characters too.
>
>:PROC
>set "fn=%~n1"
>call set "fn=%%fn:~%nc%%%"

Much better.

Herb Martin

unread,
Jun 2, 2008, 4:14:12 PM6/2/08
to

"Harlan Grove" <hrl...@gmail.com> wrote in message
news:752f870b-e3bf-412d...@k13g2000hse.googlegroups.com...

> Timo Salmi <t...@uwasa.fi> wrote...
> ...
>>The version updating many of the UnxTools (including SED) is
>>http://garbo.uwasa.fi/pub/win95/unix/UnxUpdates.zip
>
> If these are updates of GNU software, where's the source code? There
> are no source code files in this zip file nor any README file with
> urls for the source code files. Are you sure you're complying with GNU
> licensing terms?

They were the same versions (comp old new) as my versions from
SourceForge (I believe.)

Source for those is there at SourceForge.

Timo Salmi

unread,
Jun 2, 2008, 6:13:27 PM6/2/08
to
Harlan Grove <> wrote:
> Timo Salmi <t...@uwasa.fi> wrote...

>> The version updating many of the UnxTools (including SED) is
>> http://garbo.uwasa.fi/pub/win95/unix/UnxUpdates.zip

> Are you sure you're complying with GNU licensing terms?

Me? If you find that a major concern, your question should best be posed
to the originator of the package.

Harlan Grove

unread,
Jun 2, 2008, 10:28:22 PM6/2/08
to
Timo Salmi <t...@uwasa.fi> wrote...

>Harlan Grove <> wrote:
>>Timo Salmi <t...@uwasa.fi> wrote...
>>>The version updating many of the UnxTools (including SED) is
>>>http://garbo.uwasa.fi/pub/win95/unix/UnxUpdates.zip
>>Are you sure you're complying with GNU licensing terms?
>
>Me? If you find that a major concern, your question should best be posed
>to the originator of the package.

I won't go through all the EXE files in your UnxUpdates.zip file. I'll
focus on gawk.exe. That's a dated version of GNU awk. It's definitely
GNU software.

You may not have created the zip file, but you ARE redistributing it.
If the originator of the package wasn't complying with the
redistribution terms of the GPL, neither are you. There's no
information whatsoever in the zip file identifying who the originator
may be. Regardless, your redistribution of the zip file doesn't comply
with the GPL. You aren't the copyright holder, so you don't get to
decide to ignore the licensing terms.

>gawk --version
GNU Awk 3.1.3
Copyright (C) 1989, 1991-2003 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

<remainder of screen display snipped>

Under the GPL version 2's Terms and Conditions section 3,

3. You may copy and distribute the Program (or a work based on it,
under
Section 2) in object code or executable form under the terms of
Sections
1 and 2 above provided that you also do one of the following:

a) Accompany it with the complete corresponding machine-readable
source
code, which must be distributed under the terms of Sections 1 and 2
above
on a medium customarily used for software interchange; or,

b) Accompany it with a written offer, valid for at least three
years,
to give any third party, for a charge no more than your cost of
physically
performing source distribution, a complete machine-readable copy of
the
corresponding source code, to be distributed under the terms of
Sections
1 and 2 above on a medium customarily used for software interchange;
or,

c) Accompany it with the information you received as to the offer to
distribute corresponding source code. (This alternative is allowed
only
for noncommercial distribution and only if you received the program in
object code or executable form with such an offer, in accord with
Subsection b above.)

The zip file in question fails on all 3 conditions: source code files
don't accompany the EXE files; there's no written offer (you could
probably satisfy the second condition by including a README file); and
if you're redistributing this zip file without modification, then you
didn't receive any information as to the offer to distribute
corresponding source code. The last condition means that while you may
RECEIVE noncomplying binary distributions, you can't (in the legal/
moral sense) turn around and REDISTRIBUTE them yourself.

So it seems the person or site from which you received this file
wasn't complying with the terms of the GPL for this GPL'ed software.
By redistributing it as-is, you are also failing to comply with the
software's license terms and conditions. Consider at least adding a
README file pointing people to the source code.

Timo Salmi

unread,
Jun 3, 2008, 4:16:18 AM6/3/08
to
Dear Harlan,

Thank you for your vigilance. I had missed the situation and I agree.
Licenses are licenses. And besides I have neither the time nor the
interest to delve into the legalities and the question who is
responsible for the obviously stripped package. I'll just take my cues
from your finding, since I do not wish to be involved with the
distribution of any clearly controversial material. Thus I am
immediately withdrawing the following two packages from the public
distribution at Garbo

http://garbo.uwasa.fi/pub/win95/unix/UnxUtils.zip
http://garbo.uwasa.fi/pub/win95/unix/UnxUpdates.zip

The problem, incidentally, with some of the more recent GNU ports, is
that they require a separate installation since a number of the
utilities are dependent on non-default dll:s. That's why the above
collections would have been useful. They do not require any
installation, just the unzipping. But I am confident that the gentle
users will themselves find proper original or replacement utilities.

As for the good suggestion of adding a README to the packages, I do not
take that route. I manipulate the contents of only such packages where
the copyright belongs to me.

P.S. My apologies for the the deliberate, one-time top-posting. The
reason for this exception is that I'll be using the Google reference to
this posting as a note of what happened to which utilities at the Garbo
archives.

All the best, Timo

Harlan Grove <hrl...@gmail.com> wrote:
> Timo Salmi <t...@uwasa.fi> wrote...

--

Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland

Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

0 new messages