Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsWindows Server 2003Windows 2000Windows NTSmall Business ServerVirtual ServerExchange ServerIISHost Integration ServerISA ServerSMSWSUSMOMWindows Media ServerSecurityCertification
Related Topics
SQL ServerMS WindowsMS OfficePC HardwareMore Topics ...

Windows Server Forum / Windows 2000 / Command Prompt / June 2008

Tip: Looking for answers? Try searching our database.

DOS Prompt

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
digdug - 28 May 2008 20:18 GMT
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) - 28 May 2008 21:02 GMT
>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.

Can you show an example?
Todd Vargo - 28 May 2008 22:59 GMT
> 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.

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

Signature

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

Herb Martin - 28 May 2008 23:10 GMT
>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.

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 - 29 May 2008 06:54 GMT
>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.

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 - 29 May 2008 19:57 GMT
>>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
[quoted text clipped - 19 lines]
> call %t%
> :end

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 - 01 Jun 2008 00:05 GMT
> 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

Signature

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

Herb Martin - 01 Jun 2008 05:37 GMT
>> FYI:  Sed means "Script EDitor".
>> There a version of SED (and many other tools) in those UnxTools
[quoted text clipped - 5 lines]
>
>    All the best, Timo

Thank you -- very much.

Always love new tools.
Harlan Grove - 01 Jun 2008 06:39 GMT
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.
Herb Martin - 02 Jun 2008 21:14 GMT
> Timo Salmi <t...@uwasa.fi> wrote...
> ...
[quoted text clipped - 5 lines]
> 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.

> 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
[quoted text clipped - 3 lines]
> Better to use either GnuWin32 or the DJGPP ports. Both are more
> actively maintained.
Timo Salmi - 02 Jun 2008 23:13 GMT
> 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.

   All the best, Timo

Signature

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

Harlan Grove - 03 Jun 2008 03:28 GMT
Timo Salmi <t...@uwasa.fi> wrote...
>>Timo Salmi <t...@uwasa.fi> wrote...
>>>The version updating many of the UnxTools (including SED) is
[quoted text clipped - 3 lines]
>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 - 03 Jun 2008 09:16 GMT
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

> Timo Salmi <ts@uwasa.fi> wrote...
>>> Timo Salmi <ts@uwasa.fi> wrote...
[quoted text clipped - 73 lines]
> software's license terms and conditions. Consider at least adding a
> README file pointing people to the source code.

Signature

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

Harlan Grove - 01 Jun 2008 06:26 GMT
foxidrive <got...@woohoo.invalid> wrote...
...
>I use this - it uses Gnu SED
>
[quoted text clipped - 14 lines]
>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 - 01 Jun 2008 06:38 GMT
>Why do you need sed?

It was written before XP. :)

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

BTW, %%~ff ?

>for %%f in (%1) do call :PROC %%~ff
>goto :EOF
[quoted text clipped - 10 lines]
>)
>@REM ----  end batch file  ----
Harlan Grove - 01 Jun 2008 06:41 GMT
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 - 01 Jun 2008 07:36 GMT
Harlan Grove <hrln...@gmail.com> wrote...
>foxidrive <got...@woohoo.invalid> wrote...
>
[quoted text clipped - 3 lines]
>
>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"
)
@REM ----  end batch file  ----
foxidrive - 01 Jun 2008 09:24 GMT
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
[quoted text clipped - 16 lines]
>)
>@REM ----  end batch file  ----

This'll do & characters too.

:PROC
set "fn=%~n1"
call set "fn=%%fn:~%nc%%%"
Timo Salmi - 01 Jun 2008 10:29 GMT
> 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.

   All the best, Timo

Signature

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

Harlan Grove - 02 Jun 2008 02:53 GMT
foxidrive <got...@woohoo.invalid> wrote...
>It ain't gunna work.  REN doesn't support paths in the target name.
...

Oops.

>>@REM ---- begin batch file ----
>>@setlocal enableextensions
[quoted text clipped - 4 lines]
>
>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.
Stefan Kanthak - 01 Jun 2008 15:18 GMT
> foxidrive <got...@woohoo.invalid> wrote...

[ strip leading blanks ]

> Why do you need sed?
>
[quoted text clipped - 10 lines]
>
> 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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.