topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 6:43 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Open files and rename or save as with new file name  (Read 18075 times)

KaysLover

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 26
    • View Profile
    • Donate to Member
Open files and rename or save as with new file name
« on: January 30, 2009, 06:49 AM »
I have a lot of PDF files called file001/002….. (100’s of files), all 1 page documents.

Is it possible for some code that would open selected files, extract the description from the next line after the word Remittance and save the file with that name? Please note that there is a date format dd/mm/yyyy on the same line which is not required as part of the file name.

(e.g. file001/002 opened, the descriptions extracted would be ‘This is the file name for file 001’ and ‘The file name for file 002’,

Therefore file named file001 to be renamed This is the file name for file 001, file named file 002 should be renamed The file name for file 002.

If the files cannot be renamed but can be saved as with the required file name, than this would suffice as well. I am quite happy to delete the files manually.

Any assistance offered would be appreciated.
Regards

KaysLover

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: Open files and rename or save as with new file name
« Reply #1 on: February 03, 2009, 09:36 AM »
I see what you're trying to accomplish although im not sure this way is the most efficient... Does this thread do anything for you: https://www.donation...ex.php?topic=16467.0 ? If not, let me know...
CodeByter.com - http://www.codebyter.com

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 3,199
    • View Profile
    • Donate to Member
Re: Open files and rename or save as with new file name
« Reply #2 on: February 03, 2009, 09:47 AM »
XPDF contains a command-line tool called PDFtoText that can do what it says, provided the PDFs aren't image scans, and that they aren't locked.  You might try it, with an AWK script or, if you're lucky, something like Horst Schaeffer's LMOD to extract the relevant text, and use a batch file to do the renaming.


mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Open files and rename or save as with new file name
« Reply #3 on: February 03, 2009, 03:24 PM »
I think this idea of a smart PDF file renamer is extremely useful, i've toyed around with writing it myself for a number of years but just never bit the bullet and i'd just as soon leave it for someone who is already familiar with working with pdf files.  I do think it's a great idea though.  Let us know if you find anything good.

rjbull's idea is not half-bad.. to combine a commandline text extractor with a renamer tool..

KaysLover

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 26
    • View Profile
    • Donate to Member
Re: Open files and rename or save as with new file name
« Reply #4 on: February 05, 2009, 10:17 AM »
Thanks for you input so far.

I have had a look rjbull recommendation, and not been too techie, I am finding it hard to work out how to do it.

If anyone can give further assistance of LMOD, then this would be appreciated.

As a short term measure, I am going into the PDFs extracting the required description, closing the file and renaming the file all manually.
Regards

KaysLover

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 3,199
    • View Profile
    • Donate to Member
Re: Open files and rename or save as with new file name
« Reply #5 on: February 07, 2009, 03:31 PM »
I have had a look rjbull recommendation, and not been too techie, I am finding it hard to work out how to do it.

If anyone can give further assistance of LMOD, then this would be appreciated.

Without knowing what your data looks like, e.g. a PDFtoText dump, and (very important) whether it's consistent, it's not really possible to give detailed help.

LMOD can, amongst other things, extract particular bits of text from particular lines, so if you know where your target text is, and it's always in the same place, you can use it to build a temporary batch file to do the renaming for you.



KaysLover

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 26
    • View Profile
    • Donate to Member
Re: Open files and rename or save as with new file name
« Reply #6 on: February 08, 2009, 08:10 AM »
The file is created by using Crystal reports.

The text that is required to be extract will always be on say line 5. There is a date at the end of line and therefore if there is a way to extract text from line 5. The delimiter should be say 3 continuous space.

How would LMOD input cards look like?
Regards

KaysLover

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 3,199
    • View Profile
    • Donate to Member
Re: Open files and rename or save as with new file name
« Reply #7 on: February 08, 2009, 03:28 PM »
Augh, Crystal Reports!   :(

Suppose you have a file temp.txt like this:

apple
banana
cherry
damson
Line 5; date is last item 2009-02-08
feijoa
greengage

Then to extract the last item on line 5 with LMOD, do this:

lmod /L5 [$!] < temp.txt

which gives as output:



C:\zdir>lmod /L5 [$!] < temp.txt
2009-02-08

C:\zdir>


KaysLover

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 26
    • View Profile
    • Donate to Member
Re: Open files and rename or save as with new file name
« Reply #8 on: February 09, 2009, 05:43 AM »
rjbull, thanks for your valuable input so far, however I have the following question:-

The fact that I use the code lmod /L5 [$!] < temp.txt, I have several temp files and if I run the command several time, will it not just override the file? I need to be able to append to the file.

Regards

KaysLover

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 3,199
    • View Profile
    • Donate to Member
Re: Open files and rename or save as with new file name
« Reply #9 on: February 09, 2009, 06:54 AM »
I have several temp files and if I run the command several time, will it not just override the file? I need to be able to append to the file.

In haste: you need an infusion of batch file magic - here you'd have to redirect the result to a file, but appending instead of overwriting, e.g.

lmod /L5 [$!] < temp.txt >> all.txt

instead of

lmod /L5 [$!] < temp.txt > all.txt

i.e. using double redirection symbol ">>" appends rather than overwrites.


KaysLover

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 26
    • View Profile
    • Donate to Member
Re: Open files and rename or save as with new file name
« Reply #10 on: February 09, 2009, 10:14 AM »
Thank U VERY MUCK, much appreciated.
Regards

KaysLover

skrommel

  • Fastest code in the west
  • Developer
  • Joined in 2005
  • ***
  • Posts: 933
    • View Profile
    • 1 Hour Software by skrommel
    • Donate to Member
Re: Open files and rename or save as with new file name
« Reply #11 on: February 21, 2009, 10:19 PM »
 :) Here's a similar tool which could easily be altered to suit your needs.

RenamePDF - Rename PDF files to the internal name found in the /Title tag.

Skrommel

;RenamePDF.ahk
; Rename PDF files to the internal name found in the /Title tag
;Skrommel @ 2008

recurse=0 ;change to 1 to recurse subfolders

#SingleInstance,Force
#NoEnv
SetBatchLines,-1

applicationname=RenamePDF

FileSelectFolder,source,%A_ScriptDir%,3,%applicationname%`nSelect where to look for PDF-files to rename

needle:="/Title(" ;the text to look for
hexneedle:=Asc2Hex(needle)

;Stolen from Lazlo and others at http://www.autohotkey.com/forum/topic25925-30.html

;;;;; Initialization of 2 machine code functions, for setting input and showing results
MCode(Hex2Bin,"568b74240c8a164684d2743b578b7c240c538ac2c0e806b109f6e98ac802cac0e10"
. "4880f8a164684d2741a8ac2c0e806b309f6eb80e20f02c20ac188078a16474684d275cd5b5f5ec3")
MCode(Bin2Hex,"8B54240C85D2568B7424087E3A53578B7C24148A07478AC8C0E90480F9090F97C3F"
. "6DB80E30702D980C330240F881E463C090F97C1F6D980E10702C880C130880E464A75CE5F5BC606005EC3")

Loop,%source%\*.pdf,0,%recurse%
{
  filename:=A_LoopFileLongPath
  BinRead(filename,F)  ; binary haystack
  Hex2Bin(H,hexneedle) ; binary search string
  QtBuf(Q,QLen,H,20)   ; Make RegEx quote
  foundpos:=RegExMatch(F,Q)-1
  If foundpos<0
    Continue
  BinRead(filename,F,255,foundpos+StrLen(needle))
  Loop,Parse,F,`n
  {
    StringTrimRight,foundtext,A_LoopField,1
    StringReplace,foundtext,foundtext,\,,All
    SplitPath,filename,name,dir,ext,name_no_ext,drive
    MsgBox,4,%applicationname%,Rename`n %filename%`nto`n %dir%\%foundtext%.pdf
    IfMsgBox,Yes
    {
      newname:=foundtext
      Loop
      {
        IfNotExist,%dir%\%newname%.pdf
          Break
        InputBox,newname,%applicationname%,File %foundtext% exists`nRename,,,,,,,,%newname%
        If ErrorLevel=1
          Break
      }
      FileMove,%filename%,%dir%\%newname%.pdf
      Break
    }
  }
}
Return


Asc2Hex(needle)
{
  hexneedle=
  SetFormat,Integer,Hex
  Loop,Parse,needle
  {
    char:=Asc(A_LoopField)
    StringMid,char,char,3,2
    hexneedle:=hexneedle . char
  }
  SetFormat,Integer,Dec
  Return,%hexneedle%
}


QtBuf(ByRef Q, ByRef QLen, ByRef B, BLen) { ; replace each \E with \E\E\Q, enclose in \E..\Q
   RegExReplace(B, "\\E","",n)
   QLen := BLen+4*n+4
   VarSetCapacity(Q,QLen,1)
   If (n > 0) {
      VarSetCapacity(C,QLen-4,1)
      C := RegExReplace(B, "\\E", "\E\E\Q") ; AHK copies result over \0's
      Q := RegExReplace(C, ".*", "\Q$0\E")  ; enclose result in \E..\Q
   } Else
      Q := RegExReplace(B, ".*", "\Q$0\E")
}


BinRead(file, ByRef data, n=0, offset=0)  {  ; n = #bytes/all; neg offset from end
   h := DllCall("CreateFile",Str,file, UInt,0x80000000, UInt,3, UInt,0, UInt,3, UInt,0, UInt,0)
   m := 2*(offset < 0)                       ; offset >= 0 : m = 0; offset < 0: m = 2
   DllCall("SetFilePointerEx",UInt,h, Int64,offset, UIntP,U, Int,m)
   m := DllCall("GetFileSize",UInt,h, Int64P,r)
   If n not between 1 and %m%
      n = %m%
   VarSetCapacity(data, n, 1)
   DllCall("ReadFile",UInt,h, UInt,&data, UInt,n, UIntP,r, UInt,0)
   DllCall("CloseHandle", UInt,h)
   Return r
}


Bin2Hex(addr,len) {       ; convert binary data at addr to hex stream
   Local hex
   VarSetCapacity(hex,2*len+1)
   Dllcall(&Bin2Hex, "uint",&hex, "uint",addr, "uint",len, "cdecl")
   VarSetCapacity(hex,-1) ; update StrLen
   Return hex
}


Hex2Bin(ByRef buf, hex) { ; convert hex stream to binary data
   Global Hex2Bin
   VarSetCapacity(buf,(StrLen(hex)+1)//2,1)
   Dllcall(&Hex2Bin, "uint",&buf, "uint",&hex, "cdecl")
}


MCode(ByRef code, hex) {  ; allocate memory and write Machine Code there
   VarSetCapacity(code,StrLen(hex)//2)
   Loop % StrLen(hex)//2
      NumPut("0x" . SubStr(hex,2*A_Index-1,2), code, A_Index-1, "Char")
}

KaysLover

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 26
    • View Profile
    • Donate to Member
Re: Open files and rename or save as with new file name
« Reply #12 on: February 23, 2009, 05:12 AM »
skrommel,

Thanks for your valuable suggestion.

I have found a utility called View and renamer which opens up the files you specify, you input a new name, and it then creates renames the old file to the new file name. This is a bit more long winded, however it meets my requirements. The link to the page is :-

http://www.neosoftwa...php?cid=8&lid=68

Thanks
Regards

KaysLover