转换脚本&使用它

时间:2013-01-28 00:47:57

标签: jpeg file-extension

我一直在谷歌上搜索如何将文件扩展名插入到多个没有扩展名的文件(文件名到filename.txt),以及纠正错误的扩展名(从错误保存的.txt到.jpg,用于示例),以及从此重命名过程中排除其他文件/文件夹。

我特别偶然发现的一页与我的非常相似:http://ubuntuforums.org/showthread.php?t=1185203

在这个帖子的第二页上,有一个由tinge发布的脚本,这似乎是我正在寻找的解决方案(有人可以验证这个吗?):


#!/bin/bash
program () {
    FILETYPE="$1"
# Select all files in current directory, and process one at a time
for FILE in *; do
    # Strip of any extension, and save to FILE1
    FILE1=${FILE%\.*}

    # If it's the same afterward, then there was no extension - rename it
    if [ "$FILE1" = "$FILE" ]; then
    while [ -f "${FILE1}.${FILETYPE}" ] ; do
        FILE1="${FILE1}_1"
    done
        mv "$FILE" "$FILE1.$FILETYPE"
    fi
done
exit 0
}

case $1 in
    --help)   echo;echo;echo "Usage: noext.sh filetype i.e. noext.sh JPG changes all     files with no extension to a .JPG";echo;echo;echo
    ;;
    -h)   echo;echo;echo "Usage: noext.sh filetype i.e. noext.sh JPG changes all files     with no extension to a .JPG";echo;echo;echo
    ;;
    *) program $1
    ;;
esac

我不确定它是否正确复制(我试图遵循4空间缩进规则..我想我已经宰了它,哈哈),但它是在我发布的帖子链接的第二页上。由于不熟悉脚本,我不知道如何处理它。

我使用的是Windows 7。

有人可以帮忙吗?

谢谢你, -B

1 个答案:

答案 0 :(得分:0)

正如您可能已经看到的那样,此脚本以行

开头
#!/bin/bash

这意味着,在unix environments中您希望该脚本由bash二进制文件执行。但是,在Windows 7中没有预先安装bash。 Windows脚本和Unix脚本大多是相同的东西,但是它们是用不同的语言编写的,它们通常彼此不了解。

您可能想要Windows cmd.exe的此脚本版本,它是Windows中的内置shell。

否则,如果你真的想要执行这个特定文件,你可以安装一些bash的编译版本(就像MinGW,Cygwin或Git等工具一样)。

如果您只想重命名文件,可以使用其他工具做得更好,例如this answeranswers to this other question

中提供的工具