script to move files with modulus

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
doscode
Posts: 175
Joined: 15 Feb 2012 14:02

script to move files with modulus

#1 Post by doscode » 05 Sep 2012 16:35

Can you help with this? I need script to move files into different folder. There are images exported from video, so they have number of frames, e.g. from a0000.jpg to .. a0997.jpg

and I would like to go from a0000.jpg to .. a0997.jpg and move every
2nd - 7th image. (one image skipped in the folder, next 6 images moved to folder, and so on)...
Last edited by doscode on 06 Sep 2012 02:14, edited 2 times in total.

Squashman
Expert
Posts: 4474
Joined: 23 Dec 2011 13:59

Re: script to rename files

#2 Post by Squashman » 05 Sep 2012 17:38

Your title says rename but your description says move.
A single for loop with a counter should do the trick.
Then use two nested IF statements to see if your counter is between 2 and 7.
Keep adding 1 to the counter at the beginning of the loop and reset the counter to zero when it gets to 7.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: script to rename files

#3 Post by doscode » 06 Sep 2012 02:08

Yes, I changed from rename to move, because it is more effective. Can you advice SET calculation for modulus? How to calculate this
remains = x modulus 6

I am trying something like

Code: Select all

REM SET /A r = %%x %%%% 6
SET /A X = 6
echo 1)
SET /A r = %%x %%%% 6
echo %%x
echo 2)
SET /A r = %%x %%% 6
echo %%x


gives syntax error

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: script to move files with modulus

#4 Post by abc0502 » 06 Sep 2012 02:46

@doscode
set /a command doesn't take variable like "%%a" or %var% it must be only text without %% or !! signs

so set the %%x to a variable name such as "var", then

Code: Select all

set /a result = var % 6

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: script to move files with modulus

#5 Post by doscode » 06 Sep 2012 03:52

I know there's a lot of mistakes yet, but can you tell me what mistakes are in the ocde?

Code: Select all

@echo off
mkdir moved
SET c=0

FOR /F %%B IN ('dir *.jpg /b /o:n') DO (
SET c=c+1
set /a remains = c %% 6
IF %%remains NEQ 0 ( move %%B /moved )
)
pause


Command
set /a remains = c % 6
shows error:
Operator not found
Last edited by doscode on 06 Sep 2012 04:25, edited 2 times in total.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: script to move files with modulus

#6 Post by doscode » 06 Sep 2012 04:09

Or should it be like so with delayedexpansion

Code: Select all

Setlocal EnableDelayedExpansion
@echo off
mkdir moved
SET c=0

FOR /F %%B IN ('dir *.jpg /b /o:n') DO (
SET /A c=!c!+1
echo !c!
set /a remains = c % 6

REM IF !remains! NEQ 0 move %%B /moved

)


pause


But the arithmetic does not work here and echo displays c+1
Last edited by doscode on 06 Sep 2012 04:25, edited 1 time in total.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: script to move files with modulus

#7 Post by doscode » 06 Sep 2012 04:25

OK I fixed it but I have problem. I have run the script and my files disapeared, they are not in the folder moved.

Edit:
New fix

Code: Select all

Setlocal EnableDelayedExpansion
@echo off
mkdir moved
SET c=0

FOR /F %%B IN ('dir *.jpg /b /o:n') DO (
SET /A c=!c!+1
echo !c!
set /a remains = c % 6

REM IF !remains! NEQ 0 move %%B ./moved/%%B
)

Squashman
Expert
Posts: 4474
Joined: 23 Dec 2011 13:59

Re: script to move files with modulus

#8 Post by Squashman » 06 Sep 2012 06:15

I understand the logic you are trying to use but it doesn't match the description of your initial question.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: script to move files with modulus

#9 Post by foxidrive » 06 Sep 2012 06:43

Your directory spec is using unix path separators.

./moved/%%B

should be:

"moved\%%B"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: script to move files with modulus

#10 Post by foxidrive » 06 Sep 2012 06:51

Does this work?


Code: Select all

@echo off
Setlocal EnableDelayedExpansion
mkdir moved
SET c=1
FOR /F "delims=" %%B IN ('dir *.jpg /b /o:n') DO (
if not !c! EQU 1 echo move "%%B" "moved\"
SET /A c=c+1
if !c! EQU 8 set c=1&pause
)

Post Reply