0

I am trying to use robocopy to copy a file from one folder to other, but i do not think source and destination are being set properly. Really appreciate help on this.

@echo off
SET PathString = C:\Users\bibhukdas\Documents\New_folder1~C:\Users\bibhukdas\Documents\New_folder
echo ******start**** >>C:\Users\bibhukdas\Documents\New_folder\log.txt
echo ******POINT1**** >>C:\Users\bibhukdas\Documents\New_folder\log.txt
echo "%PathString%" >>C:\Users\bibhukdas\Documents\New_folder\log.txt
for /f "tokens=1,2 delims =~" %%a in("%PathString%") do set Source1=%%a&set  Destination1=%%b
echo ******POINT1**** >>C:\Users\bibhukdas\Documents\New_folder\log.txt
if "%Source1%" == "0" goto NOSOURCE1
robocopy.exe %Source1% %Destination1% *.doc /COPY:DAT /MINAGE:5 /R:1 >>    C:\Users\bibhukdas\Documents\New_folder\log.txt

:NOSOURCE1
echo *****source unavailable******* >>C:\Users\bibhukdas\Documents\New_folder\log.txt
:EXIT
user258427
  • 141
  • 1
  • 4
  • 12

2 Answers2

0

You can easily add an ECHO command to see what values your variables have. That way you will know for sure whether they are set properly or not.

Anyway, there does seem to be a couple of issues in the posted script.

The first one is this line:

SET PathString = C:\Users\bibhukdas\Documents\New_folder1~C:\Users\bibhukdas\Documents\New_folder

The spaces aroung the = are important. The one before the assignment becomes part of the variable name and the other one part of the value. So, the above line essentially creates a variable called PathString , i.e. with a trailing space character. To fix this issue, just remove both spaces.

One other is the for loop line. The delims option is followed by a space, which is invalid and will cause a syntax error. Again, just remove the space.

There's also an issue of a different kind. You are not omitting the source unavailable output after the robocopy command, which means you would get the source unavailable line in your log despite the the files having been copied. That should be easily fixed by adding a GOTO :EXIT command just after the robocopy line. However, you could also do like this:

if "%Source1%" == "0" (
  echo *****source unavailable******* >>C:\Users\bibhukdas\Documents\New_folder\log.txt
) else (
  robocopy.exe %Source1% %Destination1% *.doc /COPY:DAT /MINAGE:5 /R:1 >>    C:\Users\bibhukdas\Documents\New_folder\log.txt
)

Finally, you could improve maintainability of your script by reducing the number of output redirections.

A redirection can be applied to a bracketed block of commands, like this:

(
  command1
  command2
  command3
  ...
) >outputfile

So, you could apply the redirection to C:\Users\bibhukdas\Documents\New_folder\log.txt to all your commands that require it this way:

(
  echo ******start****
  echo ******POINT1****
  echo "%PathString%"
  for /f "tokens=1,2 delims=~" %%a in("%PathString%") do set Source1=%%a&set  Destination1=%%b
  echo ******POINT1****
  if "%Source1%" == "0" (
    echo *****source unavailable*******
  ) else (
    robocopy.exe %Source1% %Destination1% *.doc /COPY:DAT /MINAGE:5 /R:1
  )
) >>C:\Users\bibhukdas\Documents\New_folder\log.txt

Note, however, that the for loop will have to moved outside the block. This is because it sets variables that are later evaluated (using the % expansion) in the same block, which doesn't work as expected. This is one of the effects of a bracketed block. You could use delayed expansion here, but in this particular case it is much easier to just move the assignment outside the block.

So, after addressing all of the issues mentioned, your script might look like this:

@echo off
SET PathString=C:\Users\bibhukdas\Documents\New_folder1~C:\Users\bibhukdas\Documents\New_folder
for /f "tokens=1,2 delims=~" %%a in("%PathString%") do set Source1=%%a&set Destination1=%%b
(
  echo ******start****
  echo ******POINT1****
  echo "%PathString%"
  echo ******POINT1****
  if "%Source1%" == "0" (
    echo *****source unavailable*******
  ) else (
    robocopy.exe %Source1% %Destination1% *.doc /COPY:DAT /MINAGE:5 /R:1
  )
) >>C:\Users\bibhukdas\Documents\New_folder\log.txt
Andriy M
  • 76,112
  • 17
  • 94
  • 154
0

I have a done modifications to just gist of your code. I think it should work now

@echo off
SET "PathString=C:\Users\bibhukdas\Documents\New_folder1,C:\Users\bibhukdas\Documents\New_folder"
set "delim=,"
echo ******start**** 
echo ******POINT1**** 
echo "%PathString%" 
for /f "tokens=1,2 delims=," %%a in (`echo %PathString%`) do (
echo "%%a" 
echo "%%b"
echo ******POINT1**** 
)

Keep me posted. :)