我正在处理一个监视文件夹的批处理文件,并查看是否有任何文件。 我对批处理编程知之甚少,并继承了该文件,但是有没有人能让我知道是否有办法可以读取文件名并从中修剪一定数量的字符?
文件名总是如下所示:12345_20131031.txt _的右边还有8个字符,而“.txt”还有4个字符
我基本上只是想抓取文件名并修剪“_”和扩展名右边的所有内容,所以我只剩下:12345
下面是循环文件的代码。我一直在关注已经存在的东西,但我似乎得到的只是文件的完整路径/目录/文件名,我似乎无法得到正确的设置,只是给我文件名并修剪它。
任何帮助将不胜感激:
@ECHO OFF
set cnt= for %%a in (c:\Encoded_HL7_Vanderbilt\*.*) do set cnt=%%a
if !%cnt%!==!! exit
set "folder=%~1"
:: If more than 0 files exist begin, otherwise exit
FOR %%a IN (c:\Encoded_HL7_Vanderbilt_rob\*.*) DO (
:: need to get filename and trim everything to the right of "_" here
)
exit
:done
goto :EOF
答案 0 :(得分:1)
此示例显示如何获取文件名%%~nA
,然后通过_
下划线解析它。
@echo off
pushd "c:\Encoded_HL7_Vanderbilt_rob\"
for %%A in (*.*) do for /f "delims=_" %%B in ("%%~nA") do echo %%B
popd
答案 1 :(得分:1)
以下内容将遍历c:\ Encoded_HL7_Vanderbilt_rob \的目录列表,并在文件名中的 _ 之前回显所有内容。 使用/ a:-d和dir会使它不显示目录。
for /f "delims=_" %%A in ('dir /b /a:-d c:\Encoded_HL7_Vanderbilt_rob\') do (
echo %%A
REM %%A will be the the first part of the file name.
)