简体   繁体   中英

Windows shell script convert all files in directory

I'm new to this forum and never work with windows batch files but for my study thesis I need to convert meteo files (.rad) into .asc files with the following command:

.\gridasci.exe ver_500-2012071712.rad meteo-2012071712.asc

where 2012071712 is the date including time. Now I have plenty of .rad files which all have the same structure but a different date and would like to convert them into .asc files, for which a windows batch file would be great which iterates through all the files. So a sketch of the code could look like this:

for (all files with right format) in (this directory) do (
    date = extract date from (ver_500-date.rad)
    .\gridasci.exe ver_500-date.rad meteo-date.asc
)

Any help is highly appreciated!

Thank you very much and have a good day.

Perhaps:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN (ver_500-*.rad) DO (SET "_=%%~nA"
    ECHO=.\gridasci.exe %%A meteo-!_:*-=!.asc)
ENDLOCAL
TIMEOUT -1 >NUL
EXIT/B

To run gridasci against the files remove ECHO= from line 4 if happy with the results returned.

Assuming that you use Windows NT command processor (cmd.exe)...

setlocal EnableDelayedExpansion
for %%f in (ver_500-*.rad) do (
    set "date=%%f"
    set "date=!date:ver_500-=!"
    set "date=!date:.rad=!"
    .\gridasci.exe ver_500-!date!.rad meteo-!date!.asc
)

You should probably learn about Delayed Expansion and how string replace in variable expansion works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM