0

我想在 Windows 批处理或 powershell 脚本中使用日期和自动增量编号重命名 .TXT 文件。即 20121004ABC.txt、20121004ABC_02.txt、20121004ABC_03.txt。. .

棘手的部分是这些文件在上传时会移动到不同的文件夹。如果文件在存档文件夹中具有相同的日期,我希望递增的数字继续......

SO 20121004ABC.txt, 20121004ABC_02.txt, 20121004ABC_03.txt 被上传并移动到 C:\return\archive 那天晚些时候 4 个新的 .txt 文件被放入 c:\return,我想运行一个批处理文件来命名它们20121004ABC_04.txt、20121004ABC_05.txt、20121004ABC_06.txt、20121004ABC_07.txt

第二天,递增的数字将重新启动,20121005ABC.txt,20121004ABC_02.txt 到目前为止,我有:

setlocal enabledelayedexpansion
SET date=%date:~-4,4%%date:~-10,2%%date:~-7,2%
set /a count=0
for /f "tokens=*" %%a in ('dir /b /od *.txt') do (
ren %%a %date%_0!count!.txt
set /a count+=1
)

但这显然只是一个开始,并没有回答我的很多问题!

- 不会继续从存档文件夹中增加数字 - 我相信循环功能和覆盖其他文件等存在一些未知问题!

4

1 回答 1

0

正如我提到的,我对编码和学习很陌生。我想出了一种技术来解决我的问题,它可能很混乱,但似乎可以解决问题!我想分享其他人有类似问题的情况。此外,如果有人对此代码有任何批评,请这样做,我会将其视为建设性的批评,并很乐意在我这边改进代码。

另外,正如另一点可能有用也可能没有帮助,我使用 SQL 查询来导出 txt 文件,然后使用 Cygwin

谢谢!

c:\cygwin\bin\bash C:\s3\return\split.sh 'Shell Script to Split DB Export file into 25000 line Pieces
CD C:\s3\return\Returned_ARCHIVED 'Directory of Archive of Files To continue Counting from
for /f "tokens=*" %%a in ('dir /b /od') do set newest=%%a 'Newest File in Archive Directory
Set Name=%newest:~0,8% 'Take only the important part of the File name
Set /a FileNum=%newest:~12,2% 'Take only the incremental number part off the filename
SET date=%date:~-4,4%%date:~-10,2%%date:~-7,2%
If %date% == %Name% (set /a count=%filenum%+1) Else (set /a count=0) 

'if the latest file in the archive is from today date +1 ifnot Start at 0

cd C:\s3\return 'Directory if Files that need renamed
for /f "tokens=*" %%a in ('dir /b /od ABC_*') do (
ren %%a %date%ABC_0!count!.txt
set /a count+=1
)

PS:请给我一些声誉,这样我就可以将他们提供给我的帮助归功于其他人!

谢谢!

于 2012-10-11T14:53:04.580 回答