我想复制并重命名一个复制的文件。文件扩展名为“.ini”。我有多个文件夹(Banks),年份为 1985-2007。每个文件夹(1985-2007)都包含 cfgfiles 文件夹,其中包含一个 ini 文件,我想将其复制并粘贴到 Russell ->1985-2007 文件夹 -> cfgfiles 中。
我的代码:
Function RenameMoveFile($oldlocationPath, $newlocationPath, $oldfileName, $newfileName, $Point, $year, $extension)
{
$old = $oldlocationPath + $oldfileName + "_" + $Point + "_" + $year + $extension
$new = $newlocationPath + $newfileName + "_" + $Point + "_" + $year + $extension
copy-Item $old
Rename-Item $old $new
}
$year = 1985..2007
Foreach ($i in $year)
{
RenameMoveFile -oldlocationPath "C:\SNOWPACK\Samarth\1_Banks\Point1\$i\cfgfiles\" - newlocationPath "C:\SNOWPACK\Samarth\10_Russell\Point1\$i\cfgfiles\"
-oldfileName "Banks" -newfileName "Russell" -Point "P1" -$year "$i" -extension ".ini"
}
答案1
这只会以递归方式从您在代码中输入的根目录递归地查看您的文件夹。
我希望这能让你开始。但我不明白问题其余部分的要求。
$looking_for = "*.ini" # extension looking for
$path_from = "" # root of search;
$path_to = "" # where to drop found files, wont retain folder heirarchy;
$all_files_in_dir = Get-ChildItem -path $path_from -recurse -Include
$looking_for | ForEach-Object -Process {$_.FullName};
New-Item -ItemType Directory -Force -Path $path_to # Create dest for you files
Foreach ($i in $year) { ## create your year folders
$p = Join-Path $path_to $i
New-Item -ItemType Directory -Force -Path $p
}
foreach( $i in $all_files_in_dir ) {
if ( $i -like $looking_for ) {
Copy-Item $i $path_to
if ( $? ) { # was last command succsesful?
echo "$i copied to $path_to
}
}
else {
echo "ERROR: "
throw $error[0].Exception
}
}
}