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
replacing text
Moderator: DosItHelp
Re: replacing text
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)
Regards
aGerman
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
-
- Posts: 3
- Joined: 29 Mar 2010 12:56
Re: replacing text
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
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
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
Re: replacing text
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:
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
-
- Posts: 3
- Joined: 29 Mar 2010 12:56
Re: replacing text
thank you very much. All I needed was %~n0