Solved

Batch script to remove prefix from a file?

Posted on 2004-04-16
3
2,035 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
  • 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

861 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