有需求如下:
把第一列.jpg,重命名为第二列,以逗号分隔。
有tmp.txt如下
有图片如下11,000010030.jpg
21,2000.jpg
221,90ew2000.jpg
有shell脚本如下11.jpg 21.jpg 221.jpg
当然,shell的数组也是用过的,不过不在这个版本用,用它感觉也是慢。
用数组来存储新旧文件名。
[bash]#!/bin/bash
_dir=~/pic
if [ -f tmp.txt ];then {
[ $PWD == $_dir ] || cd $_dir
count=0
for var in *.jpg;do
page=${var%%.jpg};
fn=`sed -n "/^$page,/p" tmp.txt | awk -F',' '{print $2}'`;
mv $page.jpg $fn && count=`expr $count + 1`
done
echo "rename $count files ok"
}
else
echo -e "\nerror: tmp.txt does no exist\n"
fi
[/bash]
c源码如下,(求其正解)
[c]#include <stdio.h>
int main(void)
{
FILE *p = NULL;
int page;
int yh[4];
char img[20];
char mlbl[20];
if(p = fopen("file.txt", "r"))
{
int rows=0;
while(!feof(p))
{
fscanf(p, "%d,%s\n", &page, img);
printf("\n%d\t%s\n", page, img);
char oldname[80], newname[80];
/* prompt for file to rename and new name
oldname=page;
newname=img;
printf("File to rename: ");
gets(oldname);
printf("New name: ");
gets(newname);*/
/* Rename the file
if (rename(oldname, newname) == 0)
printf("Renamed %s to %s./n", oldname, newname);
else
perror("rename");
return 0;*/
rows++;
}
fclose(p);
}
return 0;
}
[/c]
刚刚开始学c,没搞懂啊,不知道rename函数接受什么样的数据类型,类型转换失败!