Page 1 of 1
replacing text
Posted: 29 Mar 2010 13:03
by miltonsnider
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
Re: replacing text
Posted: 30 Mar 2010 10:48
by aGerman
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
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
Re: replacing text
Posted: 30 Mar 2010 12:35
by miltonsnider
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
Re: replacing text
Posted: 31 Mar 2010 11:02
by avery_larry
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
Re: replacing text
Posted: 31 Mar 2010 12:25
by miltonsnider
thank you very much. All I needed was %~n0