0

I want to concatenate strings in a batch file because I need to parameterize in the batch file (.bat) to use paths frequently. So instead of writing:

ren "C:\Folder\Sub Folder\filename.ext.tmp" "C:\Folder\Sub Folder\filename.ext"
del "C:\Folder\Sub Folder\filename.ext.tmp"

I would like to write something like

set pathString="C:\Folder\Sub Folder\"
ren pathString+"filename.ext.tmp" pathString+"filename.ext"
del pathString+"filename.ext.tmp"

Is this somehow doable? If so how? Thanks!

2 Answers 2

1

Your example the command should be:

Set "pathString=C:\Folder\Sub Folder"
Ren "%pathString%\filename.ext.tmp" "filename.ext"

There's no path needed for the rename destination because it hasn't changed, (therefore no need to prefix it with %pathString%). Then there's no need to delete the file you've just renamed because it no longer exists after being renamed.

Sign up to request clarification or add additional context in comments.

Comments

0

This is pretty basic batch variable usage, just remember to use the quotes correctly:

set pathString=C:\Folder\Sub Folder
ren "%pathString%\filename.ext.tmp" "%pathString%\filename.ext"
del "%pathString%\filename.ext.tmp"

That being said, your example code does not make a lot of sense because

  • you are trying to delete a file that will not exist because you rename it to something else?

or

  • you think you need to rename the file before deleting it?

Renaming before deletion only makes sense if you are trying to delete a locked file and deletion failure is acceptable. Doing that would look something like this:

@echo off

REM Set up some things for this example:
md "%temp%\batchtest"
echo.blah blah > "%temp%\batchtest\filename.ext"

REM Actual code:

set pathString=%temp%\batchtest
ren "%pathString%\filename.ext" "filename.ext.del"
del "%pathString%\filename.ext.del"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.