1.

Solve : manipulating files?

Answer»

Hi All!!

I have multiple files in a folder! I have to search in these files and, depending on the content of them, save them in specific folders.

I need HELP creating a batch file to perform this task.

Thanks for the help.so depending on what kind of string of words are inside the file, send to another folder? brain deadAn example:

I have a folder (c:\receive) with many TXT files.
Each file has an indefinite sequence of characters.
In the FIRST line of each file is a sequence (string) of 3 characters that identify the type of information.
Depending on this information the file should be moved to another folder.

As below:

- c:\receive\file1.txt -> have the string "ABF" -> move to c:\folder1;

- c:\receive \file2.txt -> have the string "CZ7" -> move to folder c:\folder2;

Thanks for the first post.if you have Python for windows, here's an alternative solution
Code: [Select]import os,glob
currentdir = os.path.join("c:\\","test") # directory c:\test where TXT files are
os.chdir(currentdir)
for file in glob.glob("*.txt"):
firstline = open(file).readline().strip()
if firstline == "ABF":
destination = os.path.join("c:\\","folder1")
elif firstline == "C27":
destination = os.path.join("c:\\","folder2")
os.rename(file, os.path.join(destination,file) #move to specific folder

thank you for the post.
I try to download the python for windows and do the test.
I answer if a error occurs or if it right.when I try to run the code, the error is below:

Code: [Select]>>> import os,glob
>>> currentdir = os.path.join("d:\\","receb")
>>> os.chdir(currentdir)
>>> for file in glob.glob("*.ekt"):
... firstline = open(file).readline().strip()
... if firstline == "BFT":
... destination = os.path.join("d:\\","prod")
... elif firstline == "MZ7":
Traceback ( File "<interactive input>", line 5
elif firstline == "MZ7":
^
SyntaxError: invalid syntax

This is my first time on Python! I looked at the documentation and could not be found.
Please check and return.you must properly indent your Python code. indentation is very important in Python if not, your script won't run. also, what you are using is the INTERPRETER where you type in your commands one by one.. its one way to test your code, however for this case, please put the code in a file, save it as whatever name you want to give. eg myscript.py , then on the command line, type
Code: [Select]c:\test> python myscript.py

you must indent your if / else statement properly
Code: [Select]....
if firstline == "BFT":
#do something
elif firstline == "M27": <------ this line align properly with the previous "if"
elif firstline == "M27": <------ this is wrong...

please look at the Python documentation SITE, especially the tutorial for Python beginnerYes, unlike every other language python doesn't ignore most whitespace, and instead takes the thoughtful approach and makes it part of the syntax. confusing at first, but it makes for more readable code in the end, I would imagine.Quote from: BC_Programmer on June 03, 2009, 06:16:10 PM

but it makes for more readable code in the end, I would imagine.
you are right. especially if you have a large project. also, Python doesn't use unnecessary symbols like $,% or open and close braces for if/else, loops...thereby increasing readability of code.Hi all,

Thank you for your help.

Finally I got the script to run correctly and without error.
Below the final script:

Code: [Select]import os,glob
currentdir = os.path.join("d:\\","receb")
os.chdir(currentdir)
for file in glob.glob("*.ekt"):
firstline = open(file).readline().strip()
text = firstline.find("BFT")
text2 = firstline.find("MZ7")
text3 = firstline.find("PEA")
if text >= 0:
destination = os.path.join("d:\\","prod")
elif text2 >= 0:
destination = os.path.join("d:\\","mp")
elif text3 >= 0:
destination = os.path.join("d:\\","pea")
os.rename(file, os.path.join(destination,file))
text = 0
text2 = 0
text3 = 0

I will devote even more to learn this language.
Thank you very much even.Quote from: lchbusch on June 04, 2009, 09:12:01 AM
Hi all,

Thank you for your help.

Finally I got the script to run correctly and without error.
Below the final script:

Code: [Select]import os,glob
currentdir = os.path.join("d:\\","receb")
os.chdir(currentdir)
for file in glob.glob("*.ekt"):
firstline = open(file).readline().strip()
text = firstline.find("BFT")
text2 = firstline.find("MZ7")
text3 = firstline.find("PEA")
if text >= 0:
destination = os.path.join("d:\\","prod")
elif text2 >= 0:
destination = os.path.join("d:\\","mp")
elif text3 >= 0:
destination = os.path.join("d:\\","pea")
os.rename(file, os.path.join(destination,file))
text = 0
text2 = 0
text3 = 0

I will devote even more to learn this language.
Thank you very much even.

good for you... some "enhancement" to your script.
1) don't have to use find anymore.. you can use "in" operator
2) next time ,you can use a dictionary to store. eg
Code: [Select]paths = {
"PEA" : os.path.join("d:\\","pea"),
"MZ7" : os.path.....
}

final code can be shortened:
Code: [Select]import os,glob
currentdir = os.path.join("d:\\","receb")
os.chdir(currentdir)
for file in glob.glob("*.ekt"):
firstline = open(file).readline().strip()
if "BFT" in line:
destination = os.path.join("d:\\","prod")
elif "MZ7" in line:
destination = os.path.join("d:\\","mp")
elif "PEA" in line:
destination = os.path.join("d:\\","pea")
os.rename(file, os.path.join(destination,file))

Dear gh0std0g74,

One more time, thanks a lot!!

I CHANGED the script as its your indication!

I'll see how that dictionaries are used to implement in a new opportunity.



Discussion

No Comment Found