AutoHotkey Community

It is currently May 27th, 2012, 6:21 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 27 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: October 9th, 2007, 2:53 pm 
Offline

Joined: May 15th, 2007, 8:59 pm
Posts: 169
I figured it out. I was running this script from my Desktop which I have no doubt the path caused the problem. "C:\Documents and Settings\David\Desktop\xlsconvert\" I have moved it to a directory in my HD's root folder and it ran just fine "C:\xlsconvert\.

I also found out the file names can not have a space him them either.

Is there anyways I can set it up to just convert the first worksheet?, and remove the file after it has finished converting it?

This is the naming procedure I am shooting for: Goes in as test.xls, comes out as test.dat (CSV formated)

Thx for your time.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 9th, 2007, 3:37 pm 
Offline

Joined: May 15th, 2007, 8:59 pm
Posts: 169
With the help of a friend (more fluent in VB than I) I was able to modify the script to perform the exact conversion I needed.

This modified version of the script will take the original file (test.xls) and convert it into a CSV formated file (test.dat) with the custom extention change of .dat. It will process all XLS files this way then delete the left over XLS files.

Code:
   #SingleInstance force
   
   chosen_folder = %A_ScriptDir%
   ConvertXlsCsv(chosen_folder)
   
   Exitapp

ConvertXlsCsv(folder="") {
   { ;FOLDER
      If folder =
      {
         FileSelectFolder, folder, , , Folder with xls files
         If ErrorLevel = 1
            Return
         IfNotExist %folder%
            Return
      }
   }
   { ;LIST FILES TO BE CONVERTED
      Loop, %folder%\*.xls
         files=%files%`n%A_LoopFileName%
      ;MsgBox, 0, Files to be converted, %files%, 3
   }
   { ;VBS CODE
      ;ORIGINAL VBS CODE FROM Jimmy The Fish
      ;http://forums.xaprief.com/showthread.php?s=&threadid=3159   
      vbs_code =
      (
      option explicit

      dim a, arg, oArgs, ArgNum
      a = 0

      Set oArgs = WSCript.arguments
      ArgNum = oArgs.Count

      if ArgNum <> 1 then
         WSCript.echo "Syntax: cscript <Script> filename_root"
         WScript.quit(1)
      end if

      dim filename_root, oldfilename, pos,newname
      oldfilename = oArgs(0)

      rem Remove .xls file extension if it was provided
      dim ext
      ext = right( oldfilename, 4 )
      if lcase( ext ) = ".xls" then
       oldfilename = left( oldfilename, len( oldfilename ) - 4 )
      end if

      pos = instr(oldfilename, " ")

      if pos > 0 then

       newname = right(oldfilename, len(oldfilename) - instrrev(oldfilename, "\"))
       newname = replace(newname," ","_")
       filename_root = left(oldfilename, instrrev(oldfilename, "\")) & newname

       dim oShell
       set oShell = Wscript.CreateObject("WScript.Shell")
       Wscript.echo "cmd.exe /c ren """ & oldfilename & ".xls"" " & newname & ".xls"
       oShell.run "cmd.exe /c ren """ & oldfilename & ".xls"" " & newname & ".xls"

      else
       filename_root = oldfilename
      end if

      dim app
      set app = createobject("Excel.Application")
         
      dim wb
      set wb = app.workbooks.open( filename_root & ".xls" )

      const xlXMLSpreadsheet = 46
      const xlCSV = 6

      app.DisplayAlerts = false
      dim sht
      for each sht in wb.worksheets
       sht.activate
       dim output_filename
       'output_filename = filename_root & "_" & replace( sht.name, " ", "_" ) & ".csv"
      '--------------------------------------------------------------------------------------
      ' ----------MODIFIED to save to with original file name but use.dat extention----------
      '--------------------------------------------------------------------------------------
      output_filename = filename_root & ".dat"
       wb.saveAs output_filename, xlCSV
      '--------------------------------------------------------------------------------------
      ' ---------------------Break stops script after first workbook-------------------------
      '--------------------------------------------------------------------------------------
      'break
      app.DisplayAlerts = true
      wb.close false
      WScript.quit
      next
      'wb.saveAs filename_root & ".xml", xlXMLSpreadsheet
      app.DisplayAlerts = true

      wb.close false

      'app.close

      WScript.quit
      )   
      FileDelete, %folder%\xls2csv.vbs
      FileAppend, %vbs_code%, %folder%\xls2csv.vbs
   }
   { ;BATCH FILE
      FileDelete, %folder%\xls2csv.bat
      FileAppend, for `%`%i in (*.xls) do cscript xls2csv.vbs `%`%~dpni > nul , %folder%\xls2csv.bat
   }
   { ;RUN VBS FILE THROUGH BAT
      RunWait, %folder%\xls2csv.bat, %folder%
   }
   { ;DELETE TEMP FILES
      FileDelete, %folder%\xls2csv.vbs
      FileDelete, %folder%\xls2csv.bat
   }
      { ;WipeXLS files
      FileDelete, %folder%\*.xls

   }
   { ;CONVERT INTO PORTUGUESE FORMAT (OPTIONAL)
      If A_Language = 0416
      {
         ConvertCsvSemiColon(folder)
         MsgBox, 0, Arquivos convertidos em CSV português, Verifique a pasta original, 15
         Return
      }
   }
   ;MsgBox, 0, Files converted, Please check original folder, 15
}
ConvertCsvSemiColon(folder) {
   Loop, %folder%\*.csv
   {
      ;REWRITE ONLY FILES MODIFIED IN THE LAST 5 MINUTES
         FileGetTime, file_time, %A_LoopFileName%, M
         minutes_ago = %A_Now%
         minutes_ago += -5, Minutes
         If file_time < %minutes_ago%
            Continue
      ;READ CONTENT
         FileRead, content, %A_LoopFileName%
      ;REPLACE COMMAS ETC
         StringReplace, content, content, `,, `;, all
         StringReplace, content, content, `., `,, all
      ;REWRITE CSV
         FileDelete, %A_LoopFileName%
         FileAppend, %content%, %A_LoopFileName%
   }
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 11th, 2007, 3:13 am 
Offline

Joined: October 5th, 2004, 5:03 pm
Posts: 121
Location: Brasília, Brazil
icefreez wrote:
I am unable to get this script to convert an XLS to a CSV file. It shows "Files to be converted, runs the bat file, then says "Files Converted" "Please checked the original folder" The file remains unchanged in the scripts directory, and no new files have been created.

Any suggestions what is going wrong?


Try to use:

#SingleInstance force
chosen_folder =
ConvertXlsCsv(chosen_folder)
Exitapp


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 21st, 2008, 6:23 pm 
Offline

Joined: March 19th, 2007, 12:43 am
Posts: 532
Any ideas on how do i make it work with spaces in file path/name?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 21st, 2008, 10:39 pm 
Offline

Joined: October 5th, 2004, 5:03 pm
Posts: 121
Location: Brasília, Brazil
Sorry... I don't thing it's possible with spaces...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 21st, 2008, 11:38 pm 
Offline

Joined: June 26th, 2006, 6:14 pm
Posts: 1379
Location: USA
can you use the short path instead of the full path?
Code:
MsgBox % GetShortName("C:\Documents and Settings\FooUser\Really Long Path With Spaces\temp.ahk")

GetShortName(File) {
   Loop, %File%
      Return A_LoopFileShortPath
   }

_________________
Image
ʞɔпɟ əɥʇ ʇɐɥʍ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 22nd, 2008, 12:26 am 
I don't think that the VB script would word...


Report this post
Top
  
Reply with quote  
PostPosted: November 26th, 2008, 9:24 am 
Does anyone know how to get rid of the confirmation window?

I call xls2csv from a batch file, and after i have converted the file, then I got a confirmation window. I want to automatically confirm this?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: July 17th, 2009, 4:50 pm 
I have the problem icefreez had earlier. I get the window stating files to be converted but then no new files. Am I missing something? It is not a path issue as it identifies the *.xls files to be converted.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: July 17th, 2009, 5:03 pm 
nevermind, i see that the vbscript portion has to be run from the root directory. now i understand what icefrez meant. is there any way to have this run properly in the sub directory that ahk identifies in ahk script?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: July 17th, 2009, 7:56 pm 
without knowing vbscript:
to do a file with spaces, save the filename in a string, rename to temp.xls, do the conversion, then rename csv and xls back to the string name


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: December 9th, 2010, 3:41 pm 
hot, thx

u saved my ass


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 27 posts ]  Go to page Previous  1, 2

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group