根据目录中包含的文本重命名目录中的所有文件
Rename all files in directory based on text they contain
我正在尝试遍历文件夹中的所有 .lss 文件,并获取两个标签之间存在的字符串,保存该值并使用该字符串重命名文件。
示例:
42982934829.lss -> contains string:
<surveyls_title><![CDATA[J.3200-1118 - Project Title]]></surveyls_title>
Rename to `J.3200-1118 - Project Title.lss`
这是我到目前为止的内容,但我担心我的语法严重不正确..
@Echo off
Set Folder=X:\RenameTest
Set Files=*.lss
PushD %Folder%
For %%A in (%Files%) Do For /f %%B IN (
'findstr "<surveyls_title>.*</surveyls_title>" "%ProjectTitle%"'
) Do Call :Rename ..
PopD
Goto :Eof
:Rename
Echo Ren %1 "%ProjectTitle%"
我想这就是你想要的。
@echo off
pushd "X:\RenameTest"
for %%a in (*.lss) do for /f "tokens=3*delims=[]" %%i in ('type "%%a" ^| find "</surveyls_title>"') do echo ren "%%~a" "%%~i%%~xa"
popd
如果您对打印结果感到满意,只需从行中删除 echo
。
您还可以使用正则表达式提取标题:Demo Here
@echo off & color 0A
Title Extract Title using Regex
Set "InputFile=42982934829.lss"
Call :Extract_Title "%InputFile%" Title
Echo %Title%
Pause & Exit
::----------------------------------------------------------------------------------------
:Extract_Title <InputFile> <Title to be Set>
(
echo WScript.StdOut.WriteLine Extract_Title(Data^)
echo Function Extract_Title(Data^)
echo Data = WScript.StdIn.ReadAll
echo Set re = New RegExp
echo re.Global = True
echo re.IgnoreCase = True
echo re.Pattern = "\[CDATA\[(.*?)\]\]"
echo For Each Match in re.Execute(Data^)
echo Title = Match.SubMatches(0^)
echo Next
echo Extract_Title = Title
echo End Function
)>"%tmp%\%~n0.vbs"
for /f "delims=" %%A in ('cscript /nologo "%tmp%\%~n0.vbs" ^< "%~1"') do set "%2=%%A"
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
Exit /B
::----------------------------------------------------------------------------------------
我正在尝试遍历文件夹中的所有 .lss 文件,并获取两个标签之间存在的字符串,保存该值并使用该字符串重命名文件。
示例:
42982934829.lss -> contains string:
<surveyls_title><![CDATA[J.3200-1118 - Project Title]]></surveyls_title>
Rename to `J.3200-1118 - Project Title.lss`
这是我到目前为止的内容,但我担心我的语法严重不正确..
@Echo off
Set Folder=X:\RenameTest
Set Files=*.lss
PushD %Folder%
For %%A in (%Files%) Do For /f %%B IN (
'findstr "<surveyls_title>.*</surveyls_title>" "%ProjectTitle%"'
) Do Call :Rename ..
PopD
Goto :Eof
:Rename
Echo Ren %1 "%ProjectTitle%"
我想这就是你想要的。
@echo off
pushd "X:\RenameTest"
for %%a in (*.lss) do for /f "tokens=3*delims=[]" %%i in ('type "%%a" ^| find "</surveyls_title>"') do echo ren "%%~a" "%%~i%%~xa"
popd
如果您对打印结果感到满意,只需从行中删除 echo
。
您还可以使用正则表达式提取标题:Demo Here
@echo off & color 0A
Title Extract Title using Regex
Set "InputFile=42982934829.lss"
Call :Extract_Title "%InputFile%" Title
Echo %Title%
Pause & Exit
::----------------------------------------------------------------------------------------
:Extract_Title <InputFile> <Title to be Set>
(
echo WScript.StdOut.WriteLine Extract_Title(Data^)
echo Function Extract_Title(Data^)
echo Data = WScript.StdIn.ReadAll
echo Set re = New RegExp
echo re.Global = True
echo re.IgnoreCase = True
echo re.Pattern = "\[CDATA\[(.*?)\]\]"
echo For Each Match in re.Execute(Data^)
echo Title = Match.SubMatches(0^)
echo Next
echo Extract_Title = Title
echo End Function
)>"%tmp%\%~n0.vbs"
for /f "delims=" %%A in ('cscript /nologo "%tmp%\%~n0.vbs" ^< "%~1"') do set "%2=%%A"
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
Exit /B
::----------------------------------------------------------------------------------------