Author Topic: Issue with .bat and robocopy.  (Read 35369 times)

nicedreams

  • Junior Member
  • **
  • Posts: 11
    • View Profile
Issue with .bat and robocopy.
« on: July 07, 2014, 09:42:41 »
Been trying to create a new User Defined Command that I have set to Command Type=.bat

Using the below with robocopy and it will not work.  The path's get screwed up.  I've tried with quotes, without quotes, tried about every variable I can from the online docs and still comes out incorrect.   http://multicommander.com/docs/multitags


robocopy "${sourcepath}" "${targetpath}" /TEE /MT /MIR /DCOPY:T /COPY:DAT /XD AppData /XJ /FFT /ZB /SEC /XA:SH /R:1 /W:1
pause

I've even tried

robocopy "${sourcepath}" "${targetpath}"      and      robocopy ${sourcepath} ${targetpath}     and still same issue.

This is what I get and a different combo.  I've actually gotten it to have a Dest, but then it gives me double quotes and other extras and gives an error.

========================================
   Source : D:\scans" D:\testfolder"\
     Dest -

    Files : *.*

 Exc Dirs : AppData

  Options : *.* /FFT /TEE /S /E /DCOPY:DAT /COPY:DATS /PURGE /MIR /ZB /XJ /XA:SH
 /MT:8 /R:1 /W:1

------------------------------------------------------------------------------

ERROR : No Destination Directory Specified.

       Simple Usage :: ROBOCOPY source destination /MIR

             source :: Source Directory (drive:\path or \\server\share\path).
        destination :: Destination Dir  (drive:\path or \\server\share\path).
               /MIR :: Mirror a complete directory tree.

    For more usage information run ROBOCOPY /?

****  /MIR can DELETE files as well as copy them !
Press any key to continue . . .
========================================

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4271
    • View Profile
    • Multi Commander
Re: Issue with .bat and robocopy.
« Reply #1 on: July 07, 2014, 10:13:43 »
${sourcepath} ${targetpath}
will return a quoted path so you do not need to insert " around them

you can do
Code: [Select]
echo ${sourcepath} ${targetpath}
pause
To just get an output of what the tags are translated to

ice-man

  • Active Member
  • ***
  • Posts: 56
  • The Little Extra
    • View Profile
Re: Issue with .bat and robocopy.
« Reply #2 on: July 07, 2014, 14:20:51 »
First you need to figure out exactly what path you need to send to robocopy.
Then you need to compare that to the paths you get from MC.
And from that you should be able to figure out what differs and possible why it does not work.
Intel i7-6700K - Running on latest Windows 10 64bit Insider Preview

nicedreams

  • Junior Member
  • **
  • Posts: 11
    • View Profile
Re: Issue with .bat and robocopy.
« Reply #3 on: July 07, 2014, 17:28:06 »
I've tried without the quotes and it's the same issue.

If I use "copy" or "xcopy" it work fine.  It's just something between the variables and how robocopy is picking them up.

I'm trying to make a function to where when I have two panes open, it will use the robocopy /MIR to mirror the left pane to the right pane.

nicedreams

  • Junior Member
  • **
  • Posts: 11
    • View Profile
Re: Issue with .bat and robocopy.
« Reply #4 on: July 07, 2014, 18:29:35 »
Doing like you put above seems to get good results.

echo ${sourcepath} ${targetpath}
pause


But back to using robocopy and it puts them both on the source line for some reason.  Like robocopy picks those variables up differently?


Want this to work as it makes it much easier than editing my batch script or manually doing this and changing the directories.


Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4271
    • View Profile
    • Multi Commander
Re: Issue with .bat and robocopy.
« Reply #5 on: July 07, 2014, 22:03:55 »
as Ice-Man said.. You need to check what the difference is from a manually created line and the generated.

Do you get it to work if you hardcode the path in the script ?
is the generated line from MC the same as the manually created that work ?
if not what is different.

MC just generate a bat file that is then sent to cmd.exe to run.

How robocopy work I have no idea of. But if you send it the correct parameters it should work.




nicedreams

  • Junior Member
  • **
  • Posts: 11
    • View Profile
Re: Issue with .bat and robocopy.
« Reply #6 on: July 07, 2014, 23:31:54 »
To keep it simple so you can try it on your own PC without all the extra variables.  I'm on Windows 8.1 update 1.


WORKS inside of MC

robocopy D:\scans D:\testfolder
pause


DOES NOT WORK inside of MC  (I have D:\scans in my left pane and D:\testfolder in my right pane within MC when I run it.)

robocopy ${sourcepath} ${targetpath}
pause


I have tried all other variables like ${forcuspath}  and others.

Still get error with entire path on the same line for SOURCE.

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4271
    • View Profile
    • Multi Commander
Re: Issue with .bat and robocopy.
« Reply #7 on: July 08, 2014, 06:47:36 »
I don't have robocopy so I can not test..

But if the command line is exactly the same, and it does not work. it is very strange.. then something is up with robocopy.

ice-man

  • Active Member
  • ***
  • Posts: 56
  • The Little Extra
    • View Profile
Re: Issue with .bat and robocopy.
« Reply #8 on: July 08, 2014, 07:38:23 »
To keep it simple so you can try it on your own PC without all the extra variables.  I'm on Windows 8.1 update 1.

WORKS inside of MC

robocopy D:\scans D:\testfolder
pause

DOES NOT WORK inside of MC  (I have D:\scans in my left pane and D:\testfolder in my right pane within MC when I run it.)

robocopy ${sourcepath} ${targetpath}
pause


I have tried all other variables like ${forcuspath}  and others.

Still get error with entire path on the same line for SOURCE.

I do not have robocopy either. But did some quick test. and the two command lines are NOT the same

Manually entered that you say works..
Code: [Select]
robocopy D:\scans D:\testfolder
The Generated line by MC will be
Code: [Select]
robocopy "D:\scans\" "D:\testfolder\"
See. They are very different.  The generated have quotes and trailing slashes.
Maybe robocopy do not like quotes and/or trailing slashes.

And when you figure out what it is. Then you can do some bat script magic and remove the quotes or trailing slash if needed
Intel i7-6700K - Running on latest Windows 10 64bit Insider Preview

nicedreams

  • Junior Member
  • **
  • Posts: 11
    • View Profile
Re: Issue with .bat and robocopy.
« Reply #9 on: July 08, 2014, 17:39:22 »
I already modify a batch script to run backups between folders and was hoping to have the folders opened in the two panes and starting the script that way would be nice, but I guess has issues with robocopy.

I will just stick to my normal batch scripts.

All Windows 7 and 8 have robocopy if you want to try.  Its the main utility for backups and sync in networks and works like a champ.  I'm just testing with 2 test folders and files until it works.


Maybe the author can make a left and right pane variable without quotes and trailing backslash in the next version?  That would be cool.



Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4271
    • View Profile
    • Multi Commander
Re: Issue with .bat and robocopy.
« Reply #10 on: July 08, 2014, 17:49:41 »
You can strip quote and trailing slash in bat script it self.

But first you need to figure out of if is the quotes OR the trailing slash or BOTH that RoboCopy do not like.
Because if you remove both and robocopy want quotes it may still not work.

Just store the the ${sourcepath} value in a bat variable first and then strip the quotes and trailing slash. and if quotes are needed then add them again

Code: [Select]
SET SOURCE=${sourcepath}

rem Remove Quotes
SET SOURCE=%SOURCE:"=%

rem Remove Trailing Slash
IF %SOURCE:~-1%==\ SET SOURCE=%SOURCE:~0,-1%

rem Add quotes again (IF that are required)
SET SOURCE="%SOURCE%"
Then do the same for the targetpath and then you just send the bat variables to the robocopy line as %SOURCE% and %TARGET%

nicedreams

  • Junior Member
  • **
  • Posts: 11
    • View Profile
Re: Issue with .bat and robocopy.
« Reply #11 on: July 09, 2014, 03:26:34 »
Doesn't work.  I'm giving up on it.  I'll stick to batch.

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4271
    • View Profile
    • Multi Commander
Re: Issue with .bat and robocopy.
« Reply #12 on: July 09, 2014, 06:56:13 »
Then you did not write it correct.

But as you have given up. I will not ask for information about what you did.
« Last Edit: July 09, 2014, 06:59:07 by Mathias (Author) »

Krekkie

  • Junior Member
  • **
  • Posts: 16
    • View Profile
Re: Issue with .bat and robocopy.
« Reply #13 on: July 09, 2014, 07:46:01 »
Did a quick test with Robocopy:
Code: [Select]
robocopy "D:\scans\" "D:\testfolder\" --> error
robocopy D:\scans\ D:\testfolder\ --> ok
robocopy "D:\scans" "D:\testfolder" --> ok
So I think you better use the last one (remove trailing slash only) so that it will work with spaces in the folder names.

nicedreams

  • Junior Member
  • **
  • Posts: 11
    • View Profile
Re: Issue with .bat and robocopy.
« Reply #14 on: July 20, 2014, 20:35:24 »
Since there was an update to Multi Commander I went ahead and tried this again.  Still doesn't work.  Any other action like XCOPY or COPY or MOVE works fine though with the MC variables for some reason and don't understand why Robocopy picks this up wrong.

I don't think you are getting my point.  If I try to use a variable so whatever is opened in the left and right pane of the software, it will detect that and use ROBOCOPY /MIR to mirror the contents from the right pane to the left pane.  If I hard set the path in the script it works fine.  It's just when I use variables inside of Multi Commander, it messes up for some reason and adds trailing slashes and quotes.

Wish it would work or if there was a fast Directory Mirror I can trust like Robocopy built inside of this software.

So again......   robocopy "D:\scans" "D:\testfolder"       Works fine.

robocopy ${sourcepath} ${targetpath}     Does not work
robocopy "${sourcepath}" "${targetpath}"    Does not work

Using SET command to define variables, still gives same issues.

Tried all the variables and does not work, same results.
http://multicommander.com/docs/multitags

If you look at the error message below,  it never has destination like it picks up ${sourcepath} ${targetpath} as the same source line.  I've tried "quotes" also.  When I mess with it a little bit, then the destination is there, but the formatting becomes incorrect and gives errors.  It is not picking these up like if I used %appdata% and %temp% or other system variables.  Is is the $DOLLAR sign in MC screwing up the variables?

Maybe author could make up something to work with Robocopy correctly?  Maybe can just add a mirror function within MC that uses Robocopy and the option of mirror?  Will use this program every single day if I can get this working.  Will save time on modifying scripts for different tasks.


=====================================================================================
=====================================================================================
-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows

-------------------------------------------------------------------------------

  Started : Sunday, July 20, 2014 11:31:37 AM
   Source : D:\scans" D:\testfolder"\
     Dest -

    Files : *.*

 Exc Dirs : AppData

  Options : *.* /FFT /TEE /S /E /DCOPY:DAT /COPY:DATS /PURGE /MIR /ZB /XJ /XA:SH
 /MT:8 /R:1 /W:1

------------------------------------------------------------------------------

ERROR : No Destination Directory Specified.

       Simple Usage :: ROBOCOPY source destination /MIR

             source :: Source Directory (drive:\path or \\server\share\path).
        destination :: Destination Dir  (drive:\path or \\server\share\path).
               /MIR :: Mirror a complete directory tree.

    For more usage information run ROBOCOPY /?


****  /MIR can DELETE files as well as copy them !
Press any key to continue . . .
=====================================================================================
=====================================================================================
« Last Edit: July 20, 2014, 20:38:07 by nicedreams »

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4271
    • View Profile
    • Multi Commander
Re: Issue with .bat and robocopy.
« Reply #15 on: July 20, 2014, 22:27:02 »
If it works hardcoded it must be possible to make it work.

You just have to make sure that the generated line look exactly as the hardcoded. with quotes and trailing slashes in the same places.

So what does your hardcodes line look like exactly. and what does the generated line looks like (Not the line you write in the edit.. the finished line that MC generates)
what are the difference between them ?


ice-man

  • Active Member
  • ***
  • Posts: 56
  • The Little Extra
    • View Profile
Re: Issue with .bat and robocopy.
« Reply #16 on: July 22, 2014, 11:12:21 »
This is getting crazy.. This should be simple to figure out.. If it works hardcode it then it should be possible to get it working

And it is working.. Took me 3 min to figure out... and all the information for getting it to run was already in this topic.

First doing a quick test you notice that RoboCopy do NOT like path to have a trailing slash ( \ at the end ) if the path is quoted.
BUT we do want to use quoted path since we do want to support spaces in the path. So we must make sure there is no \ at the end.

So what are ${sourcepath} and ${targetpath} returning ? Quick test show that they are returning a quoted path WITH trailing slash
( eg "R:\My Folder\" )

And since we know that robocopy do not like trailing slashes they need to be removed.
And that can be done using BAT scripting. and how to do that is shown in a previous post by mathias in the topic.

So instead of sending ${sourcepath} to the robocopy line we set it to a BAT script variable.
and then we do bat scripting to remove the quotes, then we remove the trailing slash, then readd quotes again
and now the "fix" path is in a BAT script valiable named SOURCE so then we use that for robocopy instead as %SOURCE%
Code: [Select]
SET SOURCE=${sourcepath}
rem Remove Quotes
SET SOURCE=%SOURCE:"=%
rem Remove Trailing Slash
IF %SOURCE:~-1%==\ SET SOURCE=%SOURCE:~0,-1%
rem Add quotes again
SET SOURCE="%SOURCE%"

and we do the same for ${targetpath}

A complete script will look like

Code: [Select]
SET SOURCE=${sourcepath}
rem Remove Quotes, Remove Trailing slash, Readd Quotes
SET SOURCE=%SOURCE:"=%
IF %SOURCE:~-1%==\ SET SOURCE=%SOURCE:~0,-1%
SET SOURCE="%SOURCE%"

SET TARGET=${targetpath}
rem Remove Quotes, Remove Trailing slash, Readd Quotes
SET TARGET=%TARGET:"=%
IF %TARGET:~-1%==\ SET TARGET=%TARGET:~0,-1%
SET TARGET="%TARGET%"

robocopy %SOURCE% %TARGET% /TEE /MT /MIR /DCOPY:T /COPY:DAT /XD AppData /XJ /FFT /ZB /SEC /XA:SH /R:1 /W:1

pause

And there is it..

Case closed.


Intel i7-6700K - Running on latest Windows 10 64bit Insider Preview

nicedreams

  • Junior Member
  • **
  • Posts: 11
    • View Profile
Re: Issue with .bat and robocopy.
« Reply #17 on: August 11, 2014, 04:26:34 »
Haven't tried to mess with this in a bit and decided to come back to it.  Thanks for the script that you wrote up for this.

I have tried it and still getting issues.  Only spent a few minutes so far looking at the issue.  Here it is below:

(This is using a copy and paste of your code you provided.)

================================================================

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows

-------------------------------------------------------------------------------

  Started : Sunday, August 10, 2014 7:22:48 PM
   Source : D:\testfolder\
     Dest : D:\scans\

    Files : *.*

 Exc Dirs : AppData

  Options : *.* /FFT /TEE /S /E /DCOPY:DAT /COPY:DATS /PURGE /MIR /ZB /XJ /XA:SH
 /MT:8 /R:1 /W:1

------------------------------------------------------------------------------

ERROR : You do not have the Backup and Restore Files user rights.
*****  You need these to perform Backup copies (/B or /ZB).

ERROR : Robocopy ran out of memory, exiting.
ERROR : Invalid Parameter #%d : "%s"

ERROR : Invalid Job File, Line #%d :"%s"


  Started : %s %s

   Source %c

     Dest %c
       Simple Usage :: ROBOCOPY source destination /MIR

             source :: Source Directory (drive:\path or \\server\share\path).
        destination :: Destination Dir  (drive:\path or \\server\share\path).
               /MIR :: Mirror a complete directory tree.

    For more usage information run ROBOCOPY /?


****  /MIR can DELETE files as well as copy them !
Press any key to continue . . .


================================================================

nicedreams

  • Junior Member
  • **
  • Posts: 11
    • View Profile
Re: Issue with .bat and robocopy.
« Reply #18 on: August 11, 2014, 04:37:01 »
Tried messing with it and so I deleted the entire robocopy string and just put

robocopy %SOURCE% %TARGET% /MIR


It looked like it went right through, but now all my test files in my source and destination are gone and don't have a clue what happened.  Using /MIR is just a mirror function so should have my data in source still.  I do from command line.




Don't worry about this function anymore.  I don't know if you are testing this on your own computer or just writing code and having me try it.  Windows Vista/7/8 has robocopy built in and is awesome.  I just used 2 directories and some test files to try this out.  But if files just deleted from using robocopy within MultiCommander with the /MIR command then it's not enough for me to trust MC with Robocopy.

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4271
    • View Profile
    • Multi Commander
Re: Issue with .bat and robocopy.
« Reply #19 on: August 11, 2014, 08:26:21 »
Ice-Man's script works. I tried it..

Look at the error

"ERROR : You do not have the Backup and Restore Files user rights.
*****  You need these to perform Backup copies (/B or /ZB).

ERROR : Robocopy ran out of memory, exiting.

You probably need to run the script as admin as robocopy need more then standard permissions. Also it says it run out of memory

The error texts are from robocopy.. not MC.  MC ONLY creates a bat script from the script you wrote in MC. The only thing MC do are to replace MC specific tags like ${sourcepath}, And then the script is handed off to Windows just like all normal .bat script.
MC do not run the bat script it self. Windows run the script using cmd.exe as all bat script are.

And MC did not delete your files.. robocopy did.  MC only creates the script. and then windows run that script
My guess it that you did /MIR but swapped the source and destination parameter in one of your tests.
using /MIR will delete files on target if that files does not exists on source.

If you are going to run bat script you need to understand how bat scripting work.

ice-man

  • Active Member
  • ***
  • Posts: 56
  • The Little Extra
    • View Profile
Re: Issue with .bat and robocopy.
« Reply #20 on: August 11, 2014, 08:41:44 »
Mathias is correct. The command need to be run with Admin permissions since some of the parameters for robocopy uses functions that require full access0.
And it also says so in the error text  /B or /ZB parameters require "Backup and Restore Files user rights" and you need to elevate to admin before running the script
So start MC as admin before running the script or remove the /B / ZB parameter

The other errors that robocopy say are just an effect from the first error. so when fixing the first error the other errors will also go away.
ALWAYS read the error message.  The error message are your friend.
Intel i7-6700K - Running on latest Windows 10 64bit Insider Preview

nicedreams

  • Junior Member
  • **
  • Posts: 11
    • View Profile
Re: Issue with .bat and robocopy.
« Reply #21 on: August 13, 2014, 07:27:47 »
Running MC as admin has fixed the issue and is working fine now.  I really appreciate all the time and effort you guys put into this simple task for me.  Was just getting frustrated that I felt defeated with robocopy in MC after using robocopy for years.  lol

Thanks again!

nicedreams

  • Junior Member
  • **
  • Posts: 11
    • View Profile
Re: Issue with .bat and robocopy.
« Reply #22 on: August 13, 2014, 07:32:44 »
Just FYI I changed the variables to ${leftpath} and ${rightpath} since I figured out if you're not careful which pane is selected, you'll lose your files.

Messing with some other options too.  :)