Solution
Yeah a script would be the most efficient especially if you have a large number of files. In powershell it would be like this
or it could be created as a function so that you can pass it different values for reuse
Code:
$CopyFromPath = "C:\Some\Path"
$CopyToRootDirectory = "C:\Some\Other\Path"
$FileTypeFilter = "*.xlsx"
$Files = Get-ChildItem -Path $CopyFromPath -Filter $FileTypeFilter
foreach($File in $Files)
{
$DirectoryName = "\$File.BaseName"
$CopyToDirectory = "$CopyToRootDirectory$DirectoryName"
if(-not(Test-Path $CopyToDirectory))
{
New-Item -Path $CopyToDirectory -ItemType Directory
}
Move-Item $File.FullName -Destination $CopyToDirectory -Forc
}
or it could be created as a function so that you can pass it different values for reuse
Code:
Function Move-ItemToDirectory
{
param...
livix07
Honorable Member
- Joined
- Feb 19, 2018
- Messages
- 585
Most likely a script run via Command Prompt or PowerShell.
Here is a Python script that does what you want:
- you can get Python for Windows from here: Download Python
- create a file with extension .py (example: copy_files.py), copy the code below and replace the paths and the names of files and folders.
- let's say you want to copy files a,b,c (s0,s1,s2) to folders A,B,C (t0,t1,t2)
- you MUST provide the FULL PATH to the files and folders and make sure you have permissions to execute files and create folders
You can also do it like this:
Thank you for giving me an extra reason to learn programming!
Here is a Python script that does what you want:
- you can get Python for Windows from here: Download Python
- create a file with extension .py (example: copy_files.py), copy the code below and replace the paths and the names of files and folders.
- let's say you want to copy files a,b,c (s0,s1,s2) to folders A,B,C (t0,t1,t2)
- you MUST provide the FULL PATH to the files and folders and make sure you have permissions to execute files and create folders
Code:
import shutil
t0 = "/home/liviu/A"
t1 = "/home/liviu/B"
t2 = "/home/liviu/C"
s0 = "/home/liviu/Desktop/a"
s1 = "/home/liviu/Desktop/b"
s2 = "/home/liviu/Desktop/c"
target = [t0,t1,t2]
source = [s0,s1,s2]
x = 0
for item in target:
shutil.copy(source[x],target[x])
x += 1
print("done!")
You can also do it like this:
Code:
import shutil
target = ["/home/liviu/A","/home/liviu/B","/home/liviu/C"]
source = ["/home/liviu/Desktop/a","/home/liviu/Desktop/b","/home/liviu/Desktop/c"]
x = 0
for item in target:
shutil.copy(source[x],target[x])
x += 1
print("done!")
Thank you for giving me an extra reason to learn programming!
Last edited:
- Joined
- Jul 4, 2015
- Messages
- 8,998
Yeah a script would be the most efficient especially if you have a large number of files. In powershell it would be like this
or it could be created as a function so that you can pass it different values for reuse
Then you could load it and run Move-ItemDirectory -SourceDirectory = "C:\Some\Path" -DestinationDirectory = "C:\Some\Other\Path" -Filter "*.xlsx"
Code:
$CopyFromPath = "C:\Some\Path"
$CopyToRootDirectory = "C:\Some\Other\Path"
$FileTypeFilter = "*.xlsx"
$Files = Get-ChildItem -Path $CopyFromPath -Filter $FileTypeFilter
foreach($File in $Files)
{
$DirectoryName = "\$File.BaseName"
$CopyToDirectory = "$CopyToRootDirectory$DirectoryName"
if(-not(Test-Path $CopyToDirectory))
{
New-Item -Path $CopyToDirectory -ItemType Directory
}
Move-Item $File.FullName -Destination $CopyToDirectory -Forc
}
or it could be created as a function so that you can pass it different values for reuse
Code:
Function Move-ItemToDirectory
{
param
(
[Parameter(Mandatory=$true)][string]$SourceDirectory,
[Parameter(Mandatory=$true)][string]$DestinationDirectory,
[string]$Filter="*.xlsx"
)
$CopyFromPath = $SourceDirectory
$CopyToRootDirectory = $DestinationDirectory
$FileTypeFilter = $Filter
$Files = Get-ChildItem -Path $CopyFromPath -Filter $FileTypeFilter
foreach($File in $Files)
{
$DirectoryName = "\$File.BaseName"
$CopyToDirectory = "$CopyToRootDirectory$DirectoryName"
if(-not(Test-Path $CopyToDirectory))
{
New-Item -Path $CopyToDirectory -ItemType Directory
}
Move-Item $File.FullName -Destination $CopyToDirectory -Forc
}
}
Then you could load it and run Move-ItemDirectory -SourceDirectory = "C:\Some\Path" -DestinationDirectory = "C:\Some\Other\Path" -Filter "*.xlsx"
livix07
Honorable Member
- Joined
- Feb 19, 2018
- Messages
- 585
Unfortunately you must type the full path to files and folders every time you use it.Does either of these two methods have some sort of UI that allows me to choose the folder path each time before I run it? Because the folder path will be different every time.
I am a beginner in programming and most likely this is not the best code in Python.
You should ask or search on programming forums for better code.
- Joined
- Jul 4, 2015
- Messages
- 8,998
Revised copy with two prompts.
Code:
Add-Type -AssemblyName System.Windows.Forms
$SourceLocation = New-Object System.Windows.Forms.FolderBrowserDialog
$SourceLocation.Description = "Select a Source Directory"
$SourceLocation.ShowDialog()
$DestinationLocation = New-Object System.Windows.Forms.FolderBrowserDialog
$DestinationLocation.Description = "Select a Destination Directory"
$DestinationLocation.ShowDialog()
$CopyFromPath = $SourceLocation.SelectedPath
$CopyToRootDirectory = $DestinationLocation.SelectedPath
$FileTypeFilter = "*.xlsx"
$Files = Get-ChildItem -Path $CopyFromPath -Filter $FileTypeFilter
foreach($File in $Files)
{
$DirectoryName = "\$($File.BaseName)"
$CopyToDirectory = "$CopyToRootDirectory$DirectoryName"
if(-not(Test-Path $CopyToDirectory))
{
New-Item -Path $CopyToDirectory -ItemType Directory
}
$File.FullName
$CopyToDirectory
Move-Item $File.FullName -Destination $CopyToDirectory -Force
}
Last edited:
q988988
New Member
- Joined
- Nov 21, 2019
- Messages
- 19
- Thread Author
-
- #9
I created a text file (not sure how to create Python file) then changed the extension to ".py" and copied your code into it using Notepad++, but it did not seem to run as expected? Or I have to run it under a specific folder path? I would expect a prompt to show up to let me choose the folder/file paths?
livix07
Honorable Member
- Joined
- Feb 19, 2018
- Messages
- 585
Nope, it doesn't give you the possibility to choose file/folder.I would expect a prompt to show up to let me choose the folder/file paths?
You must type in the full path to the file/folder and you can use copy/paste to make it easier.
Last edited:
q988988
New Member
- Joined
- Nov 21, 2019
- Messages
- 19
- Thread Author
-
- #12
So all I need to do is to replace "Select a Source Directory" and "Select a Destination Directory" with "Z:\Files" and "Z:\Folders" respectively, then run the .py file?Nope, it doesn't give you the possibility to choose file/folder.
You must type in the full path to the file/folder and you can use copy/paste to make it easier.
q988988
New Member
- Joined
- Nov 21, 2019
- Messages
- 19
- Thread Author
-
- #13
Following is what I have in the py file, but it does not seem to work:
Add-Type -AssemblyName System.Windows.Forms
$SourceLocation = New-Object System.Windows.Forms.FolderBrowserDialog
$SourceLocation.Description = "Z:\Files"
$SourceLocation.ShowDialog()
$DestinationLocation = New-Object System.Windows.Forms.FolderBrowserDialog
$DestinationLocation.Description = "Z:\Folders"
$DestinationLocation.ShowDialog()
$CopyFromPath = $SourceDirectory.SelectedPath
$CopyToRootDirectory = $DestinationDirectory.SelectedPath
$FileTypeFilter = "*.xlsx"
$Files = Get-ChildItem -Path $CopyFromPath -Filter $FileTypeFilter
foreach($File in $Files)
{
$DirectoryName = "\$File.BaseName"
$CopyToDirectory = "$CopyToRootDirectory$DirectoryName"
if(-not(Test-Path $CopyToDirectory))
{
New-Item -Path $CopyToDirectory -ItemType Directory
}
Move-Item $File.FullName -Destination $CopyToDirectory -Forc
}
Add-Type -AssemblyName System.Windows.Forms
$SourceLocation = New-Object System.Windows.Forms.FolderBrowserDialog
$SourceLocation.Description = "Z:\Files"
$SourceLocation.ShowDialog()
$DestinationLocation = New-Object System.Windows.Forms.FolderBrowserDialog
$DestinationLocation.Description = "Z:\Folders"
$DestinationLocation.ShowDialog()
$CopyFromPath = $SourceDirectory.SelectedPath
$CopyToRootDirectory = $DestinationDirectory.SelectedPath
$FileTypeFilter = "*.xlsx"
$Files = Get-ChildItem -Path $CopyFromPath -Filter $FileTypeFilter
foreach($File in $Files)
{
$DirectoryName = "\$File.BaseName"
$CopyToDirectory = "$CopyToRootDirectory$DirectoryName"
if(-not(Test-Path $CopyToDirectory))
{
New-Item -Path $CopyToDirectory -ItemType Directory
}
Move-Item $File.FullName -Destination $CopyToDirectory -Forc
}
Attachments
q988988
New Member
- Joined
- Nov 21, 2019
- Messages
- 19
- Thread Author
-
- #20
Does this code work for the file name like 22B_1? Also the folder already contains files before I run the code. Will these two conditions affect the code? Because the code worked for the example I had previously (file A->Folder A, etc) but it did not seem to work for a bit more complicated case.
Similar threads
- Replies
- 1
- Views
- 46
- Replies
- 0
- Views
- 34
- Article
- Replies
- 0
- Views
- 44
- Replies
- 1
- Views
- 93