Disclamer: Previously我需要比较“上次修改”的日期,但是5分钟前我得到了一个新的要求,我需要与文件名上的日期进行比较,因为我解释自己吼叫。
想象一下,我有2个文件夹:
FolderData 会收到文件名,如果日期不是等于,则如下所示:
如果我今天运行.bat文件 ,我需要 将 SYS_PURCHASES_20170618.xls 复制到 FolderTemp 。必须通过从文件名( 20170618 )获取最后8个字符并检查它是否与 TODAYDATE - 1 相匹配来完成。
这对批处理文件是否可行?若然,怎么做?
如果Windows PowerShell(6.1)
之后也执行以下步骤,则接受move /y
的答案:
<section id="Details">
<h2>Spending Details</h2>
<br />
<table class="table table-hover">
<thead>
<tr>
<th>Date</th>
<th>Expense</th>
<th>Category</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="expense in expenses | orderBy: 'date'">
<td>{{ expense.date }}</td>
<td>{{ expense.text }}</td>
<td>{{ expense.category }}</td>
<td>{{ expense.amount > 0 ? '$' : ''}}{{ expense.amount }}</td>
</tr>
</tbody>
</table>
</section>
function ExpenseController($scope) {
$scope.appTitle = "Expense App using Angular";
$scope.saved = localStorage.getItem('expenses');
$scope.expenses = (localStorage.getItem('expenses') !== null) ? JSON.parse($scope.saved) : [];
localStorage.setItem('expenses', JSON.stringify($scope.expenses));
$scope.addExpense = function () {
$scope.expenses.push({
text: $scope.expenseText,
category: $scope.categoryText,
amount: $scope.amountText,
date: $scope.dateText
});
$scope.expenseText = '';
$scope.categoryText = '';
$scope.amountText = '';
$scope.dateText = '';
localStorage.setItem('expenses', JSON.stringify($scope.expenses));
};
$scope.countExpenses = function () {
var count = 0;
angular.forEach($scope.expenses, function (expense) {
count += 1;
});
return count;
};
$scope.totalAmountByCategory = function (category) {
var sum = 0;
angular.forEach($scope.expenses, function (expense) {
sum += expense.category == category ? parseFloat(expense.amount) : 0;
});
return sum;
};
$scope.totalAmount = function () {
var sum = 0;
angular.forEach($scope.expenses, function (expense) {
sum += expense.amount > 0 ? parseFloat(expense.amount) : 0;
});
return sum;
};}
) - 我相信在执行第2步时可以执行此操作,不需要第三个文件夹。答案 0 :(得分:1)
只要脚本在闰年的3月1日没有运行,FolderData中所有与“昨天”对应的最后一位数字的文件将是
我的系统使用DDMMYYYY格式。交换前两个变量的值,将其设置为MMDDYYYY。
setlocal enabledelayedexpansion
set day=%date:~,2%
set month=%date:~3,2%
set year=%date:~-4%
echo.%day%|findstr /r /b "0">nul&&set day=%day:~1%
echo.%month%|findstr /r /b "0">nul&&set month=%month:~1%
set /a day-=5
if %day% lss 1 (
set /a day+=30
set /a month-=1
if !month!==2 (set /a day-=2
set /a leap=year%%4
if !leap!==0 set day+=1
)
for /l %%# in (1 2 7) do if %month%==%%# set /a day+=1
for %%# in (8 10 12) do if %month%==%%# set /a day+=1
if !month!==0 (
set month=12
set /a year-=1
)
)
set day=0%day%
set month=0%month%
for %%# in (C:\FolderData\*) do (
set name=%%~n#
if "!name:~-8!"=="%year%%month:~-2%%day:~-2%" (
copy "%%#" "C:\FolderTemp\!name:~,-9!%%~x#"
)
)
如果我误解了某事,请告诉我。
编辑:修改了脚本,以便考虑返回多天。
答案 1 :(得分:1)
使用以下powershell脚本文件夹2是不必要的, 你没有为folder3命名 - 所以请根据你的环境进行调整。
$Folder1 = "C:\Test\2017\06\20"
$Folder3 = "C:\Test\2017\06\20\Destination"
$YesterDay = (Get-Date).AddDays(-1).ToString('yyyyMMdd')
Get-ChildItem -Path $Folder1 -Filter "SYS_PURCHASES_$YesterDay.*"|Where{ !$_.PSisContainer}|
Copy-Item -Destination {Join-Path $Folder3 (($($_.BaseName) -replace '_\d{8}$')+$_.Extension)} -whatif
以上脚本的原始输出仅插入换行符
PS C:\test\2017\06\20> ls
Directory: C:\test\2017\06\20
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 06/20/2017 18:13 Destination
-a---- 06/20/2017 18:01 1044 SO_44639321.ps1
-a---- 06/20/2017 00:56 24 SYS_PURCHASES_20170617.xls
-a---- 06/20/2017 00:56 24 SYS_PURCHASES_20170618.xls
-a---- 06/20/2017 01:07 24 SYS_PURCHASES_20170619.txt
-a---- 06/20/2017 00:56 24 SYS_PURCHASES_20170619.xls
PS C:\test\2017\06\20> .\SO_44639321.ps1
What if: Performing the operation "Copy File" on target
"Item: C:\Test\2017\06\20\SYS_PURCHASES_20170619.txt Destination: C:\Test\2017\06\20\Destination\SYS_PURCHASES.txt".
What if: Performing the operation "Copy File" on target
"Item: C:\Test\2017\06\20\SYS_PURCHASES_20170619.xls Destination: C:\Test\2017\06\20\Destination\SYS_PURCHASES.xls".
PS C:\test\2017\06\20> type .\SO_44639321.ps1
如果系统上的输出正确,请删除最后一行末尾的-whatif
参数。
答案 2 :(得分:0)
组合的Batch / PowerShell方法。
日期计算的Powershell和其余的纯批次
@Echo Off&Setlocal EnableDelayedExpansion
Set "Folder1=C:\Test\2017\06\20"
Set "Folder3=C:\Test\2017\06\20\Destination"
For /f "usebackq delims=" %%A in (
`Powershell -NonI -Nop -Com "(Get-Date).AddDays(-1).ToString('yyyyMMdd')"`
) Do Set YesterDay=%%A
For /F "Delims=" %%A in (
'Dir /B/A-D "%Folder1%\*_%YesterDay%.*" '
) Do (
Set "File=%%~nA"
Set "File=!File:_%YesterDay%=!"
Echo Copy "%%~fA" "%Folder3%\!File!%%~xA"
)
示例输出
> SO_44639321.cmd
Copy "C:\Test\2017\06\20\SYS_PURCHASES_20170619.txt" "C:\Test\2017\06\20\Destination\SYS_PURCHASES.txt"
Copy "C:\Test\2017\06\20\SYS_PURCHASES_20170619.xls" "C:\Test\2017\06\20\Destination\SYS_PURCHASES.xls"
如果输出看起来没问题,请删除复制前面的回声。