DOS Prompt

D

digdug

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.
 
P

Pegasus \(MVP\)

digdug said:
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?
 
T

Todd Vargo

digdug said:
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.
 
H

Herb Martin

digdug said:
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.
 
F

foxidrive

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
 
H

Herb Martin

foxidrive said:
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

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
 
H

Harlan Grove

foxidrive said:
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 ----
 
H

Harlan Grove

Timo Salmi said:
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.
 
H

Harlan Grove

Harlan Grove said:
...


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 ----
 
F

foxidrive

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%%%"
 
T

Timo Salmi

foxidrive said:
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
 
S

Stefan Kanthak

Harlan Grove said:
foxidrive <[email protected]> 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
 
H

Harlan Grove

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

Oops.



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.
 
H

Herb Martin

Harlan Grove said:
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.
 
T

Timo Salmi

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
 
H

Harlan Grove

Timo Salmi said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top