批处理文件用于将文件重命名为当前周的星期三日期,后跟一串文本。

huangapple go评论47阅读模式
英文:

Batch file to rename a file to include the Wednesday's date of the current week followed by a string of text

问题

I am trying to create a batch file that can rename a .pdf file to include the Wednesday's date of the current week followed by a string of text. The current example template for the filename should be "YYYY-M-D Document-03.pdf". So an example filename should be renamed to "2023-6-14 Document-03.pdf".

Below are the code I am working with:

@echo off
setlocal EnableDelayedExpansion

set "dayOfWeek=Wednesday"
for /f "tokens=1-3 delims=/ " %%a in ('date /t') do (
    set "day=%%a"
    set "month=%%b"
    set "year=%%c"
)

for /f "tokens=1-3 delims=: " %%a in ('time /t') do (
    set "hour=%%a"
    set "minute=%%b"
    set "second=%%c"
)

set "dateString=%year%-%month%-%day%"
set "newFileName=%dateString% SRP-B332-4.4.1.ab.pdf"

ren *.pdf "%newFileName%"

echo Done!

The code currently incorrectly renames the filename as:

14-06-Wed Document-03.pdf

Help on this would be greatly appreciated!

英文:

I am trying to create a batch file that can rename a .pdf file to include the Wednesday's date of the current week followed by a string of text. The current example template for the filename should be "YYYY-M-D Document-03.pdf". So an example filename should be renamed to "2023-6-14 Document-03.pdf".

Below are the code I am working with:

@echo off
setlocal EnableDelayedExpansion

set "dayOfWeek=Wednesday"
for /f "tokens=1-3 delims=/ " %%a in ('date /t') do (
    set "day=%%a"
    set "month=%%b"
    set "year=%%c"
)

for /f "tokens=1-3 delims=: " %%a in ('time /t') do (
    set "hour=%%a"
    set "minute=%%b"
    set "second=%%c"
)

set "dateString=%year%-%month%-%day%"
set "newFileName=%dateString% SRP-B332-4.4.1.ab.pdf"

ren *.pdf "%newFileName%"

echo Done!

The code currently incorrectly renames the filename as:

14-06-Wed Document-03.pdf

Help on this would be greatly appreciated!

答案1

得分: 2

使用PowerShell作为辅助工具,以下部分获取您需要的%dateString%吗?

@Echo Off
SetLocal EnableExtensions
Set "dateString="
For /F %%G In ('%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe
 -NoProfile -Command "$Today=(Get-Date).Date;"
 "$Today.AddDays([Int][DayOfWeek]::WednesDay-$Today.DayOfWeek).ToString('yyyy-M-d')"
 2>NUL') Do Set "DateString=%%G"
If Not Defined dateString GoTo :EOF
Rem Your code using the resulting variable named dateString goes below.
Echo %dateString%
Pause

如果还需要进一步帮助,请告诉我。

英文:

Does this, using PowerShell as a helper, get you what you need for your %dateString%?

@Echo Off
SetLocal EnableExtensions
Set "dateString="
For /F %%G In ('%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe
 -NoProfile -Command "$Today=(Get-Date).Date;"^
 "$Today.AddDays([Int][DayOfWeek]::WednesDay-$Today.DayOfWeek).ToString('yyyy-M-d')"
 2^>NUL') Do Set "DateString=%%G"
If Not Defined dateString GoTo :EOF
Rem Your code using the resulting variable named dateString goes below.
Echo %dateString%
Pause

答案2

得分: 0

PowerShell有时在我的电脑上启动时可能会很慢,所以通常使用cmd方法会快得多。

调用批处理文件比我运行的大多数文件都要慢(因为使用JavaScript调用系统),但对我来说比PowerShell要快。

SetWed.cmd

@echo off
call jTimestamp  -C "OUD=-3,OD=wed:0" /F "{yyyy}-{mm}-{dd}" -r string
echo ren "%~f1" "%string% %~nx1"

一旦测试通过了该偏移值(OUD是回到上周三,距离当前周的周四、周五、周六-3天的最近的周三,否则它将寻找下一个周三),然后删除最后一行 "echo"。

DBenhams jtimestamp v2.1可以从https://www.dostips.com/forum/viewtopic.php?t=7523下载或复制。

使用拖放单个文件或在包含一组文件的文件夹中运行

例如,从命令行运行(在批处理文件中使用%%作为双重百分号)

for %f in (?????.pdf) do @setWed %f

结果

>dir 2023*.*
 Volume in drive C is OS
 Directory of C:\Users\my\Downloads\Apps

2023-06-14  03:12            28,330 2023-06-14 data.pdf
2023-06-14  03:50            83,823 2023-06-14 data1.pdf
2023-06-11  20:10            70,326 2023-06-14 mypdf.pdf
2023-06-11  20:16            70,385 2023-06-14 print.pdf
               4 File(s)        252,864 bytes
英文:

Powershell can sometimes be slow to start when invoked on my PCs so often a cmd approach is much faster.

This calling a batch file is slower than most I run (since calling system with javascript) but for me it is faster than PowerShell.

SetWed.cmd

@echo off
call jTimestamp  -C "OUD=-3,OD=wed:0" /F "{yyyy}-{mm}-{dd}" -r string
echo ren "%~f1" "%string% %~nx1"

Once tested for that offset (OUD is go back -3 days for nearest Wednesday on Thu,Fri,Sat of current week days, otherwise it will hunt for next Wednesday) then remove the last line "echo"

DBenhams jtimestamp v2.1 can be downloaded or copied from https://www.dostips.com/forum/viewtopic.php?t=7523

Usage drag and drop a single file or run in folder with set of files

For example from command line (use %% as double in a batch file)

for %f in (?????.pdf) do @setWed %f

ren "C:\Users\my\Downloads\Apps\data.pdf" "2023-06-14 data.pdf"
ren "C:\Users\my\Downloads\Apps\data1.pdf" "2023-06-14 data1.pdf"
ren "C:\Users\my\Downloads\Apps\mypdf.pdf" "2023-06-14 mypdf.pdf"
ren "C:\Users\my\Downloads\Apps\print.pdf" "2023-06-14 print.pdf"

result

>dir 2023*.*
 Volume in drive C is OS
 Directory of C:\Users\my\Downloads\Apps

2023-06-14  03:12            28,330 2023-06-14 data.pdf
2023-06-14  03:50            83,823 2023-06-14 data1.pdf
2023-06-11  20:10            70,326 2023-06-14 mypdf.pdf
2023-06-11  20:16            70,385 2023-06-14 print.pdf
               4 File(s)        252,864 bytes

huangapple
  • 本文由 发表于 2023年6月15日 02:24:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定