Can a Batch File Tell a program to save a file as?

2019-03-06 01:33发布

问题:

I am currently trying to automate a file transfer process for work and I am not sure how to have a program save a file.

Currently I have created a working batch to move a log file from the point of creation to the correct folder and rename it to include the date. Then use the start function to open that file. All of this works so far.

Now I need that file which was opened to go File>save as>save

It doesn't need the name changed. The program that opens it only saves as CVS and that is my ultimate goal.

Unless someone knows an easier way to change a log file to a CSV file, I am open to that option as well. In a batch file.

Windows 7 computer

EDIT 1*

Renaming it to a csv using the ren command turns the data to garbage when it opens in excel. Going through the program to make the log a csv makes the data appear in the correct format. I need to know how to tell the program to save as. Since there is no shortcut I think I need a way to tell it to push: altF down down (arrow keys) enter enter

I realize that programs like autoit are capable of preforming the function but they are not going to be an option due to restraints with company computers.

Further Research lead me to this post: Press Keyboard keys using a batch file But I am unable to find a way to make this idea work for my case. Maybe it will give someone to go off of though that understands it more.

EDIT 2* This looks like it might get ugly but if anyone understands this as well it could prove very useful for what I need. https://msdn.microsoft.com/en-us/library/8c6yea83.aspx

EDIT 3* Sorry for the broad question but thanks for your assistance.

The BAT so far is pretty simple:

Move /-y "C:\Users\xxx\Desktop\ASI Test Folder\Station 5\Station 5 Log\Station 5 Log.log" "O:\LAB\ASI\Logs by Date\Week 2015-4-13"

ren "O:\LAB\ASI\Logs by Date\Week 2015-4-13\Station 5 Log.log" "Station 5 Log %date:~4,2%-%date:~7,2%-%date:~10,4%.log"

start "" "C:\Users\xxx\Desktop\ASI Test Folder\Station 5\Station 5 Log\Station 5 log.log

pause


I am trying to send keys to RSView to have it save the program. This will make it a CSV that can be opened as an EXCEL doc where I already created a macro that converts that data into a more readable/chartable format.

If We can save this as a CSV I can use the start command again to open it in excel then run the macro.

回答1:

You should be able to use command line switches to do this, instead of ever opening the file. http://plcview.blogspot.com/2012/03/rsview-enterprise-file-viewer-utility.html

"RSView Enterprise File Viewer.exe" /md process.log process.csv



回答2:

After much research I was able to solve my problem.

The simple answer to my original question: Can a Batch file tell a program to save as? Is yes/sort of.

While a batch cannot directly send key commands to another program in the traditional sense there are work arounds. It is possible to include multiple scripting languages in the same batch file if done appropriately you can use either java or vbs language in your batch to send keys to a program. In order to save as you need to select active window, then send key commands that would get to the save as function. Example: Alt+F Down Down Enter Enter. That would tell the program to open the file menu, go down the appropriate number of times to select save as then hit enter to tell it to open the save as menu then enter again would tell it to save.

If you are not very good with the scripting part, like me. Then it will probably be easier to create a batch file that does all the moving/renaming/opening and use wsf files to send keys. A wsf file is very similar to a batch file but it is written in java or vbs. It is less complicated than adding in both vbs and normal script to the batch. So here is how I solved my problem. Three Files: One Batch that told wsf files to open when I needed to send keys Two wsf files for the two separate times I needed to send keys to a program. (You need one wsf per program you open)

Here are the codes for each file:

Batch:

    Move /-y "C:\Users\xxxx\Desktop\ASI Test Folder\Station 5\Station 5 Log\Station 5 Log.log" "O:\LAB\ASI\Logs by Date\Week 2015-4-13"
    timeout /t 1
    ren "O:\LAB\ASI\Logs by Date\Week 2015-4-13\Station 5 Log.log" "Station 5 Log %date:~4,2%-%date:~7,2%-%date:~10,4%.log"
    timeout /t 1

    start "" "O:\LAB\ASI\Logs by Date\Week 2015-4-13\Station 5 log %date:~4,2%-%date:~7,2%-%date:~10,4%.log
    timeout /t 1

    start "" "C:\Users\xxxx\Desktop\Working BATS\Send Keys test.wsf"
    timeout /t 5

    start "" "O:\LAB\ASI\Logs by Date\Week 2015-4-13\Station 5 Log %date:~4,2%-%date:~7,2%-%date:~10,4%.csv
    timeout /t 4

    start "" "C:\Users\xxxx\Desktop\Working BATS\Excel Macro.wsf
    pause

Pretty basic stuff. Timeout function was used to account for the delay when opening files. While it wasn't necessary for moving/renaming every time, sometimes actions wouldn't be complete when the next one went through. So this made sure it worked every time. WSF (Send Keys test):

    <package>
    <job id="vbs">
    <script language="VBScript">
 set WshShell = WScript.CreateObject("WScript.Shell")
 WshShell.AppActivate "RSView Enterprise File Viewer"
     WScript.Sleep 100
     WshShell.SendKeys "%f"
     WScript.Sleep 500
     WshShell.SendKeys "{Down}"
     WScript.Sleep 500
     WshShell.SendKeys "{Down}"
     WScript.Sleep 500
 WshShell.SendKeys "{Enter}"
     WScript.Sleep 500
     WshShell.SendKeys "{Enter}"
     WScript.Sleep 500
 </script>
     </job>
     </package>

The app active window is very important. It makes sure the keys are sent to the correct window. This is the file used to have RSView save the log file as a CSV.

WSF2 (Excel Macro):

    <package>
    <job id="vbs">
    <script language="VBScript">
 set WshShell = WScript.CreateObject("WScript.Shell")
 WshShell.AppActivate "Excel"
     WScript.Sleep 100
     WshShell.SendKeys "^u"
 </script>
     </job>
     </package>

Hope this helps anyone who runs into a similar problem as I did. The key here for me was figuring out how to send keys to a program.