-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenameFile.cpp
More file actions
42 lines (37 loc) · 1009 Bytes
/
renameFile.cpp
File metadata and controls
42 lines (37 loc) · 1009 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <conio.h>
#include <windows.h>
void renameFile()
{
// Path to old and new files
char oldName[100], newName[100];
// Input old and new file name
system ("cls");
printf("Enter the name of the file you want to change: ");
scanf("%s", oldName);
backAgainNewName:
printf("enter new file name: ");
scanf("%s", newName);
// validasi jika ektensi .exe
if(strcmp(newName, "exe")==0){
printf("\nInvalid file extension name.\n");
getch();
system("cls");
goto backAgainNewName;
}else if(fopen(newName, "r") != NULL){
printf("File already exists.\n");
printf("Press any key to continue...\n");
getch();
system("cls");
goto backAgainNewName;
}
// rename old file with new name
if (rename(oldName, newName) == 0)
{
printf("The file was renamed successfully.\n");
}
else
{
printf("Unable to rename file. Please check the file exists and you have permission to modify the file.\n");
}
}