I have a folder with thousands of files (let's say .txt) with different names: VBH_V001.txt VDD_V001.txt DTG_V001.txt ADC_V001.txt DFD_V001.txt etc....
I need to create directories in that folder with the name of each file and then move those files to directories. The result should be for example Folder with the the name VBH (without _V001.txt) and in that folder I should have VBH_V001.txt file. How can I do that. Please advise.
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 -
Step by step.
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:\directory\file.txt for a file and D:\directory\anotherDirectory for a directory.
It's not a big deal, actually, and without shortcuts it looks like a plain English.
What you basically need to do, is to:
Hope that helps.