Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
Lista Forumurilor Pe Tematici
Mrrrr's Forum (VIEW ONLY) | Reguli | Inregistrare | Login

POZE MRRRR'S FORUM (VIEW ONLY)

Nu sunteti logat.
Nou pe simpatie:
Bby67
Femeie
19 ani
Bucuresti
cauta Barbat
19 - 70 ani
Mrrrr's Forum (VIEW ONLY) / Tutoriale si Ghiduri Utile // Tutorials and useful guides / [WINDOWS] Move Several Files to Folders with Partial Matching Name Moderat de TRaP, TonyTzu
Autor
Mesaj Pagini: 1
Mrrrr
AdMiN

Inregistrat: acum 17 ani
Postari: 2186
The following powershell script will move all files in declared directory into subfolders based on the firs 3 letters in the name of the files.

So I have over 200 files of which the first 3 letters are the language, eg: fre = French, eng = English etc.
I want each moved to a subfolder of the corresponding language, eg. all starting with fre to the fre subfolder, all starting with eng to the eng subfolder etc.

In the code below, I marked comments with this color and the 2 most important lines with this color.

Update the code below with your desired input directory and number of characters, and copy/paste it into any powershell window, then press Enter.


#Define Parent Folder
$directory = "D:\TEST"

#Go to Parent Folder
cd $directory

#Get all children in Parent Folder -- Filter Here as needed
$files = Get-ChildItem

# Go to every child and see if a path exists for them
foreach ($File in $files){

#Adjust substring for what is important, change 3 to the desired number of characters from the start of files
$prefix = $File.name.Substring(0,3)

#Join the Parent Folder and Prefix Path
$subdirectoryPath = Join-Path -Path $directory -ChildPath $prefix

#IF folder doesn't exist create one
if(!(Test-Path $subdirectoryPath -PathType Container))
{
New-Item $directory -Name $prefix -ItemType "Directory" -Force
}

#Define Destination Folder
$destinationPath = Join-Path -Path $subdirectoryPath -ChildPath $File.Name

#Move the File to the new path
Move-Item -Path $File.FullName -Destination $destinationPath -Force
}


Source:


_______________________________________


pus acum 3 luni
   
Mrrrr
AdMiN

Inregistrat: acum 17 ani
Postari: 2186
Instead of hardcoding the folder and the number of characters, one could change the following 2 lines:

$directory = "D:\TEST"

$prefix = $File.name.Substring(0,3)

to

$directory = Read-Host -Prompt "Enter directory where files are located"
$numb = Read-Host -Prompt "Enter desired number of characters"

$prefix = $File.name.Substring(0,$numb)


PS1 files, unless code is copied to powershell, are not easy to run in order to protect the system. So...
Either hardcode the .ps1 file location into a .bat file, or use a similar input prompt. Save the following as .bat file:

@echo off
REM This is hardcoded folder location
REM set /p uPath="D:\TEST\MoveFiles.ps1"

REM Or use the following line to enter the PS1 file location at a prompt:
set /p uPath=Enter full path to ps1 file:

REM Now run the PS1
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -File %uPath%


_______________________________________


pus acum 3 luni
   
Mrrrr
AdMiN

Inregistrat: acum 17 ani
Postari: 2186
I had problems with a directory name with characters like . and [ ].

Moved the files out of that folder to D:\TEST and no more problems.


_______________________________________


pus acum 3 luni
   
Pagini: 1  

Mergi la