cd <path to your folder>
$files = Get-ChildItem -file;
ForEach ($file in $files)
{
$folder = New-Item -type directory -name $file.BaseName;
Move-Item $file.FullName $folder.FullName;
}
This script creates a directory for each file and moves the file to this directory. To exclude _V001 from the directory name, you can call the TrimEnd method on $file.BaseName -
$file.BaseName.TrimEnd ("_V001")
Step by step.
- First of all, go to the directory that contains your files.
- Get a list of objects that represent your files by using the Get-ChildItem cmdlet with the -file attribute. These objects contain properties - such as BaseName, FullName, and so on.
- Save this list to the $files variable.
- Loop through the list with ForEach.
- In the ForEach body, create a directory for each file with the New-Item cmdlet. The -type attribute specifies the type for the new item (-type directory), for the -name attribute substitute the $file.BaseName property. BaseName property returns a string with the name of the file without the extension and path.
Save the object of the newly created directory into the $folder variable.
Move the file using the Move-Item cmdlet. This cmdlet requires two attributes: source path and destination path. As the source path, use the FullName property of the file object, and the FullName property of the directory object for the destination path. FullName property returns the name of the file or directory that contains the full path, for example D:directoryfile.txt for a file and D:directoryanotherDirectory for a directory.
It's not a big deal, actually, and without shortcuts it looks like a plain English.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…