I am working on a small portion of a larger scripting solution in which i need to create folders based on values stored in a CSV and then move the applicable files in to the new folder according to values in a column of the csv.
The format of the CSV:
fileName, folder
AC002 Y
AC034 Y
AC001
X2400 Y
AC006
AC007
AC009 Y
This is the code I have working for the problem:
$sourceDir = read-host "Please enter source Dir:"
$csv = import-csv C:\scripts\files\files.csv
$csv | where {$_.folder -eq 'Y'} | % {
$path = $sourceDir + "\" + $_.fileName
if(-not (Test-Path $path))
{
md $path
}#end if
}#end for
The next step will probably be a bit more tricky.
Thanks to Shay Levy for the help,
Craig
Try this, it will create the folder even if it exists. We can change that if you want and create only folders that do not exist:
$csv = import-csv C:\scripts\files\files.csv
$csv | where {$_.folder -eq 'y'} | `
foreach { md -force (join-path $sourceDir $_.fileName) }
Related
I'm basically trying to build a routine that reads a named folder directory, builds a CSV file, then reads in that CSV file, manipulates some of the properties to split data into new columns, exports that to another CSV.
This I have achieved with the following code:
$Folder = Read-Host 'Please enter a folder path'
$File = Read-Host 'Please enter a filename'
$OutputFile = $Folder + '\' + $File + '.csv'
$SplitFile = $Folder + '\' + $File + '_Split.csv'
$CopyDir = $Folder + '\WantedDocs\'
Get-ChildItem $Folder -Recurse -Include *.* |
select Directory, FullName, Name |
Export-Csv -Delimiter ',' -NoTypeInformation $OutputFile
$a = Import-Csv $OutputFile
$values = $a.Name.Split("_")
$a | Add-Member 'CliCode' -NotePropertyValue $values[3]
$a | Add-Member 'CopyDir' -NotePropertyValue $CopyDir
$a | Select Directory, FullName, Name, CliCode, CopyDir |
Export-Csv -Delimiter ',' -NoTypeInformation $SplitFile
Excuse me if my terminology isn't right, but I am now looking to build a batch file full of xcopy commands, using item values from the properties.
xcopy 'C:\Test\OriginalDocs\A\file1_A_A_12345678.txt' 'C:\Test\WantedDocs\*' /Y
When you use Import-Csv and assign it to a variable, that variable contain various properties, with each property containing an array of values taken from each line in the CSV file.
In my example the variable $a has properties called "Directory", "FullName" and "Name", the headers of the 3 columns in my CSV file.
If my CSV file contains these lines:
"Directory","FullName","Name"
"C:\Test\OriginalDocs\A","C:\Test\OriginalDocs\A\file1_A_A_12345678.txt","file1_A_A_12345678.txt"
"C:\Test\OriginalDocs\B","C:\Test\OriginalDocs\B\file2_B_B_43534554.txt","file1_B_B_43534554.txt"
The Directory property would be an array of 2 items: "C:\Test\OriginalDocs\A" and "C:\Test\OriginalDocs\B\"
The FullName property would be an array of 2 items: "C:\Test\OriginalDocs\A\file1_A_A_12345678.txt" and "C:\Test\OriginalDocs\B\file2_B_B_43534554.txt"
The Name property would be an array of 2 items: "file1_A_A_12345678.txt" and "file2_B_B_43534554.txt"
What I want to know is how would I be able to select all [0] items in the array for each property and build the xcopy command
e.g. if I do this:
$xc1 = "xcopy '"
$xc2 = $a.FullName
$xc3 = "' '"
$xc4 = $a.CopyDir
$xc5 = $a.CliCode
$xc6 = "\*' /Y"
$xcopy = $xc1 + $xc2 + $xc3 + $xc4 + $xc5+ $xc6
The resulting $xcopy variable contains all array vales
e.g. for the example above the xcopy variable ends up with the value:
xcopy 'C:\Test\OriginalDocs\A\file1_A_A_12345678.txt C:\Test\OriginalDocs\B\file2_B_B_43534554.txt' 'C:\Test\OriginalDocs\WantedDocs\ C:\Test\OriginalDocs\WantedDocs\12345678 43534554\*' /Y
What I want to achieve is to effectively do this with the [0] array values from each selected property:
$xc1 = "xcopy '"
$xc2 = $a.FullName[0]
$xc3 = "' '"
$xc4 = $a.CopyDir[0]
$xc5 = $a.CliCode[0]
$xc6 = "\*' /Y"
$xcopy = $xc1 + $xc2 + $xc3 + $xc4 + $xc5+ $xc6
Write the $xcopy variable to the text file (using Add-Content I believe)
Then do the same with the [1] array values:
$xc1 = "xcopy '"
$xc2 = $a.FullName[1]
$xc3 = "' '"
$xc4 = $a.CopyDir[1]
$xc5 = $a.CliCode[1]
$xc6 = "\*' /Y"
$xcopy = $xc1 + $xc2 + $xc3 + $xc4 + $xc5+ $xc6
And so on until all items in the arrays are dealt with.
So producing a text/batch file with a line for each item in the arrays i.e. all the [0], all the [1] etc.
Using my example above I'd get a text file like below.
xcopy 'C:\Test\OriginalDocs\A\file1_A_A_12345678.txt' 'C:\Test\OriginalDocs\WantedDocs\12345678\*' /Y
xcopy 'C:\Test\OriginalDocs\B\file2_B_B_43534554.txt' 'C:\Test\OriginalDocs\WantedDocs\43534554\*' /Y
I've been looking at foreach and ForEach-Object but so far I've not found anything that works for my needs. Maybe it can't be done?
To work line by line use foreach:
foreach ($Line in $a){ DoSomethingLikeCopy $Line.FullName to "$CopyDir\$($Line.CliCode)" }
Instead of XCopy use New-Item to create a new textfile with the Value of the old file or to create the folder for the new Item:
Get-Content -Path 'C:\Test\OriginalDocs\A\file1_A_A_12345678.txt' -raw | New-Item -Path 'C:\Test\OriginalDocs\WantedDocs\12345678\file1_A_A_12345678.txt' -Force
or
New-Item -Path'C:\Test\OriginalDocs\WantedDocs\12345678\*' -ItemType directory
It's pointless to export data to a CSV that you're reading back right away. Simply use a pipeline. Also, xcopy is an external command and can be run directly from PowerShell, so there's no need to have PowerShell create a batch file first.
This should be all you need:
$Folder = Read-Host 'Please enter a folder path'
Get-ChildItem $Folder -Recurse | ForEach-Object {
$clicode = $_.BaseName.Split("_")[-1]
& xcopy $_.FullName "${Folder}\WantedDocs\${clicode}\*" /y
}
If you indeed want output CSV files for every step of the way, you can do something like this:
# YOU NEED TO ADD CODE FOR CHECKING THE USER INPUT
# What I'm doing here is very rudimentary..
do {
$Folder = Read-Host 'Please enter a folder path'
} while (-not (Test-Path -Path $Folder -PathType Container))
$File = Read-Host 'Please enter a filename (no extension)'
# at the very least sanitize the given filename, and get only the Name without Extension
$BaseName = [System.IO.Path]::GetFileNameWithoutExtension($File)
$OutputFile = Join-Path -Path $Folder -ChildPath ($BaseName + '.csv')
$SplitFile = Join-Path -Path $Folder -ChildPath ($BaseName + '_Split.csv')
$CopyDir = Join-Path -Path $Folder -ChildPath 'WantedDocs'
# collect the files and get the properties Directory, FullName and Name
$a = Get-ChildItem $Folder -Recurse -Include *.* -File | Select-Object Directory,FullName,Name
# write the first CSV file:
$a | Export-Csv -Path $OutputFile -Delimiter ',' -NoTypeInformation
# redefine the collection to add extra properties CliCode, CopyDir and Result
$a = $a | Select-Object *,CliCode,CopyDir,Result
# loop through the collection
$a | ForEach-Object {
# the automatic variable $_ is a single object in the collection
# get the CliCode from the Name property:
# if the filename is "file1_A_A_12345678.txt", the CliCode will be "12345678"
if ($_.Name -match '([^_.]+)\..*$') {
$cliCode = $matches[1]
$targetDir = Join-Path -Path $CopyDir -ChildPath $cliCode
$_.CliCode = $cliCode # example: "12345678"
$_.CopyDir = $targetDir # example: "C:\Test\WantedDocs\12345678"
# copy the file, but create the target folder first if this does not exist
if (-not (Test-Path -Path $targetDir -PathType Container)) {
New-Item -Path $targetDir -ItemType Directory | Out-Null
}
Copy-Item -Path $_.FullName -Destination $targetDir
$_.Result = "OK"
}
else {
# show the error and add "Failure" to the result property
Write-Warning "Skipped file '$_.FullName'. Reason: CliCode not found"
$_.Result = "Failure"
}
}
# output the results of the copy as CSV file
$a | Export-Csv -Path $SplitFile -Delimiter ',' -NoTypeInformation
When done, the files are copied to the new locations and you'll have two CSV files:
The first 'Something.csv' before the copy:
"Directory","FullName","Name"
"D:\Test\OriginalDocs\A","D:\Test\OriginalDocs\A\file1_A_A_12345678.txt","file1_A_A_12345678.txt"
"D:\Test\OriginalDocs\B","D:\Test\OriginalDocs\B\file2_B_B_43534554.txt","file2_B_B_43534554.txt"
and the second 'Something_Split.csv' after the copy:
"Directory","FullName","Name","CliCode","CopyDir","Result"
"D:\Test\OriginalDocs\A","D:\Test\OriginalDocs\A\file1_A_A_12345678.txt","file1_A_A_12345678.txt","12345678","D:\Test\OriginalDocs\WantedDocs\12345678","OK"
"D:\Test\OriginalDocs\B","D:\Test\OriginalDocs\B\file2_B_B_43534554.txt","file2_B_B_43534554.txt","43534554","D:\Test\OriginalDocs\WantedDocs\43534554","OK"
The 'Result' column will display Failure if the filename did not contain a CliCode in the name, otherwise OK
Thank you for all the replies. Using a combination of what has been advised, I now have the solution I need.
Many thanks for all the assistance. I've added an if else section to the file processing because I would only be interested in files that follow a specific naming convention (xx_xx_xx_clicode_xxx.ext). This is for a specific project where I'll be supplied with 1000s of files, most of which should follow the naming convention. So I'm checking the number of elements in the $values variable array to make sure it has at least 4 values (i.e. [3] exists as a value). Where it doesn't exist I'm writing the filename out to a log file.
This is the completed solution:
do {
$Folder = Read-Host 'Please enter a folder path'
} while (-not (Test-Path -Path $Folder -PathType Container))
$File = Read-Host 'Please enter a filename (no extension)'
$OutputFile = Join-Path -Path $Folder -ChildPath ($File + '.csv')
$SplitFile = Join-Path -Path $Folder -ChildPath ($File + '_Split.csv')
$CopyDir = Join-Path $Folder -ChildPath 'WantedDocs'
$logfile = "log.txt"
$log = Join-Path -Path $Folder -ChildPath $logfile
Get-ChildItem $Folder -Recurse -Include *.* | select Directory,FullName,Name | Export-Csv -Delimiter ',' -NoTypeInformation $OutputFile
$a = Import-Csv $OutputFile
$a | Add-Member 'CopyDir' -NotePropertyValue $CopyDir
$a | Select Directory,FullName,Name,CopyDir | Export-Csv -Delimiter ',' -NoTypeInformation $SplitFile
Foreach ($Row in $a)
{
$values = $Row.Name.split("_")
If ($values.Count -gt 3)
{
$tempfile = Join-Path -Path $CopyDir -ChildPath $values[3]
$OriginalFile = $($Row.FullName)
$CopyFile = $tempfile
New-Item -ItemType directory -Path $tempfile -Force
Copy-Item $OriginalFile -Destination $CopyFile -Force -Recurse
}
Else
{
Add-Content $log $Row.Name
}
}
Write-Output "Finished"
Many thanks once again. Much appreciated.
We have 1 main network share named the below, after the underscore there is 8 locations which are "abc$", "def$", "ghi$", "jkl$", "mno$", "pqr$", "stu$", "vwxyz$".
Depending on the users first letter in their username a folder is created with the which is the users home area.
Example username - AdamB will be put in Networkshare1_abc$.
Example 2 username - EdwardB will be put in Networkshare1_def$
Within this folder are the is a list of all users as a folder
Networkshare1_abc$
Networkshare1_def$
Networkshare1_ghi$
Networkshare1_jkl$
Networkshare1_mno$
Networkshare1_pqr$
Networkshare1_stu$
Networkshare1_vwxy$
I need a script that will
that will loop though only the top folder list the users
check to see if a text file exists and if it does delete it (have this working below)
$Wantedfile = "Networkshare1_stu$\user1\test.txt"
$timeStamp = (Get-Date -Format "dd-MM-yyyy-hh-mm")
$FileName = $timeStamp + ".txt"
if ((Test-Path $Wantedfile$FileName) -eq $false) {
Write-Host "file does not exists"
} elseif ((Test-Path $Wantedfile$FileName) -eq $true) {
Write-Host "file present..removing file"
Remove-Item $Wantedfile$FileName
}
The bit I am struggling with is the loop and how to get it to check each folder in the above locations as streamline as possible.
I am not sure if your files are really named like
"Networkshare1_stu$\user1\test.txt03-21-2017-19-41.txt"
but basically what you need is foreach loop. You need to get all the folders in the top folder. In $directories you will have abc$, def$ etc. folders. Then you will loop through each of them and search for the file using Where-Object.
$nameOfFile = "test.txt"
$fullPathFile = "\\Networkshare1_abc$\user1\test.txt"
$directories = Get-ChildItem "\\Networkshare1" -Directory
foreach($directory in $directories)
{
Write-Host "Searching in $directory.FullName ..."
$files = Get-ChildItem $directory -Recurse | Where-Object{$_.FullName -like "*$nameOfFile"}
# or to use fullPathFile
# $files = Get-ChildItem $directory -Recurse | Where-Object{$_.FullName -eq $fullPathFile}
foreach($file in $files)
{
Write-Host "Removing $file.FullName - continue?"
Read-Host ""
Remove-Item $file -Force
}
}
I am working on folders that contain many types and sizes of files within them. What I want to do is move files that are not contained in one folder into a new folder. I have embedded a picture link that helps illustrate what I am aiming to do.
I would like test123.pdf to be moved to a new location because it's not contained within the other folder. Below I have some code that simply compares the contents of each folder and outputs which file is out of place. I have been researching some things online, but have come up empty. Can anyone help me proceed?
Disclaimer: I know the path is wrong, but I can't show it for security reasons.
$Folder1 = Get-ChildItem -Recurse -path "Enter Path here"
$Folder2 = Get-ChildItem -Recurse -path "Enter the Path here"
Compare-Object -ReferenceObject $Folder1 -DifferenceObject $Folder2
It sounds like you want to compare the contents of two folders, ID the file(s) that are not present in both folders, and then move them to a third folder. To accomplish this, you can define your 2 paths in variables, compare the contents of the folders, grab the full path names of the different items, and then move them to the new destination.
$path1 = yourfirstpath
$path2 = yoursecondpathforcomparing
$path3 = yourdestinationpath
diff (ls $path1 -recurse) (ls $path2 -recurse) | ForEach {$_.InputObject.FullName} | Move-Item -Destination $path3
diff = Compare-Object , ls = Get-ChildItem
This will do the work, any file that is not in both Folder1 & Folder2 will be moved to Folder 3
$Folder1 = 'C:\folder1'
$Folder2 = 'C:\folder2'
$Folder3 = 'C:\folder3'
foreach ($file in Get-ChildItem -Recurse $Folder1)
{
if (-not(Test-Path "$Folder2\$file"))
{
Move-Item $file.FullName $Folder3
}
}
This will compare by file name, and maintain the directory structure the extra files were found in, and handle the recursion properly. The output of the code demonstrates the directory structure I tested with.
# be explict here to ensure everything is lowercase
# so when this is cut and pasted, it does not break
# when you enter the real path
$d1 = "d:\test\one".ToLower()
$d2 = "d:\test\two".ToLower()
$f1 = gci -Recurse $d1 -File | % {$_.FullName.ToLower()}
$f2 = gci -Recurse $d2 -File | % {$_.FullName.ToLower()}
"f1"
$f1
"`nf2"
$f2
$same = #()
$extra = #()
foreach ($f in $f2)
{
$f2tof1path = $f.Replace($d2, $d1)
if ($f1.Contains($f2tof1path) -eq $false)
{
$extra += $f
}
else {
$same += $f
}
}
"`nSame"
$same
"`nExtra"
$extra
"`nMOVE"
$folder3 = "d:\test\three"
foreach ($f in $extra)
{
# move files somewhere, create dir if not exists
$dest = $f.Replace($d2,$folder3)
$destdir = $(Split-Path -Parent -Path $dest)
if (!(Test-Path $destdir))
{
# remove quotes to do new-item, keep them to show what it will do
"New-Item -ItemType Directory -Force -Path $destdir"
}
# remove quotes to do move-item, keep them to show what it will do
"Move-Item $f $dest"
}
Output
f1
d:\test\one\1.txt
d:\test\one\2.txt
d:\test\one\sub\1.txt
d:\test\one\sub\2.txt
f2
d:\test\two\1.txt
d:\test\two\2.txt
d:\test\two\3.txt
d:\test\two\sub\1.txt
d:\test\two\sub\2.txt
d:\test\two\sub\3.txt
Same
d:\test\two\1.txt
d:\test\two\2.txt
d:\test\two\sub\1.txt
d:\test\two\sub\2.txt
Extra
d:\test\two\3.txt
d:\test\two\sub\3.txt
MOVE
New-Item -ItemType Directory -Force -Path d:\test\three
Move-Item d:\test\two\3.txt d:\test\three\3.txt
New-Item -ItemType Directory -Force -Path d:\test\three\sub
Move-Item d:\test\two\sub\3.txt d:\test\three\sub\3.txt
If this does not solve your problem, it should get you 99% there. Play around with the code, have fun, and good luck.
I need a batch file /script/tool to delete specified files in folder.
I have a folder with a lot of .xml files. It can contain files named difference of only a few characters (indicating the date).
aa_bb_000000001_2015_9_1.xml
aa_bb_000000001_2015_9_15.xml
aa_bb_000000001_2015_10_1.xml
aa_bb_000000002_2015_5_5.xml
aa_bb_000000002_2015_8_14.xml
aa_bb_000000002_2015_10_1.xml
aa_bb_000000005_2015_7_7.xml
.
.
The length of this part is 15 string
aa_bb_000000001
This part represents a date
2015_10_1
I need to delete all the files that part of the name with a date is earliest.
As a result batch should stay only files:
aa_bb_000000001_2015_10_1.xml
aa_bb_000000002_2015_10_1.xml
aa_bb_000000005_2015_7_7.xml
.
.
Here's one solution that's fairly short. To understand how the code works, it would be best to focus on what the Group-Object command does, what regular expressions are, and how they interact with the -match operator:
$Groups = Get-ChildItem "C:\XMLFiles\*.xml" | Group-Object {$_.Name.Substring(0, 15)}
$FilesToKeep = #{}
foreach ($Group in $Groups) {
$MaxDate = "00000000"
foreach ($FileInfo in $Group.Group) {
$FileInfo.name -match "(\d{4})_(\d{1,2})_(\d{1,2}).xml$" | Out-Null
$Date = $Matches[1]+([int]$Matches[2]).ToString("00")+([int]$Matches[3]).ToString("00")
if ($Date -gt $MaxDate) {
$MaxDate = $Date
$FilesToKeep[$Group.Name] = $FileInfo.FullName
}
}
}
Get-ChildItem "C:\XMLFiles\*.xml" | Where-Object {-not $FilesToKeep.ContainsValue($_.FullName)} | Remove-Item
Ihave started a simple script in order to move files of a certain prefix to a folder of the same name eg: W100_11.jpg W100_12.jpg in to folder W100.
Thanks to help from the answers below I have progressed and have a successful loop which can iterate through the file sin the folder, I am having issues with the -filter switch and when trying to use the move-item cmdlet I am getting errors
The current code is:
$sourceDir = read-host "Please enter source Dir:"
$format = read-host "Format to look for with . :"
#$length = read-host "length of folder name:"
gci -Path $sourceDir | % {
If( -not $_.PSIsContainer)
{
$path = $sourceDir + "\" + $_.Name.substring(0, 3)
$_
if(-not (Test-Path $path))
{
mkdir $path
}
move $_.fullname $path
}
}
I am still having issues when using the -filter switch. This is a partial solution to the issue
The code below shortens your for loop and shows an example of using substrings on file names. You get a FileInfo object from the Get-ChildItem (gci) call, so you need to use it's property Name to do substrings. See MSDN for more info on FileInfo.
$sourceDir = read-host Please enter source Dir
$format = read-host Format to look for with .
gci -Path $sourceDir -filter $format | % {
If( -not $_.PSIsContainer)
{
$path = $sourceDir + "\" + $_.Name.substring(0, 3)
if(-not (Test-Path $path))
{
mkdir $path
}
move $_ $path
}
}
If files names are always with this prefix "W100" you can easily take a substring in this way:
$a = "w100_12.ps1"
$a.Substring(0,4) # give W100
if prefix is not fix give more sample to help you solve it.
When piping use the built-in variable $_ to refer to the piped object, the '%' sign is an alias for 'for each':
get-childitem -Path $sourceDir -Filter $format -Recurse | % {If ($_.extension -eq ".ps1"){Write-Host $_.fullname}}
Answers above refer to substring, here is the MS Technet page that explains Powershell substring usage:
http://technet.microsoft.com/en-us/library/ee692804.aspx