在Windows批处理编程中读取文件名
我想在Windows批处理编程中读取文件名.我正在尝试使用其他方法,但失败了,请帮助.
I want to read the name of a file in windows batch programming. I am trying by using different methods but failed please help.
场景已在下面给出.
我在一个文件夹中有不同的文件,但所有文件的文件名长度都相同. 例如
I have different files in a folder but the length for filename is same for all files. E.g.
1000342578_30062011.PDF
1000342329_30062011.PDF
我只想将_
之后和.PDF
(例如30062011
)部分之前的部分保存在变量中.
And I just want to save the part after _
and before .PDF
(e.g. 30062011
) part in a variable.
下面只是尝试,但我无法通过.
Below are just tries but I am not able to get through.
@echo off
echo.Date : %date%
echo.Year : %date:~10,4%
dir /s /b C:\Users\zeeshando\Desktop\*.txt
for %%x in (c:\temp\*.xls) do echo %%x
lfnfor off
for %%x in (*.*) do echo %%x > filelist.txt
PAUSE
for %i in (C:\Users\zeeshando\Desktop) do
echo %~ni
PAUSE
更新
我当前的代码,基于@Alex K.的答案:
My current code, based on @Alex K.'s answer:
@setlocal enabledelayedexpansion
for %%x in (C:\Hi\*.*) do (
set fn=%%x
set fn=!fn:~22,8!
echo !fn!
call:handler
)
goto:eof
:handler
echo !fn!
copy C:\Hi\*.* E:\!fn!
goto:eof
PAUSE
但是它没有复制文件.请检查上面的代码.
But it is not copying the files. Please check the above code.
您可以使用_和在for循环中执行此操作.作为分隔符,如下所示:
You could do this in your for loop using _ and . as delimiters, like this:
FOR /F "tokens=2 delims=_." %%i IN ('DIR /b C:\Users\zeeshando\Desktop\*_*.*') DO ECHO %%i