简体   繁体   中英

Generating Folders based on file names and moving files to a subfolder in the generated folder

I've managed to create folders based on file names, and moving them in the corresponding folders. But i just cant figure out how to move them in sub folders of the corresponding folders.

Example:

IMG1 Create folder IMG1 and move file to \IMG1\Image\

IMG2 Create folder IMG1 and move file to \IMG2\Image\

IMG3 Create folder IMG1 and move file to \IMG2\Image\

Thanks in advance!

Are you looking for something like

$PathToSearch = "C:\Users\mspow\Downloads\Test"
$Files = Get-ChildItem -Path $PathToSearch -File 

foreach ($file in $Files)
{
    New-Item -ItemType Directory -Force -Path ($PathToSearch + "\" + $file.BaseName + "\Image")
    Move-Item -Path $file.FullName -Destination ($PathToSearch + "\" + $file.BaseName + "\Image")
}

We use Get-ChildItem to get a list of all files in the directory. Then for each of those files, first we create a new subdirectory in that folder using the Base Name of the file- the name without the extension. Then we move the original file into the new subdirectory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM