batch script - run command on each file in directory

前端 未结 4 1713
春和景丽
春和景丽 2020-12-05 00:03

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):

ssconv         


        
相关标签:
4条回答
  • 2020-12-05 00:34

    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 )
    
    0 讨论(0)
  • 2020-12-05 00:44

    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 }
    
    0 讨论(0)
  • 2020-12-05 00:45

    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

    0 讨论(0)
  • 2020-12-05 00:49
    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"

    0 讨论(0)
提交回复
热议问题