replacing text

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miltonsnider
Posts: 3
Joined: 29 Mar 2010 12:56

replacing text

#1 Post by miltonsnider » 29 Mar 2010 13:03

I would like to replace the .bat from test.bat with nothing I tried:
set mystr =test.bat:bat % but nothing is replaced. I am using windows 7 64bit. Any help is appreciated. What I am trying to do is take the %0 and remove the .bat from all my .bat files so everything is not hardcoded.

regards
Milton

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: replacing text

#2 Post by aGerman » 30 Mar 2010 10:48

I don't understand why you want to use %0 to rename all .bat files. %0 contains the full name of the own file. How ever, to do it for its own name you have to use this line

Code: Select all

ren "%~0" "%~n0"

otherwise (for all batches in a folder and sub folders)

Code: Select all

pushd "c:\your Folder" || goto :eof
for /f "delims=" %%a in ('dir /a-d /b /s *.bat') do  ren "%%~a" "%%~na"
popd


Regards
aGerman

miltonsnider
Posts: 3
Joined: 29 Mar 2010 12:56

Re: replacing text

#3 Post by miltonsnider » 30 Mar 2010 12:35

Thanks for your reply. I want to manipulate the text associated with the file name. Here is the entire bat file. I would like to replace the .bat in the filename string with nothing so that I can use it later in the bat file. I have many bat files and rather than recoding everytime I create a new bat file, I want the code to know the text to the left of .bat. Lets suppose the bat file is dopf.bat. I want to extract the dopf out of dopf.bat and use it as the backup directory as in the code below:
echo off
@FOR /F "tokens=1-4 delims=/ " %%I IN ('DATE /t') DO SET mydate=%%J.%%K.%%L
REM @FOR /F "tokens=1-4 delims=: " %%T IN ('time /t') DO SET mytime=%%T.%%U.%%V
@FOR /F "tokens=1-4 Delims=: " %%T IN ('time /t') DO SET mytime=%%T.%%U.%%V
SET MyDir=%mydate%\%mytime%
Set mystr = dopf

md e:\files\Backups\Websites\%mystr%\%MyDir%

xcopy a:\%mystr%\*.* e:\files\Backups\Websites\%mystr%\%MyDir%\*.* /S/Y

regards
Milton

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: replacing text

#4 Post by avery_larry » 31 Mar 2010 11:02

If you're always talking about the batch file that is running, then use "%~n0". That returns the filename without the extension of the batch file that is currently running.

Otherwise there are several ways to do that, but I would continue with the above theme by using a call statement:


Code: Select all

@echo off
set "filetoworkwith=c:\path to file\mybatch.bat"
call :getfilename "%filetoworkwith%"
echo %fname%
goto :eof


:getfilename
set "fname=%~n1"
goto :eof

miltonsnider
Posts: 3
Joined: 29 Mar 2010 12:56

Re: replacing text

#5 Post by miltonsnider » 31 Mar 2010 12:25

thank you very much. All I needed was %~n0

Post Reply