Search code examples
windowsfor-loopbatch-filecmdfile-rename

Passing a truncated filename to another batch file


I have a bunch of files in a folder named cores. The files have names like c_000223~a, c_120302~a, etc.

I have a batch file with a for loop in it. It should go through each core file and send it to another batch file called RunMSFlux for processing. The trouble is RunMSFlux doesn't accept the core filenames they are. It doesn't want the 'c_' at the beginning, just 000223~a, 120302~a, and so on.

I can't modify RunMSFlux to accept the 'c_', as it is read-only. Also, I don't want to rename the core files as they are used by other programs outside of my batch file. So I need a way to strip off the c_ at the beginning of the filename and then pass that on to RunMSFlux.

Here is my batch file so far:

    @echo on
    set bindirectory=K:\Physics\bin\1112\
    set coredirectory=K:\Physics\cores\
    set quiet=yes

for %%f in ( %coredirectory%* ) do (

    call %bindirectory%\RunMSFlux.bat %%f measbu measbu
)

Is there a way to get the batch file to strip out the c_ first before passing the filename to the for loop? Or perhaps could I create a .txt file with the names of the truncated core files in them and have the script go through it line by line and pass those names off to the for loop?


Solution

  • After much wailing and gnashing of teeth I believe I finally have something that works:

    @echo on
    set bindirectory=K:\Physics\bin\1112\
    set coredirectory=K:\Physics\cores\
    set quiet=yes
    
    for /F "tokens=1* eol=_ delims=_" %%g in ( 'dir /A:-d /B %coredirectory%' ) do (
    
    call %bindirectory%RunMSFlux.bat %%h measbu measbu 
    )
    

    The tokens, eol, and delims strip out the c_ in the filenames listed by 'dir /A:-d /B' before passing the truncated filename on to RunMSFlux.bat inside the loop.