Rename a file with a script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

Rename a file with a script

#1 Post by loc3loan » 18 Feb 2014 13:33

Hello all,

Could someone help me with a script to rename a text file?

Here is the task that I would like to accomplish:

When a user drop a text file to the script shortcut, it will use that file name and rename other file with that file name. For example:

There are 3 file on two different folder: File1.txt and File2.txt on one folder, and File3.txt on a different folder. When the user drop either File1 or File2 to the script shortcut, it would rename the File3 to File1 or File2_new.txt

Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename a file with a script

#2 Post by foxidrive » 19 Feb 2014 00:09

Code: Select all

@echo off
if /i "%~n1"=="file1" ren "c:\folderB\file3.txt" "%~n1_new.txt"
if /i "%~n1"=="file2" ren "c:\folderB\file3.txt" "%~n1_new.txt"

loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

Re: Rename a file with a script

#3 Post by loc3loan » 19 Feb 2014 08:45

Thanks for the reply foxidrive. Sorry for not clear, the filename for file1 and file2 are randomly change to a different housename. We do not know what the housename the operator is going to drag to the script shortcut. Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename a file with a script

#4 Post by foxidrive » 19 Feb 2014 09:06

Try this out:

Code: Select all

@echo off
ren "c:\folderB\file3.txt" "%~n1_new.txt"

Post Reply