【问题标题】:Batch Increment File Name from Remote Folder从远程文件夹批量增加文件名
【发布时间】:2012-09-26 03:49:50
【问题描述】:

我想在 Windows 批处理或 powershell 脚本中使用日期和自动增量编号重命名 .TXT 文件。 IE。 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
)

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

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

【问题讨论】:

  • 名称的ABC 部分来自哪里?

标签: batch-file rename batch-rename


【解决方案1】:

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

还有一点可能有用也可能没用,我使用 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:请给我一些声誉,这样我就可以赞扬其他人为我提供的帮助!

谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 2015-12-03
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多