batch script - run command on each file in directory
Asked Answered
B

4

52

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?

Bawl answered 9/1, 2013 at 14:34 Comment(1)
Does this answer your question? How do you loop in a Windows batch file?Avila
V
70
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"

Vereeniging answered 9/1, 2013 at 14:43 Comment(10)
I could not get it to work with the /r. Removed that and it was fine.Cozy
@BradIrby I've used it to run a jar-Konverter instead of ssconvert and it was working well, so you might had some issues with your dos cmd.exe ;-).Crawfish
FWIW /r means "recursive" (and seemed to work ok here) (the "%%vx" in this instance just means "use %%v and add the letter x after that")Sorci
remember that if you try from command prompt it works with single percent sign while in a batch(cmd) file it needs the double percent sign!Fredkin
Why didn't you explain the command so that others like me can make better use of it...?Canadianism
you should add some explanation to what you actually did. People viit stackoverflow to learn, not to copy - at least they shouldKilroy
Is there a way to remove the old extension ? Because "%%v" is the whole namePlexiform
@Phoenix you could use "%%~nv" to just get the filename without extensionVereeniging
@Vereeniging could you point me to some resource about ~nv syntax?Wilhite
@Wilhite https://mcmap.net/q/353892/-how-to-show-only-filenames-without-extensions-using-dir-commandVereeniging
S
32

Actually this is pretty easy since Windows Vista. Microsoft added the command FORFILES

in your case

forfiles /p c:\directory /m *.xls /c "cmd /c ssconvert @file @fname.xlsx"

the only weird thing with this command is that forfiles automatically adds double quotes around @file and @fname. but it should work anyway

Sandman answered 9/1, 2013 at 15:4 Comment(1)
Note that the quotes may mess with other commands, but can be removedRaeraeann
P
17

you can run something like this (paste the code bellow in a .bat, or if you want it to run interractively replace the %% by % :

for %%i in (c:\directory\*.xls) do ssconvert %%i %%i.xlsx

If you can run powershell it will be :

Get-ChildItem -Path c:\directory -filter *.xls | foreach {ssconvert $($_.FullName) $($_.baseName).xlsx }
Pterosaur answered 9/1, 2013 at 14:42 Comment(2)
Does the first command work as %%i already has .xls extension so you'll be converting to .xlsxlsxMannequin
What if my directory has a space character in it. How to handle that?Wilhite
M
0

I am doing similar thing to compile all the c files in a directory.
for iterating files in different directory try this.

set codedirectory=C:\Users\code
for /r  %codedirectory% %%i in (*.c) do 
( some GCC commands )
Maurer answered 10/10, 2017 at 5:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.