batch to move files with same names

June 2, 2009 at 00:59:19
Specs: Windows XP
hi!
i have a batch that moves pdf and txt files from one into another folder, if they have the same name...

so for example it would move:
aaa.pdf
bbb.pdf
ccc.pdf
aaa.txt
bbb.txt
ccc.txt

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

but now the situation has changed and the txt-file has not exactly the same name anymore as the pdf-file. now, the txt has an additional "_Word" - so that would make:

aaa.pdf
bbb.pdf
ccc.pdf
aaa_Word.txt
bbb_Word.txt
ccc_Word.txt

how do i have to adapt my batch, so that it still recognizes the same "prefixes"?

thanks so much for your help!!!!! :o)


See More: batch to move files with same names

Report •


#1
June 2, 2009 at 02:06:50
If the longer txt file names are consistent in layout, it could be hammered out.


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

M2


Report •

#2
June 2, 2009 at 02:08:33
well, the word BEHIND the original name (in my example "_Word") is consistent... but the name infront is hex, and can vary in length... and i don't know how to mid/trim it or hammer the text behind out ;)

Report •

#3
June 2, 2009 at 02:20:30
Are the 'old leading chars' consistent?

Post several real world filenames.


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

M2


Report •

Related Solutions

#4
June 2, 2009 at 02:28:20
first of all: million thanks for your time!!!

here some real examples:

a40a7a9b-492.pdf
870fc7c.pdf
dee70120.pdf
cbd0a85f0d15.pdf

a40a7a9b-492_Neusysteme.txt
870fc7c_Neusysteme.txt
dee70120_Neusysteme.txt
cbd0a85f0d15_Neusysteme.txt


Report •

#5
June 2, 2009 at 03:31:29
NOTES: As written, it doesn't MOVE anything. It previews what's to be done. [for safety] To activate, edit out the ECHO in fromt of MOVE.

Change the PUSHD line to suit where your files are.

===============================

@echo off & setLocal EnableDelayedExpansion

pushd x

for /f "tokens=* delims=" %%a in ('dir/b/a-d *.txt') do (
set textname=%%~Na
for /f "tokens=1 delims=_" %%i in ("!textname!") do (
if exist %%i.pdf (
echo move %%i.pdf
echo move %%a
)
)
)


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

M2


Report •

#6
June 2, 2009 at 05:20:01
great! It works for most of my files, thank you very much (after adding the path after move %%i.pdf C:\testfolder" to define where it should be moved to)

but he type of my file-names vary alot. so i just saw that a few (not many) contain a "_" in their name… so there would be files called b2a6a4b_37851.pdf - and of course with the underscore it won't work if there's also a file called b2a6a4b.pdf - which could be the case…

b2a6a4b_37851.pdf and b2a6a4b_37851_Neusysteme.txt b2a6a4b.pdf and b2a6a4b_Neusysteme.txt

any idea?
am really sorry for this underscore, i didn't realize it would matter before - as i have about 10 different structures... :o)


Report •

#7
June 2, 2009 at 05:38:08
I need to see a meaningful list; otherwise we're wasting time.


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

M2


Report •

#8
June 2, 2009 at 05:54:24
I understand... and am really sorry for this incomplete info I gave before.

So - about 90% have this structure:

a40a7a9b-492.pdf & a40a7a9b-492_Neusysteme.txt
870fc7c.pdf & 870fc7c_Neusysteme.txt
dee70120.pdf & dee70120_Neusysteme.txt
cbd0a85f0d15.pdf & cbd0a85f0d15_Neusysteme.txt

Other exceptions:
b2a6a4b_37851.pdf & b2a6a4b_37851_Neusysteme.txt
_b1358286e_24.02.2008.pdf & _b1358286e_24.02.2008_Neusysteme.txt
U18.320.34.21.pdf & U18.320.34.21_Neusysteme.txt
2009-02-17_11-56-51_369c092fb.pdf & 2009-02-17_11-56-51_369c092fb_Neusysteme.txt

What makes it difficult is, that it is possible that there are two pdf and two txt with nearly the same name, only the ending after the "_" differs. Like

b2a6a4b.pdf & b2a6a4b_Neusysteme.txt
and
b2a6a4b_37851.pdf & b2a6a4b_37851_Neusysteme.txt

And if the wrong combination of pdf und txt are moved, it's a real mess for the import that has to be done with them...

So the only regularity is the "_Neusysteme.txt" that is additional...

A huge THANK YOU you from mii!


Report •

#9
June 2, 2009 at 20:16:05
if you have Python on windows, here's an alternative solution
import os,glob,sys
directory = sys.argv[1]
destination=sys.argv[2]
os.chdir(directory)
for files in glob.glob("*.txt"):
    corresponding_file = '_'.join(files.split("_")[:-1]) + ".pdf"
    if os.path.exists(corresponding_file):        
        try:
            os.rename(files,os.path.join(destination,files))
            os.rename(corresponding_file,os.path.join(destination,corresponding_file))
        except Exception,e:
            print e
        else:
            print "Succesfully coped %s and %s to %s " % (corresponding_file,files,destination)
    else:
        print "Text File %s does not have corresponding pdf file: %s" %(files,corresponding_file)

usage:

C:\test\tmp>python test.py c:\test\tmp c:\tmp
Succesfully coped 2009-02-17_11-56-51_369c092fb.pdf and 2009-02-17_11-56-51_369c092fb_Neusysteme.txt to c:\tmp
Succesfully coped 870fc7c.pdf and 870fc7c_Neusysteme.txt to c:\tmp
Succesfully coped a40a7a9b-492.pdf and a40a7a9b-492_Neusysteme.txt to c:\tmp
Succesfully coped b2a6a4b_37851.pdf and b2a6a4b_37851_Neusysteme.txt to c:\tmp
Succesfully coped b2a6a4b.pdf and b2a6a4b_Neusysteme.txt to c:\tmp
Succesfully coped cbd0a85f0d15.pdf and cbd0a85f0d15_Neusysteme.txt to c:\tmp
Succesfully coped dee70120.pdf and dee70120_Neusysteme.txt to c:\tmp
Text File U18.320.34.21_Neusysteme.txt does not have corresponding pdf file: U18.320.34.21.pdf
Text File _b1358286e_24.02.2008_Neusysteme.txt does not have corresponding pdf file: _b1358286e_24.02.2008.pdf

first parameter to the script is source directory and 2nd parameter is destination.


Report •

#10
June 3, 2009 at 00:06:55
oh. thank you very much for your reply!
to be honest i have no idea how to use python and because i need to finish this work pretty urgently, i will have to use a normal batch-command.
but i guess i will download python to give it a try later. so thank you for the answer and inspiration ;)

Report •

#11
June 3, 2009 at 01:21:41

here is link to the executable of the above script. just one exe file. If you are in a hurry, just use it temporarily.

Report •

#12
June 3, 2009 at 01:28:47
well, i downloaded the test.exe but where do i enter the paths to test it? sorry - i really have nooo idea of python ;)

Report •

#13
June 3, 2009 at 01:38:27
well, you basically don't care whether its a Python script anymore..just treat it as a command. Put test.exe in some directory. eg c:\test

so if all your files are in c:\source directory, and then you want to move to c:\tmp for example, then on command line

c:\test> test.exe c:\source c:\tmp

that's all.


Report •

#14
June 3, 2009 at 01:53:14
wow, sounds really like a great thing - thank you for your time!
but it doesn't seem to work. i tried it all. i adapted the paths, i made folders and used the same command, i tried different directories, nothing seemed to work.
and even though it looks really easy i prefer the batch-version as i will need to probably adapt the ending myself... we'll be running it for different types of endings.

Report •

#15
June 3, 2009 at 02:02:42
well, no choice then...wait for the batch version. or else, just download Python and run the script.

Report •

#16
June 3, 2009 at 02:06:54
;) ok. but thank you anyway alot for your help! i at least learnt something and will try python and your exe when i have more time...

so... hopefully somebody will help me with the batch version... i just wish i knew this all myself :o)


Report •

#17
June 3, 2009 at 02:58:11
>> i just wish i knew this all myself

you don't just wish for something to happen. It won't happen unless you do something. You have to put in effort. Read up on how to do batch scripting, either by books or internet.


Report •

#18
June 3, 2009 at 04:03:30
yes, of course, you're right! i've been working through C:\WINDOWS\Help\ntcmds.chm::/ntcmds.htm and online-tutorials again and again. i just spent a whole evening yesterday trying to solve this batch-problem at home - with the help of similar examples (and didn't achieve).
but sometimes it's difficult if you don't have anybody to answer questions when you're stuck - that's why i am here ;)
i am for sure doing my best :o) but i guess i need more time!

Report •


Ask Question