Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch script - run command on each file in directory

I need to convert some xls files into xlsx files. I can successfully convert one xls file into xlsx by running this command into cmd prompt (windows):

ssconvert inputFileName.xls outputFileName.xlsx 

(ssconvert is a Gnumeric's command-line utility that can convert between different spreadsheet file formats)

I'd like to write a batch file that FOR EACH file in a specified directory runs the command I wrote above, using the current file name both for input and for output filename.

For example, if I have this set of files:

c:\directory\file1.xls c:\directory\file2.xls c:\directory\file3.xls 

the output should be

c:\directory\file1.xlsx c:\directory\file2.xlsx c:\directory\file3.xlsx 

so the batch pseudo code should be something like

directory = c:\directory\ for (fileName in directory)     ssconvert fileName.xls fileName.xlsx 

Can anyone help me?

like image 632
BeNdErR Avatar asked Jan 09 '13 14:01

BeNdErR


People also ask

How do I run a shell script in all files in a directory?

All of them are simple one-liner shell scripts that displays the given string(s) using "echo" command in the standard output. Again, I make the second script executable and run it and so on. Well, there is a better way to do this. We can run all scripts in a directory or path using "run-parts" command.

How do you sequentially execute commands in batch file?

Try using the conditional execution & or the && between each command either with a copy and paste into the cmd.exe window or in a batch file. Additionally, you can use the double pipe || symbols instead to only run the next command if the previous command failed.

What is %% f in batch file?

To use for in a batch file, use the following syntax: for {%%|%}<variable> in (<set>) do <command> [<commandlineoptions>] To display the contents of all the files in the current directory that have the extension .doc or .txt by using the replaceable variable %f, type: for %f in (*.doc *.txt) do type %f.

Can a batch script file run multiple commands at the same time?

You can use batch scripts to run multiple commands and instructions on your machine simultaneously. Using a batch script, you will be able to execute all your commands one by one automatically.


1 Answers

for /r %%v in (*.xls) do ssconvert "%%v" "%%vx" 

a couple have people have asked me to explain this, so:

Part 1: for /r %%v in (*.xls)

This part returns an array of files in the current directory that have the xls extension. The %% may look a little curious. This is basically the special % character from command line as used in %PATH% or %TEMP%. To use it in a batch file we need to escape it like so: %%PATH%% or %%TEMP%%. In this case we are simply escaping the temporary variable v, which will hold our array of filenames.

We are using the /r switch to search for files recursively, so any matching files in child folders will also be located.

Part 2: do ssconvert "%%v" "%%vx"

This second part is what will get executed once per matching filename, so if the following files were present in the current folder:

c:\temp\mySheet.xls, c:\temp\mySheet_yesterday.xls, c:\temp\mySheet_20160902.xls

the following commands would be executed:

ssconvert "c:\temp\mySheet.xls" "c:\temp\mySheet.xlsx" ssconvert "c:\temp\mySheet_yesterday.xls" "c:\temp\mySheet_yesterday.xlsx" ssconvert "c:\temp\mySheet_20160902.xls" "c:\temp\mySheet_20160902.xlsx"

like image 78
paul Avatar answered Sep 18 '22 12:09

paul