?
Solved

Batch script to remove prefix from a file?

Posted on 2004-04-16
3
Medium Priority
?
2,057 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Progress

770 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