0

I am trying to create a batch file that searches for .pst files and then moves it those files to a specific location. So far this is what I have. Please help.

cd \   
setlocal enabledelayedexpansion 

for /r %%i in (*.pst) do (   
::echo %%~i

set MyPath=%i%   
move "%MyPath%" "C:\users\zack.vigrass\desktop\"
)

endlocal

PAUSE

edit: I didn't realize I wasn't asking a question. I was mostly asking Why does it returned a blank file instead of the intended .pst file.

Dan
  • 1
  • 1
  • 4
    why not `move %%i` directly ? And I guess `%i%` is just a typo here in your example ? If all of this does not apply: please state your specific problem, at present you are only stating facts and not asking a question. – Marged Dec 16 '15 at 20:35
  • 2
    another victim of the famous [delayed expansion trap](http://stackoverflow.com/a/30284028/2152082)... (and of course that typo, that @Marged noted) – Stephan Dec 16 '15 at 20:43
  • 1
    set "MyPath=%%i" (with qoutes around and the `%` corrected), so any trailing spaces will no longer become part of the `MyPath` value... – aschipfl Dec 16 '15 at 21:00
  • Careful using `:: Comment` in blocks of code, safer to use `rem comment` there. – Bloodied Dec 17 '15 at 02:08

1 Answers1

2
set MyPath=%i%   

will not set mypath to the contents of the metavariable controlling the for loop (%%i, but will set it to the contents of i which is an ordinary environment variable, most likely undefined and hence nothing.

Extra problem : there are trailing spaces on the line which will be included in the value assigned.

Hence mypath will acquire a value of (a few spaces)

move "%MyPath%" "C:\users\zack.vigrass\desktop\"

Unfortunately, batch replaces the value of any %var% within a block (a series of parenthesised statements) literally with the value of that variable at the time the for (in this instance) is encountered.

Since when the for is encountered, mypath is undefined, then the result will be

move "" "C:\users\zack.vigrass\desktop\"

There appears to be no reason in this instance to transfer the value to mypath.

move "%%i" "C:\users\zack.vigrass\desktop\"

should work for you.

Magoo
  • 77,302
  • 8
  • 62
  • 84