I have a folder where files get dropped, I wish to pull the files from that folder and move to a new folder based on part of the file name. If the new folder is missing then create it.
I have attempted to put together the below, however it throws an error about the path already existing and doesn't move the file.
File names can be any thing with out pattern except the last 16 characters of the file, I am removing these and using the remaining as the folder name.
I am really new to scripting so if i have made a silly mistake explanations are appreciated.
Edit
I have played with different orders of operations, added a "-Force" to the new item command, tried with using "Else" and not "If (!(".
I am now at the point where it proudly displays the new directory and then stops.
Could i add the move-item part to a new for each loop so it is processed after the dir is created and tested? If so how do you arrange the { } parts?
Edit 2
I finally have it working, updated script below, the movie-item command was having issues when running into special characters in file names, in my case it was square brackets. The -literalpath switch fixed that for me.
Thanks every one for your input.
Updated script 3.0
#Set source folder
$source = "D:\test\source\"
#Set destination folder (up one level of true destination)
$dest = "D:\test\dest\"
#Define filter Arguments
$filter = "*.txt"
<#
$sourcefile - finds all files that match the filter in the source folder
$trimpath - leaves $file as is, but gets just the file name.
$string - gets file name from $trimpath and converts to a string
$trimmedstring - Takes string from $trimfile and removes the last 16 char off the end of the string
Test for path, if it exists then move on, If not then create directory
Move file to new destination
#>
pushd $source
$sourcefile = Get-ChildItem $source -Filter $filter
foreach ($file in $sourcefile){
$trimpath = $file | split-path -leaf
$string = $trimpath.Substring(0)
$trimmedstring = $string.Substring(0,$string.Length-16)
If(!(Test-Path -path "$dest\$trimmedstring")){New-Item "$dest\$trimmedstring" -Type directory -Force}
move-Item -literalpath "$file" "$dest\$trimmedstring"
}
You may have to tweak the paths being used but the below should work.
$sourcefiles = ((Get-ChildItem $source -Filter $filter).BaseName).TrimEnd(16)
foreach ($file in $sourcefiles)
{
if(!(Test-Path "$dest\$file")){
New-item -ItemType directory -path "$dest\$file"
}
Move-Item "$source\$file" "$dest\file"
}
I finally have it working, updated script below, the movie-item command was having issues when running into special characters in file names, in my case it was square brackets. The -literalpath switch fixed that for me. Thanks every one for your input.
#Set source folder
$source = "D:\test\source\"
#Set destination folder (up one level of true destination)
$dest = "D:\test\dest\"
#Define filter Arguments
$filter = "*.txt"
<#
$sourcefile - finds all files that match the filter in the source folder
$trimpath - leaves $file as is, but gets just the file name.
$string - gets file name from $trimpath and converts to a string
$trimmedstring - Takes string from $trimfile and removes the last 16 char off the end of the string
Test for path, if it exists then move on, If not then create directory
Move file to new destination
#>
pushd $source
$sourcefile = Get-ChildItem $source -Filter $filter
foreach ($file in $sourcefile){
$trimpath = $file | split-path -leaf
$string = $trimpath.Substring(0)
$trimmedstring = $string.Substring(0,$string.Length-16)
If(!(Test-Path -path "$dest\$trimmedstring")){New-Item "$dest\$trimmedstring" -Type directory -Force}
move-Item -literalpath "$file" "$dest\$trimmedstring"
}
Related
I want to move some files from one place to another with saving of directory. Result of my script is broken encoding and it doesn't work. I use robocoby, because I have files with names more 256 simbols. I need to move files from different locations. And we are talking about several hundred files.
$source = Get-Content "C:\Users\bill\Downloads\111.TXT" -Encoding UTF8
$destination = "C:\Users\bill\OneDrive\Documents"
foreach($file in $source)
{
robocopy $file $destination /MOVE /E /copyall /log:C:\Users\bill\OneDrive\Documents\log.txt
}
Jane,
Try building your robocopy command like this:
*** UPDATED and TESTED ***
Clear-Host
$source = Get-Content "G:\Test\111.txt" -Encoding UTF8
$destination = "G:\BEKDocs\Test"
Foreach ($SrcPath in $source) {
$file = Split-Path -Path $SrcPath -Leaf
$path = Split-Path -Path $SrcPath -parent
$PLen = $Path.Length -3
$PAdd = $Path.Substring(3,$PLen)
$SavePath = Join-Path -Path $Destination -ChildPath $PAdd
$robocopyOptions = #('/Move', '/copyall')
$LogFile =
#('/log+:G:\BEKDocs\Transfer\log.txt')
$CmdLine = #($path, $SavePath, $file) +
$robocopyOptions + $LogFile
& 'robocopy.exe' $CmdLine
} #End Foreach
Note: Use single quotes as indicated!
Update notes:
Part of the problem was that you had the filenames attached to the paths in your file where RoboCopy wants SourcePath DestPath FileSpec as the first three arguments.
You're copying single files so there is no need for the Recurse /E parameter.
Since you want to preserve the directory structure you need to append the Source path, less the drive (d:) to your destination directory.
You're also calling RoboForm in a loop so you need the /Log+ parameter so each file is appended to the file rather than over writing it.
In my test I copied a file from the base directory, another from one level deep and a third from 2 levels deep. The code preserved the directory structure starting with the specified Destination as the Base or Root directory.
Took a bit of time to figure all this out but I wasn't going to let it go. Hope this works for you!
I am having a strange problem in Powershell (Version 2021.8.0) while creating folders and naming them. I start with a number of individual ebook files in a folder that I set using Set-Location. I use the file name minus the extension to create a new folder with the same name as the e-book file. The code works fine the majority of the time with various file extensions I have stored in an array beginning of the code.
What's happening is that the code creates the proper folder name the majority of the time and moves the source file into the folder after it's created.
The problem is, if the last letter of the source file name, on files with the extension ".epub" end in an "e", then the "e" is missing from the end of the created folder name. I thought that I saw it also drop "r" and "p" but I have been unable to replicate that error recently.
Below is my code. It is set up to run against file extensions for e-books and audiobooks. Please ignore the error messages that are being generated when files of a specific type don't exist in the working folder. I am just using the array for testing and it will be filled automatically later by reading the folder contents.
This Code Creates a Folder for Each File and moves the file into that Folder:
Clear-Host
$SourceFileFolder = 'N:\- Books\- - BMS\- Books Needing Folders'
Set-Location $SourceFileFolder
$MyArray = ( "*.azw3", "*.cbz", "*.doc", "*.docx", "*.djvu", "*.epub", "*.mobi", "*.mp3", "*.pdf", "*.txt" )
Foreach ($FileExtension in $MyArray) {
Get-ChildItem -Include $FileExtension -Name -Recurse | Sort-Object | ForEach-Object { $SourceFileName = $_
$NewDirectoryName = $SourceFileName.TrimEnd($FileExtension)
New-Item -Name $NewDirectoryName -ItemType "directory"
$OriginalFileName = Join-Path -Path $SourceFileFolder -ChildPath $SourceFileName
$DestinationFilename = Join-Path -Path $NewDirectoryName -ChildPath $SourceFileName
$DestinationFilename = Join-Path -Path $SourceFileFolder -ChildPath $DestinationFilename
Move-Item $OriginalFileName -Destination $DestinationFilename
}
}
Thanks for any help you can give. Driving me nuts and I am pretty sure it's something that I am doing wrong, like always.
String.TrimEnd()
Removes all the trailing occurrences of a set of characters specified in an array from the current string.
TrimEnd method will remove all characters that matches in the character array you provided. It does not look for whether or not .epub is at the end of the string, but rather it trims out any of the characters in the argument supplied from the end of the string. In your case, all dots,e,p,u,b will be removed from the end until no more of these characters are within the string. Now, you will eventually (and you do) remove more than what you intended for.
I'd suggest using EndsWith to match your extensions and performing a substring selection instead, as below. If you deal only with single extension (eg: not with .tar.gz or other double extensions type), you can also use the .net [System.IO.Path]::GetFileNameWithoutExtension($MyFileName) method.
$MyFileName = "Teste.epub"
$FileExt = '.epub'
# Wrong approach
$output = $MyFileName.TrimEnd($FileExt)
write-host $output -ForegroundColor Yellow
#Output returns Test
# Proper method
if ($MyFileName.EndsWith($FileExt)) {
$output = $MyFileName.Substring(0,$MyFileName.Length - $FileExt.Length)
Write-Host $output -ForegroundColor Cyan
}
# Returns Tested
#Alternative method. Won't work if you want to trim out double extensions (eg. tar.gz)
if ($MyFileName.EndsWith($FileExt)) {
$Output = [System.IO.Path]::GetFileNameWithoutExtension($MyFileName)
Write-Host $output -ForegroundColor Cyan
}
You're making this too hard on yourself. Use the .BaseName to get the filename without extension.
Your code simplified:
$SourceFileFolder = 'N:\- Books\- - BMS\- Books Needing Folders'
$MyArray = "*.azw3", "*.cbz", "*.doc", "*.docx", "*.djvu", "*.epub", "*.mobi", "*.mp3", "*.pdf", "*.txt"
(Get-ChildItem -Path $SourceFileFolder -Include $MyArray -File -Recurse) | Sort-Object Name | ForEach-Object {
# BaseName is the filename without extension
$NewDirectory = Join-Path -Path $SourceFileFolder -ChildPath $_.BaseName
$null = New-Item -Path $NewDirectory -ItemType Directory -Force
$_ | Move-Item -Destination $NewDirectory
}
I am trying to write a PowerShell script to do the following.
Rename files in source (FTP folders) directories with it's "current_name_datetime.csv" as per a source file "Source_list.csv" this file has the directories "source,destination" I want this script to look into.
Copy newly renamed files to backup directories as per destination in Source_list.csv this file has the directories "source,destination" I want this script to look into.
Move newly renamed files to final destination directory which is not in my current script.
Source_list.csv contents
cscenter,Costume_Supercenter
fkimports,FKImports
My Script:
$sdfiles = Get-Content c:\!tony\Source_list.csv
$sourceDir = "c:\test\"
$destinationDir = "c:\testing\"
Get-ChildItem $sourceDir -Recurse -Include $sdfiles "*.csv"|
ForEach-Object{
$newname= "{0}{1}_{2}.csv" -f $destinationDir, $_.BaseName, [datetime]::Now.ToString('MM-dd-yyyy-hh-mm-ss')
$_|Copy-Item -Include ,$sdfiles -Destination $newname -whatif }
Error:
What if: Performing operation "Copy Directory" on Target "Item: C:\test\cscenter Destination: C:\testing\cscenter_10-01-2015-12-22-24.csv".
I see in the error that it is trying to copy the directory not the single file in each directory and creating a new folder using the original folder name and renaming the folder and appending the date/time stamp.
Confused. The -Include parameter should only be accepting a single array of strings, throwing "*.csv" on to the end of it won't work AFAIK. Additionally It will be interpreting the whole line of the CSV, ie searching for the file "cscenter,Costume_Supercenter" so shouldn't be returning anything. At least that's what I see when I replicate this on my computer.
Lastly you've tried to filter the files, piped that to Copy-Item and tried to filter it again?
I'd take a more straightforward approach:
$sdfiles = Import-CSV c:\!tony\Source_list.csv -Header #("File", "Folder")
$sourcedir = "c:\test\"
$destinationdir = "c:\testing\"
$sdfiles | ForEach-Object {
$path = $sourcedir + $_.File + ".csv"
$folder = $destinationdir + $_.Folder + '\'
if (!(Test-Path $folder)) { New-Item -Type Directory -Path $folder }
if (Test-Path ($path))
{
$newname = "{0}{1}_{2}.csv" -f $folder, $_.File, (Get-Date).ToString('MM-dd-yyyy-hh-mm-ss')
Copy-Item -Path $path -Destination $newname -whatif
}
else { Write-Error "File $($_.File) not found" }
}
It's a bit chunkier but much easier to read and tweak to your liking. Note that Import-CSV does require PowerShell v3. Let me know if you've got v2 and need help tweaking it for a two-dimensional array.
I also recommend looking into Microsoft's MVA courses on PowerShell, they are excellent resources for starting out.
How can I move a file with special characters in it? I am not allowed to rename the file.
My file is: File.Server.Windows.2003.[SP2].01232005.txt
# dFold = The destination folder in the format of \\drive\folder\SubFolder\
# tDir = Root Target Directory on NAS
# sFold = The name of the subfolder that the files will go into
$dFold = "$tDir$sFold"
# sDir = Source Directory
# $File = Original file name as seen in the source Directory
Move-Item -Path $sDir$File -Destination $dFold -force
When I try to execute the above code it does not move the file. I can add some Write-Host statements and it says it moves the file, but it really don't.
Write-Host "Now moving " $File "to " $dFold"\"
Move-Item -Path $sDir$File -Destination $dFold -force
# Now we just write put what went where or not
Write-Host $File "Was Moved to:" $dFold
Output:
Now moving File.Server.Windows.2003.[SP2].01232005.txt to \\NAS\Inventory\Servers\
File.Server.Windows.2003.[SP2].01232005.txt Was Moved to: \\NAS\Inventory\Servers
Did you try:
Move-Item -LiteralPath $sDir$File -Destination $dFold
Move-Item allows wildcard matches when using the -Path parameter, so a substring [SP2] is interpreted as a single character 'S', 'P', or '2' instead of the string '[SP2]'. Using the parameter -LiteralPath instead of -Path prevents that.
Right up front apologies for my lack of knowledge with Powershell. Very new to the language . I need to copy some files located in a certain path to another similar path. For example:
C:\TEMP\Users\<username1>\Documents\<varyingfoldername>\*
C:\TEMP\Users\<username2>\Documents\<varyingfoldername>\*
C:\TEMP\Users\<username3>\Documents\<varyingfoldername>\*
C:\TEMP\Users\<username4>\Documents\<varyingfoldername>\*
etc....
to
C:\Files\Users\<username1>\Documents\<varyingfoldername>\*
C:\Files\Users\<username2>\Documents\<varyingfoldername>\*
C:\Files\Users\<username3>\Documents\<varyingfoldername>\*
C:\Files\Users\<username4>\Documents\<varyingfoldername>\*
etc....
So basically all files and directories from path one need to be copied to the second path for each one of the different paths. The only known constant is the first part of the path like C:\TEMP\Users...... and the first part of the destination like C:\Files\Users.....
I can get all the different paths and files by using:
gci C:\TEMP\[a-z]*\Documents\[a-z]*\
but I am not sure how to then pass what's found in the wildcards so I can use them when I do the copy. Any help would be appreciated here.
This should work:
Get-ChildItem "C:\TEMP\*\Documents\*" | ForEach-Object {
$old = $_.FullName
$new = $_.FullName.Replace("C:\TEMP\Users\","C:\Files\Users\")
Move-Item $old $new
}
For additional complexity in matching folder levels, something like this should work:
Get-ChildItem "C:\TEMP\*\Documents\*" -File | ForEach-Object {
$old = $_.FullName
$pathArray = $old.Split("\") # Splits the path into an array
$new = [system.String]::Join("\", $pathArray[0..1]) # Creates a starting point, in this case C:\Temp
$new += "\" + $pathArray[4] # Appends another folder level, you can change the index to match the folder you're after
$new += "\" + $pathArray[6] # You can repeat this line to keep matching different folders
Copy-Item -Recurse -Force $old $new
}