Computing.Net > Forums > Programming > batch to move files with same names

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

batch to move files with same names

Reply to Message Icon

Name: mii
Date: June 2, 2009 at 00:59:19 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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)



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: June 2, 2009 at 02:06:50 Pacific
Reply:

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


0

Response Number 2
Name: mii
Date: June 2, 2009 at 02:08:33 Pacific
Reply:

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


0

Response Number 3
Name: Mechanix2Go
Date: June 2, 2009 at 02:20:30 Pacific
Reply:

Are the 'old leading chars' consistent?

Post several real world filenames.


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

M2


0

Response Number 4
Name: mii
Date: June 2, 2009 at 02:28:20 Pacific
Reply:

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


0

Response Number 5
Name: Mechanix2Go
Date: June 2, 2009 at 03:31:29 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: mii
Date: June 2, 2009 at 05:20:01 Pacific
Reply:

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)


0

Response Number 7
Name: Mechanix2Go
Date: June 2, 2009 at 05:38:08 Pacific
Reply:

I need to see a meaningful list; otherwise we're wasting time.


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

M2


0

Response Number 8
Name: mii
Date: June 2, 2009 at 05:54:24 Pacific
Reply:

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!


0

Response Number 9
Name: ghostdog
Date: June 2, 2009 at 20:16:05 Pacific
Reply:

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.


0

Response Number 10
Name: mii
Date: June 3, 2009 at 00:06:55 Pacific
Reply:

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


0

Response Number 11
Name: ghostdog
Date: June 3, 2009 at 01:21:41 Pacific
Reply:


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


0

Response Number 12
Name: mii
Date: June 3, 2009 at 01:28:47 Pacific
Reply:

well, i downloaded the test.exe but where do i enter the paths to test it? sorry - i really have nooo idea of python ;)


0

Response Number 13
Name: ghostdog
Date: June 3, 2009 at 01:38:27 Pacific
Reply:

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.


0

Response Number 14
Name: mii
Date: June 3, 2009 at 01:53:14 Pacific
Reply:

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.


0

Response Number 15
Name: ghostdog
Date: June 3, 2009 at 02:02:42 Pacific
Reply:

well, no choice then...wait for the batch version. or else, just download Python and run the script.


0

Response Number 16
Name: mii
Date: June 3, 2009 at 02:06:54 Pacific
Reply:

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


0

Response Number 17
Name: ghostdog
Date: June 3, 2009 at 02:58:11 Pacific
Reply:

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


0

Response Number 18
Name: mii
Date: June 3, 2009 at 04:03:30 Pacific
Reply:

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!


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: batch to move files with same names

move files with same name www.computing.net/answers/programming/move-files-with-same-name/18856.html

Batch to move file by sysdate www.computing.net/answers/programming/batch-to-move-file-by-sysdate/14939.html

Batch to move files www.computing.net/answers/programming/batch-to-move-files/15936.html