Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename a file via VBscript

Status
Not open for further replies.

dls8232

MIS
Apr 18, 2008
2
US
I want to be able to right click on a file and have it automatically change the file I right clicked on to the filename.extention.original.

In other words if a file is named 'program.exe' I want to be able to automatically just right click on the file select a menu option then it will automatically find the path of the file I right clicked on then rename it 'program.exe.original'.

I do a lot of renaming of files and I hate right clicking then going to the end of the file and renaming the file.

I know how to add the menu item to the right click menu. I just basically need to know if there is a vbscript function that can return the path of the file I have right clicked on. Then I will be able to rename it from there.
 
[1] Make a vbs like this.
[tt]
'call it whatever, renamefile.vbs
dim spath,fso,f,sname
if count(wscript.arguments)<>0 then
spath=wscript.arguments(0)
set fso=createobject("scripting.filesystemobject")
'this is in the rightclick menu, so it can spare further test for existence, otherwise, you should.
if fso.fileexists(spath) then
set f=fso.getfile(spath)
sname=f.name
f.name=sname & ".original"
set f=nothing
end if
set fso=nothing
end if
[/tt]
[2] Create an entry in right-click menu (that you said you know how) with the command line:
[tt] wscript.exe <path-to=renamefile.vbs-quoted if necessary> "%1"[/tt]
 
Amendment

I had made erroneous use of count, which should be .length on the arguments collection. Further, fear of multiple rename may happen which can be made avoid runtime error, this is the amended version which made good of those deficiency.

[1'] Amended renamefile.vbs or whatever.
[tt]
dim spath,fso,f,sname
if wscript.arguments.length<>0 then
spath=wscript.arguments(0)
set fso=createobject("scripting.filesystemobject")
'this is in the rightclick menu, so it can spare further test for existence, otherwise, you should.
if fso.fileexists(spath) then
if fso.fileexists(spath & ".original") then
fso.copyfile spath, spath & ".original", true
fso.deletefile spath, true
else
set f=fso.getfile(spath)
sname=f.name
f.name=sname & ".original"
set f=nothing
end if
end if
set fso=nothing
end if
[/tt]
 
Now how about a vbscript to take the .original off the file name essentially naming it back to the original file name.

I tried to figure it out based on the script above but was unable to.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top