Solved

Batch script to remove prefix from a file?

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now