Hi - This does almost exactly what I Need it to. It goes through a file that I provide and copies the files. However, it does not respect directory structure and will instead just copy all files to the root of the destination.
Would you please help me solve this so that the files provided in my filename.txt will copy and maintain directory structure.
I’m not really sure that I got what you try to do and why you do how you do but some general tips might guide you to a succesfull end.
I assume you did not provide the complete code - you use a variable $WFPROFILE but in the code you show it’s not filled with something.
As far as I think I got it you’re using a text file with structured information to control what your script should do. You get a single line cut it with a regex pattern into pieces, transform it and then use it.
As Powershell is able to deal really easily with structured text you could use
Import-CSV
to get what you need from your text file and feed your loop with it.
Last but not least: think about formatting your code nicely. Specially when you show it to other people. Code indentation helps understanding more easily. And in a few weeks YOU are other people.
function FILEBACKUP {
param(
$Path = "C:\Source\filebackup.txt"
)
$Files = Import-CSV -Path $Path -Delimiter "%" -Header Option1,Option2,Option3
Foreach ($File in $Files) {
## do something with the options ... par example:
## $Source = $File.Option1 + $File.Option2
## $Destination = $File.Option2 + $ENV:$File.Option3
Copy-Item -Path $Source -Destination $Destination
}
}
… hope it helps
PS:> (79,108,97,102|%{[char]$_})-join’’
I apologize for not posting the entire code. However I will post it now. I am not having problems with the rest of the script just the FILEBACKUP part.
Lets say one of the files I was calling was office 365 - files OK well this script will create a folder based on that file name and then parse through the file and copy the files in the paths of the file IE:
%APPDATA%\Microsoft\Templates*.dotm
The problem is that it does copy the files but it copies them all to the root of the destination. I need to make my FILEBACKUP function respect the entire path. So it should be copying the files to DEST>Microsoft>Templates
There are a lot of examples regarding what you are asking. Check out this MCP mag article. I’m just searching for “powershell copy keep folder structure” and there are a lot of examples.
your copy-item is missing -recurse -force, a leading comma in your ‘controler file’ will not work and if you already have …\Microsoft\Excel in your ‘controler file’ another folder like …\Microsoft\Excel*.xlsb does not make sense because it’s already included in the other one
1: If I change my copy-item line to include -recurse -force it still does not respect directory structure. Example I am copying the files ending in *.officeUI which live in the office folder. But they don’t get copied to the office folder, the go to the root of the destination.
2: If I try the other way and using the $ENV:APPDATA it completely errors out and says it can’t find a drive matching $ENV:APPDATA
1: If I change my copy-item line to include -recurse -force it still does not respect directory structure. Example I am copying the files ending in *.officeUI which live in the office folder. But they don't get copied to the office folder, the go to the root of the destination.
Of course Powershell will not copy the folder if you specify the files to be copied. ;-)
Maybe you have to change your controler file to provide the folder you are looking for AND the file pattern:
‘controler file’: