我对powershell脚本有疑问。我有一个每日任务调度程序,它导出名为12122_000000.txt的文件并将其存储在C:\驱动器上。我在C:\ Report还有另一个Report文件夹。在该Report文件夹中,我有很多文件名为12122_000001.txt,12122_000002.txt,12122_0000003.txt,12122_0000004.txt等等。
我想要一个脚本来检查Report文件夹以找到最新的文件名。然后,将C:\上的文件名12122_000000.txt移动到C:\ Report并将其重命名为最高名称增加1.文件名格式必须为12122_xxxxxx.txt
我试过编写脚本但是没有用。请帮助我获得PowerShell的知识。
$SourceFile = "C:\12122_000000.txt"
$DestinationFolder = "C:\Report"
if (Test-Path $SourceFile)
{
$latest = Get-ChildItem -Path $DestinationFolder| Sort-Object Name -Descending | Select-Object -First 1
$i = 1
Move-Item -path $SourceFile -destination $latest.basename + $i++ + ".txt")
}
谢谢
答案 0 :(得分:3)
这是增加此文件名并移动文件的简便方法:
$SourceFile = "C:\12122_000000.txt"
$DestinationFolder = "C:\Report"
if (Test-Path $SourceFile)
{
$latest = Get-ChildItem -Path $DestinationFolder| Sort-Object Name -Descending | Select-Object -First 1
#split the latest filename, increment the number, then re-assemble new filename:
$newFileName = $latest.BaseName.Split('_')[0] + "_" + ([int]$latest.BaseName.Split('_')[1] + 1).ToString().PadLeft(6,"0") + $latest.Extension
Move-Item -path $SourceFile -destination $DestinationFolder"\"$newFileName
}
答案 1 :(得分:0)
$ i变量应该来自$ latest中存储的实际文件名:
$prefix, $i = $latest.baseName -split '_'
$i = '{0:d6}' -f ([int]$i + 1)
参数应该括在括号中,因为它是一个表达式:
Move-Item $SourceFile -dest (Join-Path $DestinationFolder "$prefix`_$i.txt")
答案 2 :(得分:0)
您可以扫描目标目录以提取所有文件索引的列表,使用#include <iostream>
#include <sstream>
using namespace std;
class Streamer // class for one line string generation
{
public:
Streamer& clear() // clear content
{
ss.str(""); // set to empty string
ss.clear(); // clear error flags
return *this;
}
template <typename T>
friend Streamer& operator<<(Streamer& streamer,T str); // add to streamer
string str() // get current string
{ return ss.str();}
private:
stringstream ss;
};
template <typename T>
Streamer& operator<<(Streamer& streamer,T str)
{ streamer.ss<<str;return streamer;}
Streamer streamer; // make this a global variable
class MyTestClass // just a test class
{
public:
MyTestClass() : data(0.12345){}
friend ostream& operator<<(ostream& os,const MyTestClass& myClass);
private:
double data;
};
ostream& operator<<(ostream& os,const MyTestClass& myClass) // print test class
{ return os<<myClass.data;}
int main()
{
int i=0;
string s1=(streamer.clear()<<"foo"<<"bar"<<"test").str(); // test strings
string s2=(streamer.clear()<<"i:"<<i++<<" "<<i++<<" "<<i++<<" "<<0.666).str(); // test numbers
string s3=(streamer.clear()<<"test class:"<<MyTestClass()).str(); // test with test class
cout<<"s1: '"<<s1<<"'"<<endl;
cout<<"s2: '"<<s2<<"'"<<endl;
cout<<"s3: '"<<s3<<"'"<<endl;
}
CmdLet识别最大索引,添加一个然后移动文件。
我这样做是为了设置测试环境:
Measure-Object
这将0索引文件模式化为目标目录:
$rootDirectory = "C:\test"
$filePrefix = "12122_"
$fileExt = ".txt"
$filePathFmt = "{3}\{1}{0,6:000000}{2}"
@(1..12) | % { New-Item ([String]::Format($filePathFmt, $_, $filePrefix, $fileExt, $rootDirectory)) -Type File } | Out-Null
New-Item ([String]::Format($filePathFmt, 0, $filePrefix, $fileExt, "C:")) -Type File | Out-Null
输出:
$nextIdx = (Get-ChildItem $rootDirectory -Filter "${filePrefix}*${fileExt}" | Select-Object -ExpandProperty Name | ForEach-Object { [int]($_.Replace($filePrefix,"").Replace($fileExt, "")) } | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum) + 1
Move-Item ([String]::Format($filePathFmt, 0, $filePrefix, $fileExt, "C:")) ([String]::Format($filePathFmt, $nextIdx, $filePrefix, $fileExt, "$rootDirectory"))