Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

:: 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

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 ;)

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

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 ;)

@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

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |