For 循环读取带空格的文件名 [英] For loop reading file names with spaces

查看:79
本文介绍了For 循环读取带空格的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扫描目录中的文件以获取其中的文本,但是每当我遇到一个从 Windows 中将-Copy"添加到其末尾的文件时,程序将不会读取它.我试过在传递的名称中使用引号,但没有使用骰子.

I'm trying to scan files in directorys for text inside, but whenever I come across a file that has the ' - Copy' added to the end of it from windows, the program will not read it. I've tried using quotes within the name being passed, but no dice.

FOR /R %%F in (*.CDP) do (
    for /f "tokens=*" %%a in (%%~nxF) do (

我一直在使用此代码并且对我看到的典型文件没有任何问题.但是,如果它类似于 dummy_file - Copy,我会从程序中收到一条错误消息,提示系统找不到文件 dummy_file".期间包括.如果我使用

I've been using this code and have no issues with the typical files Im seeing. however if its something like dummy_file - Copy, I will get an error from the program saying 'System cannot find the file dummy_file.' period included. If I use

FOR /R %%F in (*.CDP) do (
        for /f "tokens=*" %%a in ("%%~nxF") do (

然后跳过第二个 for 循环,程序继续进行.我认为这会使循环将其视为字符串文字,但显然 for 循环有自己的阅读方式.

Then the second for loop gets skipped, and the program proceeds on. I thought this would made the loop take it as a string literal, but apparently the for loop has its own way of reading things.

是否可以接受此循环中带有 - Copy 的文件?我可以在这里使用 dummy_file - Copy.cdp 吗?

Is it possible to accept files in this loop that have a - Copy in them? Will I be able to use dummy_file - Copy.cdp here?

推荐答案

for /R %%F in (*.CDP) do (
    for /F "usebackq delims=" %%A in ("%%~fF") do (

变化:

  1. 此代码不是检索所有标记,而是禁用分隔符

  1. Instead of retrieving all tokens, this code disables the delimiters

由于文件名被引用,usebackq被include表示我们处理的不是直接字符串,而是文件内容

As the file name is quoted, usebackq is include to indicate that we are not processing a direct string, but the file contents

由于代码递归文件夹,如果想法是读取文件内容,那么不是直接使用文件名和扩展名,而是需要完整路径,因为文件位置可能与当前目录不同

As the code recurses folders, if the idea is to read the file contents then instead of directly using the file name and extension, the full path is needed as the file location can differ from current directory

这篇关于For 循环读取带空格的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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