Solved

Batch script to remove prefix from a file?

Posted on 2004-04-16
3
2,040 Views
Last Modified: 2009-07-29
Is it possible to use a batch script (for loop of somekind) to remove prefixes from a bunch files?

I have files named Project_<filename>.txt and I would like to remove the "Project_" prefix from them so I would have only <filename>.txt files.

I have a perl script that does this pretty neatly but I was wondering if it would be possible to do it by using just batch script.

This in windows 2000 enviroment.

-- Ville
0
Comment
Question by:Wilde79
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 65

Accepted Solution

by:
rockiroads earned 125 total points
ID: 10842910
I got something to get u going, but I cant find anything which takes a part of a string in a string (if u know what I mean)

the approach would be

dump the directory contents into a temporary textfile
then read this textfile and process each file

e.g.

dir/b project*.txt >tmp.txt

for /f "delims=" %%i in (tmp.txt) do echo File is %%is

if u can manage to figure out how to extract a string porting, then just could expand the above loop, use the COPY statement first (I say that 'cos you dont want to lose any work!!!)
if it works with COPY then use RENAME

0
 
LVL 65

Expert Comment

by:rockiroads
ID: 10843174
ok, I found this script, it reverses text, I wonder if you can use this, as an example to extract a portion of the filename


@ECHO OFF
:: Keep variables local
SETLOCAL

:: Check command line parameters
IF [%1]==[] GOTO Syntax
ECHO.%* | FIND "=" >NUL
IF NOT ERRORLEVEL 1 GOTO Syntax

:: Initialize variables
SET REVERSE=
SET INPUT=%*
:: Strip leading space in NT 4 only
VER | FIND "Windows NT" >NUL
IF NOT ERRORLEVEL 1 SET INPUT=%INPUT:~1%

:Loop
:: Continue till the input string's last character
IF NOT DEFINED INPUT GOTO Finish
:: Separate first character from input string
SET FIRSTCHAR=%INPUT:~0,1%
SET INPUT=%INPUT:~1%
:: Rebuild string in reverse order
SET REVERSE=%FIRSTCHAR%%REVERSE%
:: Next character
GOTO Loop

:Finish
ECHO.
:: Add leading space for Windows 2000 and later
VER | FIND "Windows NT" >NUL
IF ERRORLEVEL 1 (ECHO Input string   = %*) ELSE (ECHO Input string   =%*)
ECHO Reverse string = %REVERSE%
GOTO End

:Syntax
ECHO.
ECHO REVERSE.BAT,  Version 2.00 for Windows NT 4 / 2000
ECHO.
ECHO Usage:  REVERSE ^<string^>
ECHO.
ECHO ^<string^> cannot contain equal signs ("=")
ECHO.
ECHO Example:
ECHO REVERSE Any string
ECHO.
ECHO returns:
ECHO Input string   = Any string
ECHO Reverse string = gnirts ynA

:End
ENDLOCAL

0
 
LVL 65

Expert Comment

by:rockiroads
ID: 10843180
I suppose your main batch script and call this other batch script which strips the portion
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is about my first experience with programming Arduino.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question