Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2075
  • Last Modified:

Batch script to remove prefix from a file?

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
Wilde79
Asked:
Wilde79
  • 3
1 Solution
 
rockiroadsCommented:
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
 
rockiroadsCommented:
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
 
rockiroadsCommented:
I suppose your main batch script and call this other batch script which strips the portion
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now