How to loop over files in directory and change path and add suffix to filename

前端 未结 5 2295
猫巷女王i
猫巷女王i 2020-11-22 06:23

I need to write a script that starts my program with different arguments, but I\'m new to Bash. I start my program with:

./MyProgram.exe Data/data1.txt [Logs/d

5条回答
  •  野性不改
    2020-11-22 07:03

    for file in Data/*.txt
    do
        for ((i = 0; i < 3; i++))
        do
            name=${file##*/}
            base=${name%.txt}
            ./MyProgram.exe "$file" Logs/"${base}_Log$i.txt"
        done
    done
    

    The name=${file##*/} substitution (shell parameter expansion) removes the leading pathname up to the last /.

    The base=${name%.txt} substitution removes the trailing .txt. It's a bit trickier if the extensions can vary.

提交回复
热议问题