Batch move files into folders

March 23, 2010 at 22:29:12
Specs: Windows 7
Easiest with examples, i have something like this but with alot more files, lost of singles, lots in parts some with additional info in other files which have the same name but diff extensions

eg
mysingleworkfile.txt
myworkfilepart1.txt
myworkfilepart2.txt
myworkfilepart1.jpg
myworkfilepart2.jpg

I want to put each single file in its own directory with the same name

I want each file in parts and its extra images into single directories

Im pulling my hair out trying to learn batch script >< This is what i have no far, and yes i have alot to learn haha

Any help would be greatly appreciated!

--------------------
@echo off
for %%i in (*.*) do (

set test="%%~ni":~-5%

set substring="%%~ni":0,-5%

if %test%==part1 (
md "%substring%
move "%substring%"*.* "%substring% )
//eg move myworkfile*.* myworkfile which should move all parts and the xtra files


md "%%~ni"
move "%%~ni".* "%%~ni"
)

----------------------------



See More: Batch move files into folders

Report •

#1
March 24, 2010 at 14:27:28
Just one hint. You can't assign an environment variable and then refer to it inside the same sequence of statements embraced by parenthesis.

You have to enabke the "Delayed Expansion" at the beginning of your batch by coding

@echo off & setlocal EnableDelayedExpansion

then you have to mark the environment variables by ! instead of the conventional %. so e.g.

if !test!==part1 (
md "!substring!"
move "!substring!"*.* "!substring!" )

As I said this is just a hint and other things might be (and surely are)wrong in your code. To know more about the delayed expansion type set /?.


Report •

#2
March 24, 2010 at 22:18:05
Ok another whole afternoon experimenting i still cant get it to work...

iv read set /? for /? but im missing something obvious apparently

Why dose this make a substring
set substring=%asubstring:~0,-5%

But this just makes it something odd
set substring=!asubstring!:~0,-5%

Why dose
echo !substring!
output something totally differant from !!!!!
echo %substring%

Im pretty much ready to just give up, i could have gone through and manually made all these dame directories by now :(

Cheers for the tips but i could really do with some more...

@echo off & setlocal EnableDelayedExpansion

for %%i in (*.*) do (

set test=%%~ni:~-5%
set substring=%%~ni:0,-5%
//im doing something wrong here i just don't know what...

if !test!==cd1 (
md !substring!
move !substring!*.* !substring!
)

md %%~ni
move %%~ni.* %%~ni
)


Report •

#3
March 24, 2010 at 23:57:27
this is not "grammatically correct" due to unbalanced variable-tags (%, !):
set substring=!asubstring!:~0,-5%
my unnerstaning is, tags are like quotes, they never work except in matched pairs, nested properly: "he said 'crap' "
but not "he said" 'crap"
or even (sometimes)
"she said "screw this!" but he said "not now""
you want this:
set substring=!asubstring:~0,5!
note: the removal of the trailing perct, which conflicts the whole thing.
note also: the removal of the "internal" exclm, which also conflicts.
basically mostly its either "either or", its either all ! or all %, depending
on wether a) enabledelayed is turned on, and b) the "situation":
inside or outside a "block", etc. BUT you can put a subset of
%var% inside a !---!, if you catch the gist:
set /a gg=0
set /a cc=5
set test=!a:~%gg%,%cc%!
will (i think, without testing) expand both percs inside the exclms,
but only outside codeblocks! (i think) (i SUCK at this stuff...).
I fully sympathasize, this
crap takes a who friggin LOT of getting used. If you ask real nice,
Judago is excellent at explicating this muck, i am not. He can point
to his how-tos and web stuff.
Delayed expansion is something you either figure out quick, or abandon
ship, and i still have issues with it.
case in point:
"Why dose
echo !substring!
output something totally differant from !!!!!
echo %substring%"
---
delayedexpansion strikes again. exactly!
ps: i would work on just fixing your script, but i'm way beat tonight! i figure you'll get some competent help, if not, maybe some incompetent help tomorrow (ie: me)

Report •

Related Solutions

#4
March 25, 2010 at 10:26:20
Yeah i think i need to do abit more reading on that but it was enough to get it working ^_^

That must have been the ONLY thing i hadn't tried lol

To make it cleaner i check if the files exist
( if there are 2 parts or additional files they all get moved at once on the first part 1 file)
Also put the second part to move individual files in the else statment it should have been in :P

Thx for the help!


@echo off & setlocal EnableDelayedExpansion

for %%i in (*.*) do (

if exist %%i (
set test=%%~ni
set substring=!test!
set test=!test:~-5!
set substring=!substring:~0,-5!


if !test!==part1 (
md !substring!
move !substring!*.* !substring!
) ELSE (
md %%~ni
move %%~ni.* %%~ni
)
echo ----- Moved to !substring! ---------
)

)


Report •

#5
March 25, 2010 at 10:59:48
lol yeah it dosnt like spaces, now it dose ^_^

-------------------------------------------
@echo off & setlocal EnableDelayedExpansion

for %%i in (*.*) do (

if exist %%i (
set test=%%~ni
set substring=!test:~0,-3!
set test=!test:~-3!

if !test!==cd1 (
md "!substring!"
move "!substring!*.*" "!substring!"
) ELSE (
md "%%~ni"
move "%%~ni.*" "%%~ni"
)
)
)
-------------------------------------------


Report •

Ask Question