0

I set up a batch (copy.bat) file in my Windows sendto Directory that is for Copying a bunch of files via rightclicking the directories and choosing sendto "copy.bat".

@echo off
setlocal enabledelayedexpansion
set cnt=0
set cur=0
:files
for /R "%~1" %%F in (*.7z, *.avi) do (set /a cnt+=1)
shift
if "%~1" NEQ "" goto :files
echo !cnt! files found for copying.
echo Processing ...
set "DEST_DIR=E:\"
:while
for /R "%~1" %%F IN (*.7z, *.avi) do (
 if exist "%%F" (
 set FILE_DIR=%%~dpF
 set FILE_INTERMEDIATE_DIR=!%%~dpF:%%~1%=!
 xcopy /E /I /Y "%%F" "%DEST_DIR%!FILE_INTERMEDIATE_DIR!"
 )
 set /a cur+=1
 echo !cur!/!cnt! done...
)
shift
if "%~1" NEQ "" goto :while
pause

If I run the first for loop alone it counts the files. If I run the second loop on its own it copies exatly those files the other loop is supposed to count. However, both loops in one batch file will somehowe conflict. The Filecount works correctly but the copy process delivers an error: "The Path of is too long". Between "of" and "is" there is an additional Space. The final pause will correctly wait for button press. I assume it has to do something with the %%F variable, but renaming it in one of the loops does not help.

asked Oct 12, 2022 at 13:41
2
  • First advice: Do not put any files other than shortcuts into SendTo. Creat a shortcut, .lnk file with your target as the actual batch file located in an appropriate and easily managed directory within your usual filing area. Commented Oct 12, 2022 at 15:01
  • Thx for that advice. This way its easier to edit the files. Commented Oct 13, 2022 at 7:07

1 Answer 1

0

Please change the name of your .bat - copy is a keyword to cmd.

Your problem is that the first routine shifts out all of the parameters, so your second routine has no parameters to deal with.

The easiest way is probably:

set "DEST_DIR=E:\"
call :while %*
pause
goto :eof
:while
for /R "%~1" %%F IN (*.7z, *.avi) do (
...
if "%~1" NEQ "" goto while
goto :eof

which executes the second routine as en internal subroutine (the : in the call is required) providing that routine with a fresh copy of the parameter-list (%*)

You could also consider moving the if "~1"... to the start of the routine (just after the :while), change it to if "%~1" EQU "" goto :eof and change the final statement to goto while.

The label :eof is defined as end-of-file by cmd and should not be declared.

answered Oct 12, 2022 at 16:00
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.