在 Windows 批处理中读取文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6664659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:24:16  来源:igfitidea点击:

Read File Name in Windows Batch Programming

windowsbatch-file

提问by Mareena

I want to read the name of a file in windows batch programming. I am trying by using different methods but failed please help.

我想在 Windows 批处理编程中读取文件的名称。我正在尝试使用不同的方法但失败了请帮忙。

Scenario has been given below.

下面给出了场景。

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

And I just want to save the part after _and before .PDF(e.g. 30062011) part in a variable.

我只想在变量中保存(例如)部分之后_和之前的部分。.PDF30062011

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


UPDATE

更新

My current code, based on @Alex K.'s answer:

我当前的代码,基于@Alex K. 的回答:

@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.

但它不是复制文件。请检查上面的代码。

回答by Dave

You could do this in your for loop using _ and . as delimiters, like this:

您可以在 for 循环中使用 _ 和 . 作为分隔符,像这样:

FOR /F "tokens=2 delims=_." %%i IN ('DIR /b C:\Users\zeeshando\Desktop\*_*.*') DO ECHO %%i

回答by Andriy M

@ECHO OFF
SET "destpath=destinationPath"
FOR %%f IN (sourcePath\*.*) DO CALL :process "%%f"
GOTO :EOF

:process
SET srcfile=%1
SET "name=%~n1"
SET "name=%name:*_=%"
COPY %srcfile% "%destpath%\%name%"

The SET "name=%~n1"line extracts from the full path just the name without extension.

SET "name=%~n1"行仅从完整路径中提取没有扩展名的名称。

The SET "name=%name:*_=%"line deletes the _character and everything before it in the value of nameand stores the result back to name.

SET "name=%name:*_=%"行删除了_值中的字符及其之前的所有内容,name并将结果存储回name.

Ultimately, before executing the COPYcommand, nameonly contains that part of the original filename which is between _and the extension.

最终,在执行COPY命令之前,name只包含原始文件名中_扩展名和扩展名之间的那部分。

回答by Alex K.

As they are of a fixed length you can use :~startpos,lensyntax;

由于它们的长度是固定的,因此您可以使用:~startpos,len语法;

set fn=1000342578_30062011.PDF
set fn=%fn:~11,8%
echo %fn%

>30062011

In a for loop;

在 for 循环中;

setlocal enabledelayedexpansion
for %%x in (*.*) do (
    set fn=%%x
    set fn=!fn:~11,8!
    call:handler
)
goto:eof

:handler
    echo !fn!
goto:eof