Rename copy file (Perl)

This code can be found in Chapter 4 of Windows Server Cookbook

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the Perl code from this site.

# This code shows how to rename (same as move in WMI) and copy a file
# or folder. 

# ---------------------------------------------------------------
# Adapted from VBScript code contained in the book:
#      "Windows Server Cookbook" by Robbie Allen
# ISBN: 0-596-00633-0
# ---------------------------------------------------------------

use Win32::OLE;
$Win32::OLE::Warn = 3;

# ------ SCRIPT CONFIGURATION ------
$strComputer = '.';
$strCurrentFile = '<CurrentFilePath>';# Path to existing file or folder
$strNewFile = '<NewFilePath>';# New path of file or folder
# ------ END CONFIGURATION ---------
$objWMI = Win32::OLE->GetObject('winmgmts:\\\\' . $strComputer . '\\root\\cimv2');
$objFile = $objWMI->Get('Cim_Datafile=\'' . $strCurrentFile . '\'');
print "Renaming $strCurrentFile to $strNewFile\n";
$intRC = $objFile->Rename($strNewFile);
if ($intRC != 0) {
    print 'There was an error renaming the file: ' . $intRC, "\n";
}
else {
    print "File rename successful\n";
}

# ------ SCRIPT CONFIGURATION ------
$strComputer = '.';
$strCurrentFile = '<CurrentFilePath>';
# Path to existing file or folder
$strNewFile = '<NewFilePath>';
# Path to copy file or folder
# ------ END CONFIGURATION ---------
$objWMI = Win32::OLE->GetObject('winmgmts:\\\\' . $strComputer . '\\root\\cimv2');
$objFile = $objWMI->Get('Cim_Datafile=\'' . $strCurrentFile . '\'');
print "Copying $strCurrentFile to $strNewFile\n";
$intRC = $objFile->Copy($strNewFile);
if ($intRC != 0) {
    print 'There was an error copying the file: ' . $intRC, "\n";
}
else {
    print "File copy successful\n";
}

This code has been viewed 4171 times.

New from the creators of TechTasks.com: StatSheet.com