batchfiles move to multi dest

? ?
February 19, 2009 at 14:41:50
Specs: Windows XP, ?
how can i move several source files to multiple destination folders?
e.g.,
move
file134 to folder134
file264 to folder264
file598 to folder598
thanks.

See More: batchfiles move to multi dest

Report •


#1
February 20, 2009 at 02:00:03
move file134 folder134
move file264 folder264
move file598 folder598


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
February 20, 2009 at 08:33:40
setlocal enabledelayedexpansion
for %%f in (file*) do {
   set name=%%f
   set name=!name:file=folder!
   md !name!
   move %%f !name!
}


Report •

#3
February 20, 2009 at 12:21:44
Thanks M2 and klint!
-M2
typing

move file134 folder134
move file264 folder264
move file598 folder598

is the solution but instead of that, I was wondering if you could do a %1 or something so all the files would go to its folder? There are hundreds of files to do so typing that in would require lots of time and there might be typo. Thanks.

-klint
i tried that it seems like it created a new folder %f instead of moving the files to its own folder. not sure where did it go wrong =/


Report •

Related Solutions

#4
February 21, 2009 at 00:34:52
@echo off & setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b/a-d file*') do (
move %%a %%~Na
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#5
February 23, 2009 at 02:01:17
If it has created a new folder with the name "%f" instead of creating a new folder with a name of "folder" followed by the number of each file, then you have written

set name=%f

instead of

set name=%%f


Report •

#6
February 23, 2009 at 14:14:28
M2:
I did it, but it didn't move the files to its folder. When I changed to copy instead of move, it copied the files under the same directory. Let me try to be more clear with I'm trying to do, maybe this will help you a bit:
Let me try to be more clear with what I'm trying to do, maybe this will help.
I have a z drive with a folder called "def" that conisists of the following folder names:
00065978
00060792
00065691
00064753
NP58964
NP45891
release_4589
release_2369
There are hundreds of folders in the def folder of the z drive.
In c drive, there is a folder called "data". This data folder consists of hundreds of files with the same name as the folders in the def folder of the z drive:
00065978.txt
00060792.txt
00065691.txt
00064753.txt
NP58964.txt
NP45891.txt
release_4589.txt
release_2369.txt
What I'm trying to do is move or copy those files into its own folder located in the z drive without typing the name because it will take a long time. So I was wondering if there is batch code that will move or copy those files in the c drive to its folder in the z drive.
Hope this explanation is more clear than before. Thanks M2.

klint
I tried it but it moved the files but I don't know where it moved to, but it somehow created this "f" file that I can't open =/. I tried to explain it better above, maybe it will help you understand what I'm trying to do. Thanks a lot.


Report •

#7
February 23, 2009 at 23:34:43
I think this is what you want.

@echo off
pushd G:\Test
for %%j in (*) do (
md "%%~nj"
move "%%j" "%%~nj"
)
popd
exit

You need to change the path. It will put everything into its own named folder including the batch so that is why the need to save in a different directory.
Will function without pushd if you want to put into the same directory.


Report •

#8
February 24, 2009 at 12:57:24
Thanks for the reply Shaka. This is getting really close to what I want. I tested out and it's working, but let me make it a bit clearer since I think i forgot to mention something.

The file names have a word in front such as test for example:
test_list00065978.txt
test_list_00060792.txt
test_list_00065691.txt
test_list_00064753.txt
test_list_NP58964.txt
test_list_NP45891.txt
test_list_release_4589.txt
test_list_release_2369.txt

The folder names have only the numbers in the end:
00065978.txt
00060792.txt
00065691.txt
00064753.txt
NP58964.txt
NP45891.txt
release_4589.txt
release_2369.txt

The code works pretty well, but is possible not to create its own folder since the folder already exists in the directory? I tried taking the "md" part out and the code didn't do anything.
Thanks again Shaka.


Report •

#9
February 24, 2009 at 14:33:32
Shaka
Basically, the code is doing more than required. What it's doing now is creating a folder with name of the file, then it moves the file into the created folder.

Since the folders for those files exist, I was wondering if you can just move those files
test_list00065978.txt
test_list_00060792.txt
test_list_00065691.txt
test_list_00064753.txt
test_list_NP58964.txt
test_list_NP45891.txt
test_list_release_4589.txt
test_list_release_2369.txt

to its folder, that is
00065978
00060792
00065691
00064753
NP58964
NP45891
release_4589
release_2369

Thanks.


Report •

#10
February 25, 2009 at 01:33:03
Sounds like you need a combination of Shaka's and my script, minus the md command. Something like the one below:
@echo off
setlocal enabledelayedexpansion
pushd G:\Test (CHANGE THIS LINE)
for %%j in (*) do (
   set name=%%~nj
   set name=!name:test_list_=!
   set name=!name:test_list=!
   if exist "!name!\" (
      move "%%j" "!name!"
   ) else (
      echo Warning: %%j not moved to !name!: folder does not exist.
   )
)
popd

Note that in your example, you are showing an inconsistent prefix. Sometimes, it is test_list_ and other times just test_list. I have therefore used two commands to remove both prefixes.


Report •

#11
February 25, 2009 at 06:25:02
Very good Klint. Works for me.

Report •

#12
February 25, 2009 at 11:31:38
it worked, klint! this really save me a lot of time. thank you.
again, thank you everyone for taking your time to help. i really really appreciate all your time =)

Report •

#13
February 25, 2009 at 13:27:24
one last question regarding this:
what if my files are in the c drive while my folders are in the z drive? is it possible to get those files from c drive to move to z drive?
thanks

Report •

#14
February 26, 2009 at 01:55:44
Just change the "move" command to something like this:

move "%%j" "Z:\parent\folder\etc\!name!"

(as well as the "if not exist" command above it.)


Report •

#15
February 26, 2009 at 10:54:34
thanks!

Report •


Ask Question