How do I change file extension in a parameter within batch file?

Viewed 33

I'm a newbie in the batch file world and what I'm trying to do is this:

  1. I execute a R script through a batch file.
  2. This produces a .Rout file with the same name of the R script on the same folder where the script is located (what changes is that instead of a .R file now it's a .Rout file).
  3. I want to remove that specific .Rout file, and only that.

So far I've managed to run the script but I'm struggling with the deleting part. I have the full path of the R script stored in a variable through this command

for /f "delims=" %%x in (listFile.txt) do set file=%%x

It reads a txt file with the outcome of the search for the R script in the computer.

I guess I could launch another search for the .Rout file like I did with the R script but I am sure there must be a more efficient way since the path is essentially the same, just changing the file extension.

I tried doing %~n%file% but with no result. Can you help me please?


Edit As suggested in the comments I tried

@(for /f "delims=" %%x in (listFile.txt) do @echo %%x) & pause

which returns

c:\Users\user\OneDrive\Documents\SomeFolder\filename.R
c:\Users\user\OneDrive\Documents\OtherFolder\filename.R
c:\Users\user\OneDrive\Documents\OtherFolder\SubFolder\filename.R

These are the three locations of the R script I'm looking for on my computer (I duplicated the file onto different folders beacuse I'm trying to make it universal enough in case someone changes the R file location)


Code:

@echo off
(
   for %%a in ( c d e f g h) do (
      if exist "%%a:\" dir "%%a:\R.exe" /b /s /a-d
   )
)>"listRpath.txt"

for /f "delims=" %%x in (listRpath.txt) do set Rpath=%%x



(
   for %%a in ( c d e f g h) do (
      if exist "%%a:\" dir "%%a:\filename.R" /b /s /a-d
   )
)>"listFile.txt"

for /f "delims=" %%x in (listFile.txt) do set file=%%x


"%Rpath%" CMD BATCH "%file%"

:: Delete aux ".txt" files
del "listRpath.txt"
del "listFile.txt"

I can run my R script just fine with this code. My issue is changing the .R extension of the file to a .Rout one so I can then del "%RoutFile%".

0 Answers
Related