PDA

View Full Version : simple batch script needed


jsag
07-30-2010, 06:52 PM
Sorry if this is in the wrong section, and I searched and tried things for hours before posting.

I have hundreds of .txt files named eg:

013929.txt
145678a.txt etc etc

Inside each text file is like this eg:
QKAE6K
Full Name Of Item

blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah
etc


I need to write a script that can open each file in the folder and take the first line EG: QKAE6K
and then add a minus and then add the second line of text EG:

So the above example would look like...
QKAE6K-Full Name Of Item

Then rename the text file to that EG QKAE6K-Full Name Of Item.txt

If anyone can help, please help a n00b out. I've tried for a day now editing scripts I find online and have just rage binned them to sulk at how stupid I feel.

I'm sure this must be simple to someone reading, please help.

I will try again searching, reading and trying and if I get it working I'll re-edit this post.

edttoma
08-24-2010, 08:34 PM
Hi,

This probably does not cover all the logic for copying the files and there is no error handling!

Save script as as a VBscript file (.vbs) and execute.

The script will read all .txt files in c:\temp\ and create new slightly modified files in c:\temp\renamed

Cheers
Tomas


Dim fso, Folder, FileObj , InFile , OutFile, LineIn1 ,LineIn2 ,LineOut
Const ForReading = 1, ForWriting = 2
Const InFolder = "C:\TEMP\" , OutFolder = "C:\TEMP\RENAMED\"
' Create file system object
Set fso = CreateObject("Scripting.FileSystemObject")
' Point to input folder
Set Folder = fso.GetFolder(InFolder)
' Get the TXT files in input folder
For Each FileObj In Folder.Files
If UCase(Right(FileObj.Name,4))=".TXT" Then
' So we found a file. Open .txt for reading
Set InFile = fso.OpenTextFile(InFolder & FileObj.Name, ForReading)
' Read first two lines LineIn1 = InFile.ReadLine
LineIn2 = InFile.ReadLine
'Create new first line
LineOut = "EG " & LineIn1 & "-" & LineIn2
' Create the outputfile name from new first line
Set Outfile = fso.CreateTextFile(OutFolder & LineOut & ".txt", True)
' Write first line
OutFile.WriteLine LineOut
'Copy rest of lines from input file to output file
Do While InFile.AtEndOfStream <> True
LineIn1 = InFile.ReadLine
OutFile.WriteLine LineIn1
Loop
' Close files
OutFile.Close
Set OutFile =Nothing
InFile.Close
Set InFile =Nothing
End If
Next
' Clean up
Set Folder = Nothing
Set fso = Nothing