Skip to main content

Microsoft PowerShell Script to Rename and move files to different Directory

 <#

This Script will rename the files on a folder path and move them into a single folder

#>



#Set your source path here

$oldPath = "C:\test\old";


#Set your destination path here

$newpath = "C:\test\new";


#>

This will loop through all the files in the source folder path recursively
and and get there full file path and add to the $files variable

#>

$files = Get-ChildItem -Path $oldPath -File -Recurse -Force; # |  select FullName



$ter=0;

$olddir="adad";


foreach ($file in $files)  {


$ter++;


#>

this will get only the directory name of the files

#>

$dirpath = [System.IO.Path]::GetDirectoryName($file.FullName);

$dirname = [System.IO.Path]::GetFileName($dirpath);




$foo = $dirname.Replace("`n", "");

$foo = $foo.Replace(" ", "");

$foo = $foo.Replace("`t", "");


$olddir=$foo;



$oldname = $file.FullName;

$oldext= $file.Extension;

$newname = $foo+$ter.ToString()+$oldext;

#$newnamee = $newname -replace '[~#%&*{}|:<>?/|"]', '';

$newname;

$oldname;

#$newnamee;


Rename-Item -Path $oldname -newname $newname


}





$filesNew = Get-ChildItem -Path $oldPath -File -Recurse -Force; 

foreach ($file in $filesNew)  {


$file.FullName;


Move-Item -Path $file.FullName -Destination $newpath;


}


Comments

Popular posts from this blog

Encrypt/Lock Computer Drives Or Pendrive In Windows 7

                   Microsoft Bitlocker drive encryption is used to encrypt/lock any Local drive or removable drives like pendrives.Bitlocker encrypts the drives using 128 bit key.Once the drives are encrypted/locked it can only opened using the password.Microsoft introduced bitlocker encryption in windows vista.The bitlocker feature is currently available in Ultimate and Enterprise editions of windows 7 ,Vista as well as the Windows Server 2008 and Windows Server 2008 R2 .Bitlocker requires that your hard disk should have atleast two NTFS drives :One of the operating system and the other with a minimum size of 100 MB.

Download Facebook or Youtube Videos Without Any Tools

                Have you ever liked a video shared by your friend on facebook and wanted to download it…Well this this tutorial is not only for facebook or youtube instead it can be used for any video streaming website like metacafe,dailymotion,veoh or yahoo videos.There are websites on the internet that will convert your video link into a download link but it has the following drawback

How To Remove “Error Deleting File or Folder”

It is a very common and annoying message that the file or folder you are trying to deleted cannot be deleted or thependrive cannot be removed as it is used by the system.In this tutorial I will show how to remove your pendrive or delete the files and folders if you get such an error.This error has various flavours and it can be of any of the following types: