使用批处理文件获取文件名

时间:2018-02-01 12:48:41

标签: windows batch-file

我已经编写了批处理文件来提取文件夹中多个文件的最后8个字符。但是该批处理文件没有给出期望的结果。我的文件夹包含下面提到的文件名sub_rachit_01.pdf和sub_kapoor_02.pdf。我想从文件夹中提取rachit_01和kapoor_02作为变量。批处理文件碎片如下:

@echo off

set /p location=Please enter location of .pdf files:

SETLOCAL ENABLEDELAYEDEXPANSION 
for /f "tokens=*" %%a in ('dir /b %location%\*.pdf') do (
set filename=%%~na

set file=%filename:~-8%
echo %file%
)

pause

2 个答案:

答案 0 :(得分:1)

根据我的评论:

@Echo Off
:AskInput
ClS
Set "location="
Set /P "location=Please enter location of .pdf files: "
If Not Exist "%location%\*.pdf" GoTo AskInput

SetLocal EnableDelayedExpansion
For %%A In ("%location%\*.pdf") Do (Set "filename=%%~nA"
    Set "filename=!filename:~-8!"
    Echo !filename!)

Pause

我已使用普通For /F循环替换了您的For循环,在那里为用户输入添加了一点保护,并删除了使用其他变量,更改了{{ 1}}动态。

答案 1 :(得分:0)

首先,你characterLayoutRight = (RelativeLayout.LayoutParams) character_image_view.getLayoutParams(); characterLayoutRight.removeRule(RelativeLayout.ALIGN_PARENT_LEFT); characterLayoutRight.addRule(RelativeLayout.RIGHT_OF, R.id.message_container); messageLayoutRight = (RelativeLayout.LayoutParams) messageContainer.getLayoutParams(); messageLayoutRight.removeRule(RelativeLayout.RIGHT_OF); messageLayoutRight.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 但从不使用它。 enabledelayedexpansion也会显示不包含扩展名的名称。如果文件实际上都包含%%~na,则可以搜索并替换。

sub_

执行@echo off set /p location=Please enter location of .pdf files: SETLOCAL ENABLEDELAYEDEXPANSION for /f "tokens=*" %%a in ('dir /b %location%\*.pdf') do ( set "filename=%%~na" set "filename=!filename:sub_=!" echo !filename! ) endlocal pause

for /?