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

How To Choose Best Hosting for Your Website

                                                If you are starting a website the first thing you should do is find a good hosting provider.If you select the right hosting provider it will not only save you from future hassles but it is also beneficial for SEO of your website.There are too many hosting provider available in the market and many of them will give you fake promises like unlimited bandwidth , connections , space etc.But as your website start gaining decent traffic they will ask you to upgrade your hosting plans as they cannot support that much traffic under your current plan.

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.

Machine Learning

Machine Learning By Tom M. Mitchell Publisher: Mc/Gra w-H i ll 1997 | 432 Pages | ISBN: 0070428077 | File type: PDF | 38 mb