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

Facts to Consider when purchasing a Hardware Firewall

  When buying a hardware firewall, there are several factors you should consider. Here is a checklist of important things to consider: Performance: Determine the required throughput and session capacity to meet the needs of your network. Security features: Look for features such as intrusion detection and prevention, VPN support, content filtering, anti-virus, anti-spam, and application control. Ease of use: Make sure the firewall has a user-friendly interface for easy configuration and management. Scalability: Consider the ability to add or upgrade hardware components to accommodate future growth. Integration: Determine how well the firewall integrates with your existing network and security infrastructure. Support: Look for a vendor with a good reputation for providing timely and effective support. Price: Evaluate the total cost of ownership, including hardware, licensing, and ongoing maintenance and support costs. Compliance: Check if the firewall meets any regulatory or complia...

Windows Password Remover 7.01

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: