move files with same name

April 30, 2009 at 09:50:55
Specs: Windows XP
hi there

i'm only learning to write batches and have a problem with one that i can't get to run as i want it to:

i have 2 folders: C:\Folder1 and C:\Folder2

in Folder1 there is one *.txt and lots of *.pdf-files.

one of all these pdf-files has the same name as the txt-file. (aaa.txt, aaa.pdf, bbb.pdf, ccc.pdf)

all i need to do is move the txt-file (aaa.txt) and the pfd-file with the SAME NAME (in this case aaa.pdf) from Folder1 to the Folder2.

i know the name of the folders, but the name of the files change all the time. how can i handle this?

thank you for any help - it is always greatly apreciated!!!!

;) mii


See More: move files with same name

Report •


#1
April 30, 2009 at 17:44:32
:: move pdf w same name as txt

@echo off & setLocal EnableDelayedExpansion

pushd c:\folder1

for /f "tokens=* delims=" %%a in ('dir/b *.txt') do (
move %%a c:\folder2
move %%~Na.pdf c:\folder2
)


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

M2


Report •

#2
May 3, 2009 at 13:43:45
wow. brilliant. thank you very very much for the fast and very useful reply! i really apreciate it a lot!

of course i tried the batch and it works in my test....

but as soon as i put a second *.txt-file into the folder1 it moves that one with the other txt-file. so it would move aaa.txt, aaa.pdf AND xyz.txt. i would only want it to move one txt and one pdf, both with the same name. i tried a few things but nothing worked :(

our situation will be, that we have a bunch of pdf-files in a folder1. then we "generate" txt-files and move them - one by one - to folder1. then they need to be moved with "their" pdf file, two by two, to folder2, so that they both can be imported into an archive at the same time...

hmmm. sorry for the follow-up. maybe you know a solution. if yes, it would be a huge help ;)


Report •

#3
May 3, 2009 at 18:48:17
If there's more than one txt how do you decide which to move?


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

M2


Report •

Related Solutions

#4
May 4, 2009 at 01:06:34
only the txt-file that has a matching pdf-file - with the exact same name. theoretically it shouldn't happen that there is a txt-file in folder1 without a matching pdf, but if there is an error... it could happen.

so: if folder1 contains

test_1.txt, test_1.pdf, test_2.txt, test_2.pdf, test_3.txt

it should move them all EXCEPT the test_3.txt because that one has no matching pdf. at the moment it would move them all (except the not-matching pdf-files).

i mean, we can use it as it is, but if we could stop the not-matching-txt-files to be moved it would be the deluxe-version ;)


Report •

#5
May 4, 2009 at 03:07:29
@echo off & setLocal EnableDelayedExpansion

pushd c:\folder1

for /f "tokens=* delims=" %%a in ('dir/b *.txt') do (
if exist %%~Na.pdf (
move %%a c:\folder2
move %%~Na.pdf c:\folder2
)
)


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

M2


Report •

#6
May 4, 2009 at 04:59:20
YEEES, exactly this i was looking for!!! thank you so much! :)

Report •


Ask Question