Reputation: 15
The idea is moving Files to Folders Like :
The Files Names:
1.pdf,2.pdf,3.pdf.....1000.pdf
And the results have to be:
(1.pdf-100.pdf) To Folder 1
(101.pdf-200.pdf) To Folder 2
(201.pdf-300.pdf) To Folder 3
..
This Code is working but it start with (0) and not (1)
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for %%a in (*.pdf) do (
echo %%~na|FINDSTR /R /C:"^[0-9]*$" >nul 2>nul&&(
set /a NumBas=%%~na/1000*1000
set /a NumHaut=^(%%~na/1000*1000^)+999
echo %%~a -^> !NumBas!-!NumHaut!
IF NOT EXIST "!NumBas!-!NumHaut!" MD "!NumBas!-!NumHaut!"
MOVE /Y "%%~a" "!NumBas!-!NumHaut!\%%~a" >nul
)
)
Upvotes: 0
Views: 1196
Reputation: 67216
Simpler:
@echo off
setlocal EnableDelayedExpansion
for %%a in (*.pdf) do (
set /A "Num=(%%~Na-1)/100+1, NumBas=(Num-1)*100+1, NumHaut=Num*100"
IF NOT EXIST "!NumBas!-!NumHaut!" MD "!NumBas!-!NumHaut!"
MOVE /Y "%%~a" "!NumBas!-!NumHaut!\%%~a" >nul
)
If you want folders with simpler "Folder 1", "Folder 2", etc names, just use "Folder !Num!"
and eliminate NumBas and NumHaut variables.
Upvotes: 1
Reputation: 724
The reason that your calculation is not working is because the CLI does not support floating points.
When you are doing:
set /a NumBas=%%~na/1000*1000
you are getting a decimal value. I suspect you are trying to take advantage of this in order to try to round the value. Unfortunately this doesn't work because as soon as you do 1/1000 it is 0.0001 which becomes 0, which is why your NumBas is becoming zero when passed to the next command.
I also noticed that you're dividing and multiplying by 1000, instead of 100. dividing by 100 will result (once corrected) in your files being sorted into thousands:
1-1000
1001-2000
2001-3000
and not into 100s as you asked in your question, to correct that just change the 1000s to 100s.
Now on the floating point, The easiest way to fix this would be to check if the NumBas value is less than 1, and if it is, make it 1
IF !NumBas! LSS 1 Set /a NumBas +=1
This adjusted code should result in what you're looking for:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for %%a in (*.txt) do (
echo %%~na|FINDSTR /R /C:"^[0-9]*$" >nul 2>nul&&(
set /a NumBas=%%~na/100*100
IF !NumBas! LSS 1 Set /a NumBas +=1
set /a NumHaut=!NumBas!+99
echo %%~a -^> !NumBas!-!NumHaut!
IF NOT EXIST "!NumBas!-!NumHaut!" MD "!NumBas!-!NumHaut!"
MOVE /Y "%%~a" "!NumBas!-!NumHaut!\%%~a" >nul
)
)
Upvotes: 1