在Windows批处理编程中读取文件名 [英] Read File Name in Windows Batch Programming

查看:62
本文介绍了在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

这篇关于在Windows批处理编程中读取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆