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

Quick Reference Cards

A reference card or quick reference card is a concise bundling of common syntactic rules and idioms of a particular formal language . It serves as an ad hoc memory aid for an experienced user . In spite of what the name reference card suggest, the term only tends to be used in the narrow context of programming languages or markup languages . However, this concept is now being adopted to portray concise information in many other fields.

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:

Cisco Network Topology Icons

Cisco icons are globally recognized and accepted as the standard in network icon topologies. Edraw Network Diagram software includes the standard Cisco Corporate Icons. It's easy to design and diagram your network diagrams.