General Question Searching for a solution with a DOS batch code

fort

Active member
Joined
Mar 19, 2008
Messages
1,017
Reaction score
20
Points
38
Hello,

Working on nightlights.

I try to move, in DOS (with windows XP and a * .bat) - from a root folder - a set of files to subfolders that the batch creates simultaneously, and to which it assigns names in relation to file numbering.

A series of files named a_1.bmp, b_1.bmp, c_1.bmp, ... will be moved to a subfolder named 1.
A series of files named a_2.bmp, b_2.bmp, c_2.bmp, ... will be moved to a subfolder named 2.
A series of files named a_myfile.bmp, b_myfile.bmp, c_myfile.bmp, ... could be moved to a subfolder named myfile.

Some files might have names as: a_001396.bmp, b_001396.bmp..etc. And the corresponding subfolders named 001396, 001 397 ...

I found a code template ( and made some change for my use ) that might be appropriate ... (in French...)
https://openclassrooms.com/forum/sujet/dossiers-automatiquement-en-fonction-des-fichiers

... but I stumble on an obstacle:

beyond ..._9.bmp, and starting from _1.bmp, the script no longer creates subfolders.

Starting from ..._10.bmp or more, it works.

But I have no choice, at first sight, but to start from ..._1.bmp.

Not even ..._01.bmp

Attachment for testing.

If someone have an idea.

:hmm:

And thank you.


Edit: I'm quite a great beginner in DOS.
 

Attachments

  • Dispatch.zip
    98 KB · Views: 3
Last edited:

kuddel

Donator
Donator
Joined
Apr 1, 2008
Messages
2,064
Reaction score
507
Points
113
Hi fort,
you're using the * (wildcard) a little bit wrong.
The * matches one ore more characters,
The ? matches exactly one character.
So changing the copy-line to this should work:
Code:
move /Y  "?_!disp!.bmp" "!disp!"


---------- Post added at 17:43 ---------- Previous post was at 17:35 ----------

Another thing is, that you could check for the existence of the (sub-)directory with 'if not exists' before executing 'MD'. So you don't have to "silence" error output (>NUL 1>&2)
Code:
@echo off
setlocal enabledelayedexpansion
for %%i in (*.bmp) do (
    for /F "tokens=2 delims=._" %%j in ('echo %%i') do (
        set disp=%%j
        if not exist !disp! MD !disp!
        move /Y "?_!disp!.bmp" "!disp!"
    )
)
pause
 

fort

Active member
Joined
Mar 19, 2008
Messages
1,017
Reaction score
20
Points
38
Hello kuddel

I resumed after taking a break of a few weeks this summer my activity with the nightlights and this ends.

All my batch are pretty much at the point. But if I sometimes spent a lot of time on some ( I'm quite inexperienced, not far from a zero level ), I really did not see how to get around that one. I was even looking in the meantime for a substitution script to get around the difficulty but with an extra operation and quite a bit of research again.

Your script is impeccable.

:)

Thank you and...thank you and...thank you.

( i'll try to understand your changes: this could be usefull for others implementations )


Edit:

you're using the * (wildcard) a little bit wrong.
The * matches one ore more characters,
The ? matches exactly one character.

I did not think about that. I read here or there that it was equivalent. And according to the context ...

I really tried for several hours to change the settings, different combinations, even preview here or there for example that a space (disp=%% j or disp= %% j) could change the behavior of the script, but I was down.

:)
 
Last edited:

kuddel

Donator
Donator
Joined
Apr 1, 2008
Messages
2,064
Reaction score
507
Points
113
...yeah, DOS batch scripts have a high potential to drive one nuts :p, so don't feel bad.
My experience is just the result of many many many failures and dead-ends, too.
 

fort

Active member
Joined
Mar 19, 2008
Messages
1,017
Reaction score
20
Points
38
Hello,

I have a problem with this script again. It works well for files named 1_1.bmp, 1_2.bmp ... 1_50.bmp ... 2_1.bmp, 2_2.bmp ... 2_50.bmp ... and creates as expected folders '1' '2' ... '50' ...; and simultaneously puts 1_1, 2_1 ... in the folder '1'; 1_2, 2_2 ... in folder '2'; 1_50 ... in the folder '50' ...

Now, it happens that my files may have to be named: OSM_1_1.bmp or ITO_1_1.bmp... ... so, longer names, but I search for an identical result (folders '1' '2'. .. '50' with files of the same last number in).
And there, it does not work anymore.

I searched on the internet for these questions of 'tokens' and 'delims' and some others things, but I am down. I do not see.

I think that
... tokens should be now =3 and delims=._" %%k or %%j %%k
( for /F "tokens=2 delims=._" %%j )
... set disp= %%k...
( set disp=%%j )
... but there:
"?_!disp!.bmp"
???

I can work around the problem with an additive script but if I could do with this one alone ...( there are already so many operations to do elsewhere for this work )

Again two attachments:

1 the initial device with "short name files".
2 the same but with "long name files", as above, but that does not work.

Good day
 

Attachments

  • Short_names.zip
    98 KB · Views: 0
  • Long_names.zip
    98.1 KB · Views: 1
Last edited:
Top