批量更改文件扩展名 [英] change file extension in batch

查看:146
本文介绍了批量更改文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题. IMDU命令做

imdu /b file.imd file.raw

并将file.imd转换为file.raw

我有很多.imd 所以我需要一批 我已经尝试过

for %%x in (*.imd) do imdu /b %%x %%~nx.raw

但是无法创建名为%%.raw的文件

我需要批量删除扩展名imd并用raw替换 怎么办?

解决方案

打开命令提示符窗口,运行for /?并仔细,完整地阅读输出帮助.

解释了%~xI –仅>的文件扩展名 –和%~nI –仅%I%~nxI文件名扩展名%I的文件名.请注意,分配给区分大小写的循环变量I的字符串是真正存在还是根本不存在的文件或文件夹都没有关系.实际上,它可以是任何字符串.

%~nI引用的文件名是最后一个反斜杠后到字符串的最后一个点或末尾的字符串.如果分配给循环变量的字符串以反斜杠结尾(即,是文件夹路径),或者文件名以点开头并且没有真实的文件扩展名,例如UNIX/MAC上的隐藏文件,通常会被命名.那么它可以是空字符串./p>

%~xI引用的文件扩展名是从上次反冲后的最后一个点到分配给循环变量的字符串末尾的所有内容.因此,%~xI引用文件的文件名,其名称类似于.htaccess而不是%~nI,在这种特殊情况下为空字符串.在UNIX/MAC上,名为.htaccess的文件是真实名称为htaccess的文件,而在文件名开头的.则使该文件在UNIX/MAC文件系统上隐藏.

请注意,循环变量区分大小写,而nxf,...等修饰符不区分大小写.因此%~NXI%~nxI相同.通常,使用大写的循环变量和小写的修饰符更容易理解.

这可能会使读者感到困惑,并且在某些特殊情况下,对于cmd.exe来说,使用同样也是修饰符的字符作为循环变量意味着什么,例如,在cmd窗口中运行命令行时,该含义是什么:

for %f in ("1. " "2. " "3. ") do @echo %~ffile

I不是修饰符,因此可以使用%I而不是%f避免上面命令行的错误输出,因为在Windows命令提示符窗口中运行时可以看到它:

for %I in ("1. " "2. " "3. ") do @echo %~Ifile

现在很明显,%~ff被解释为引用分配给循环变量f的字符串的完整限定文件名,而%~I被解释为引用分配给循环变量I的字符串,并且删除了双引号

因此,我建议避免将其中一个字符ADFNPSTXZadfnpstxz用作循环变量,或者至少在使用它们时要格外小心.

出于所有这些原因,最好在批处理文件中使用,在该文件中,必须在以下命令行中将百分号加倍:

for %%I in (*.imd) do imdu.exe /b "%%I" "%%~nI.raw"

对于这样简单的循环,也可以使用 ASCII 字符而不是字母或对于Windows命令处理器没有特殊含义的数字,例如:

for %%# in (*.imd) do imdu.exe /b "%%#" "%%~n#.raw"

在批处理文件中搜索所有出现的#都比在其他字符串中也多次出现的字母出现的所有出现要容易.字符$作为循环变量也非常好,因为它也没有特殊含义,并且通常在批处理文件的其他字符串中不存在.

请不要忘记在文件名两边加上双引号,因为文件的文件名中可能包含空格,圆括号或&"号,而文件名需要用双引号引起来.在这种用例中,命令 FOR 保存一个文件名,该文件名始终在循环变量中不包含双引号.

另一个提示:

使用 FOR 处理与通配符模式匹配的文件或文件夹,这些文件或文件夹在 FOR 迭代中被执行的命令行重命名,移动或删除,这在以下方面是有问题的FAT32和exFAT驱动器由于目录条目的列表更改而 FOR 在循环迭代期间访问此列表.

例如,批处理文件中下面的命令行(当前目录位于 FAT32 exFAT 驱动器上)可能导致临时文件被重命名多次./p>

for %%# in (*.tmp) do ren "%%#" "%%~n#_1.tmp"

在这种情况下,最好在批处理文件命令 DIR 中使用以获取 FOR 捕获的文件名列表,该列表现在处理的不是文件名列表.通过 FOR 对每个文件名执行的命令行进行修改,如下所示.

for /F "eol=| delims=" %%# in ('dir *.tmp /A-D /B /ON 2^>nul') do ren "%%#" "%%~n#_1.tmp"

Simple question. IMDU command do

imdu /b file.imd file.raw

and convert the file.imd on file.raw

I have a lot of .imd so i need a batch i have tried

for %%x in (*.imd) do imdu /b %%x %%~nx.raw

But doesn't work and create a file called %%.raw

I need a batch wich remove extension imd and replace with raw How to do?

解决方案

Open a command prompt window, run for /? and read the output help carefully and completely.

There is explained %~xI – only file extension of %I – and %~nI – only file name of %I – and %~nxIfile name with extension of %I. Please note that it does not matter if the string assigned to case-sensitive loop variable I is really a file or a folder which really exists or does not exist at all. In fact it can be any string.

The file name referenced with %~nI is the string after last backslash up to last dot or end of string. It can be an empty string if the string assigned to loop variable ends with a backslash, i.e. is a folder path, or name of file starts with a dot and has no real file extension like hidden files on UNIX/MAC are often named.

The file extension referenced with %~xI is everything from last dot after last backlash to end of string assigned to loop variable. So %~xI references the file name of a file with a name like .htaccess and not %~nI which is in this special case an empty string. A file named .htaccess is on UNIX/MAC a file with real name being htaccess and . at beginning of file name makes this file hidden on UNIX/MAC file systems.

Please note that loop variables are case-sensitive while the modifiers like n, x, f, ... are not case-sensitive. So %~NXI is identical to %~nxI. It is in general better readable to use a loop variable in upper case and the modifiers in lower case.

It can be confusing for readers and in some special cases also for cmd.exe what is meant on using as loop variable a character which is also a modifier, for example on running in a cmd window the command line:

for %f in ("1. " "2. " "3. ") do @echo %~ffile

I is not a modifier and so the wrong output by the command line above can be avoided using %I instead of %f as it can be seen on running in a Windows command prompt window:

for %I in ("1. " "2. " "3. ") do @echo %~Ifile

Now it is clear that %~ff was interpreted as reference to full qualified file name of string assigned to loop variable f while %~I is interpreted as referencing the string assigned to loop variable I with double quotes removed.

So I recommend to avoid one of these characters ADFNPSTXZadfnpstxz as loop variable or be at least careful on using them.

For all that reasons it is better to use in batch file on which the percent sign must be doubled the following command line:

for %%I in (*.imd) do imdu.exe /b "%%I" "%%~nI.raw"

For such simple loops it is also possible and good practice to use an ASCII character not being a letter or digit having no special meaning for Windows command processor like:

for %%# in (*.imd) do imdu.exe /b "%%#" "%%~n#.raw"

It is easier to search in batch file for all occurrences of # than for all occurrences of a letter existing also many times in other strings. The character $ is also very good as loop variable because it has also no special meaning and does usually not exist in other strings in a batch file.

Don't forget the double quotes around the file names as files could contain spaces or round brackets or ampersands in their file names which require double quotes around file names. Command FOR holds in this use case a file name always without surrounding double quotes in loop variable.

One more hint:

The usage of FOR to process files or folders matching a wildcard pattern which are renamed, moved or deleted by executed command line(s) on FOR iterations is problematic on FAT32 and exFAT drives because of list of directory entry changes while FOR accesses this list during loop iterations.

For example the command line below in a batch file with current directory being on a FAT32 or exFAT drive can result in a temporary file being renamed more than once.

for %%# in (*.tmp) do ren "%%#" "%%~n#_1.tmp"

In such cases it is better to use in the batch file command DIR to get a list of file names captured by FOR which processes now a list of file names not being modified by the command line(s) executed by FOR on each file name as shown below.

for /F "eol=| delims=" %%# in ('dir *.tmp /A-D /B /ON 2^>nul') do ren "%%#" "%%~n#_1.tmp"

这篇关于批量更改文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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