Reputation: 398
I am writing a batch program for controling my movie archive (Personel usage). This is what i am trying to do for copying folders.
:_Kopya
set "TRGT=%~1" & set "KPY-GLN[1]=%~2" & set "KPY-GLN[2]=%~3" & set "KPY-GLN[3]=%~4"
REM Checking user input and defining variables.
for /l %%s in (1,1,3) do (
if DEFINED KPY-GLN[%%s] (
for /f "tokens=1-2 delims=:" %%a in ("!KPY-GLN[%%s]!") do (
call :_Kontrol "%%a" "%%b" "" "" "aaaaa[%%s]" "bbbbb[%%s]" "" ""
if "!TEST!"=="0" goto :EOF
)
)
)
REM Copying folders.
for /l %%s in (1,1,3) do (
if NOT DEFINED bbbbb[%%s] set bbbbb[%%s]=!aaaaa[%%s]!
for /l %%a in (!aaaaa[%%s]!,1,!bbbbb[%%s]!) do (
call :_ReadLine "%MURL%" "%%a" "LINE"
if EXIST "!TRGT!\!LINE:~20!" rd /s /q !TRGT!\!LINE:~20!
robocopy /s /e "!LINE!" "!TRGT!\!LINE:~20!" >NUL 2>&1
)
)
goto :EOF
And this is the way i call,
call :_Kopya "C:\" "123:125" "124:130" "125"
Which means copy the file numbers from 123
to 125
and from 124
to 130
and 125
.
It works fine but there is a problem i want to solve. When i call this function the way i show its copying file number 124
2 times and file number 125
3 times. How can i fix this issue?
PS1: %MURL%
its a text file and contains local address of those files. Its something like this M:\Movies\000y.001y\The.Lord.of.the.Rings.The.Return.of.the.King.(2003){0167260}[00087]
PS2: :_ReadLine
its a function that reading specific line and adding value of this line to LINE
variable.
Upvotes: 0
Views: 81
Reputation: 80193
@ECHO Off
SETLOCAL
:: remove variables starting $
FOR /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
:: Parameters are adirectory range*
:: where range may be a single number or start:finish
SET "directory=%~1"
:loop
SHIFT
IF "%~1"=="" GOTO :eof
FOR /f "tokens=1,2delims=:" %%a IN ("%~1") DO (
IF "%%b"=="" (CALL :kopythis %%a) ELSE (FOR /L %%c IN (%%a,1,%%b) DO CALL :kopythis %%c)
)
GOTO loop
GOTO :EOF
:kopythis
IF DEFINED $%1 GOTO :EOF
SET $%1=Y
ECHO(COPY whatever with parameters %directory% and %1
GOTO :eof
This should do what you seem to need. I'll leave you to work out the details of how to structure whatever copy mechanism you need from the parameters provided.
Note that with this approach, quoting the parameters is optional with the obvious exception of the first when it's optional if the first doesn't contain separators. It also allows any number of range parameters.
Upvotes: 1