1

So I'm fairly new to VBA, using it within some spreadsheets so forgive me if this is a super obvious fix, I'm sure it is but have spent hours on with with no success.

The following piece of code will lie inside of an if statement - not sure if thats worth mentioning or not.

I want to simply move, and rename files from one location to the other It needs to utilize cmd.exe, and use the 'move' argument - the source path and source destinations will be changeable depending on the pc - so it will need to use the '%temp% - as the initial file will be stored in this folder, and %userprofile% for the destination path.

Here is the code so far - I think the issue is just to do with the formatting -

Sub movefile ()

Dim Origpath As String
Dim NewPath As String

OrigPath="%temp%\newfile.txt"
NewPath="%userprofile%`Documents\newfile.vbs"

<<next code inside of an if statement>>

moveFile="C:\windows\system32\cmd.exe /C move " & OrigPath & " " &  NewPath

End Sub

Maybe the " and & arent placed correctly? But the code works fine up until the line 'movefile...'

Probably a super easy way of doing it - help please!!

2
  • 2
    Isn't this what Name does? It looks like you could use Environ$ to get those paths: Environ$("temp") and Environ("UserProfile"). Commented Feb 6, 2020 at 19:35
  • 2
    moveFile="C:\windows\system32\cmd.exe /C move """ & OrigPath & """ """ & NewPath & """" in case of spaces in the paths, and you seem to have a typo in your NewPath value: backtick instead of backslash. Commented Feb 6, 2020 at 19:50

1 Answer 1

0

Try using Name:

name OrigPath as  NewPath

Alternatively you can use use fdo movefile, as explained in this answer.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.