1.

Solve : batch script move file based on name?

Answer»

Hello,

I WOULD like to create a script (in batch if possible) to move file from my desktop to my server based on it filename it will place it in the correct folder. I have nearly no knowing of any programming language, only very basic batch scripting.

So for example I have [php]Webserver-03-09[23D12A].php on a shared folder on my desktop, so the script should move the file to the D:\backup\Web server folder on the server.
Another example is that filename could be like [flash]frontpage_animation_03_09[45D5F2].swf to be moved to a folder named frontpage animation.

I know the easy way would be to rename the destination folder but the script is primary made for me to get a little better with batch programming.

So the first thing for the script is to remove [] and is content :
I found this : http://archives.devshed.com/forums/ms-dos-127/remove-underscore-from-200-files-1205654.html

for /f "tokens=1* delims=_" %i in ('dir /b/a:-d ') do ECHO "%i_%j" T "%i%j"

But actually I have just tested this command but it doesn t seem to work to remove a underscore... another thing is where is %j coming from ?


if you have Python on Windows and the following assumptions:
1) assuming you can map to your server
2) assuming all file start with [

Code: [Select]import os,glob,sys,re
pat=re.compile("(\[.*?\])")
source = sys.argv[1]
destination = sys.argv[2]
os.chdir(source)
for files in glob.glob("[*"):
newfilename = pat.sub("",files)
if "_" in newfilename:
newfilename = newfilename.split("_")
elif "-" in newfilename:
newfilename = newfilename.split("-")
newpath = os.path.join(destination , ' '.join(newfilename[:-2]))
if not os.path.exists(newpath):
os.mkdir(newpath)
try:
os.rename(os.path.join(source,files),os.path.join(newpath,files))
except EXCEPTION,e:
print e
else:
print "Move %s to %s successful." %( files, destination)
usage: c:\test> python test.py

Here's an executable you can use in your batch
eg
Code: [Select]C:\test>dir
Directory of C:\test

06/12/2009 11:22 PM <DIR> .
06/12/2009 11:22 PM <DIR> ..
06/12/2009 11:18 PM 1,613,993 test.exe
05/02/2009 04:52 PM 29 [flash]frontpage_animation_03_09[45D5F2].swf
06/03/2009 04:29 PM 137 [php]Webserver-03-09[23D12A].php


C:\test>
C:\test>test.exe c:\test c:\tmp
Move [flash]frontpage_animation_03_09[45D5F2].swf to c:\tmp successful.
Move [php]Webserver-03-09[23D12A].php to c:\tmp successful.

C:\test>dir /s c:\tmp\

Directory of c:\tmp

06/12/2009 11:22 PM <DIR> .
06/12/2009 11:22 PM <DIR> ..
06/12/2009 11:22 PM <DIR> frontpage animation
06/12/2009 11:22 PM <DIR> Webserver
0 File(s) 0 bytes

Directory of c:\tmp\frontpage animation

06/12/2009 11:22 PM <DIR> .
06/12/2009 11:22 PM <DIR> ..
05/02/2009 04:52 PM 29 [flash]frontpage_animation_03_09[45D5F2].swf
1 File(s) 29 bytes

Directory of c:\tmp\Webserver

06/12/2009 11:22 PM <DIR> .
06/12/2009 11:22 PM <DIR> ..
06/03/2009 04:29 PM 137 [php]Webserver-03-09[23D12A].php
1 File(s) 137 bytes
Thanks a lot gh0std0g74 ! Generally all the files are beginning with [ but it is not ALWAYS the case, but in this case i ll do that manually. Actually I was planning to learn python so it is a great way to get more in this language.



Discussion

No Comment Found