如何批量增加文件名? [英] How do I increment a filename in batch?

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

问题描述

我正在编写一个批处理文件,该文件可以使用google maps地理位置API跟踪丢失的android手机.我无法理解我的代码出了什么问题.我遵循了在互联网上找到的所有内容,但是如果运行代码,它将返回语法不正确"或类似的信息.如果存在基本名称,则基本上会增加文件名.您能告诉我我的代码有什么问题以及如何纠正它吗?

:MainProcessNew
cd C:\Users\%USERNAME%\Documents
for %%G IN (*.json) do (
set file = %%G
)
set "baseName=data"
set "n=0"

:loop
set /a n+=1
if exist "%baseName%%n%.json (
goto loop
)

echo.>"C:\Users\%USERNAME%\Documents\data%n%.json"

解决方案

使用此批处理代码:

:MainProcessNew
cd /D "%USERPROFILE%\Documents"
for %%G in (*.json) do set "FileName=%%G"

set "BaseName=data"
set "FileNumber=0"

:FileNameLoop
set /A FileNumber+=1
if exist "%BaseName%%FileNumber%.json" goto FileNameLoop

echo/>"%USERPROFILE%\Documents\%BaseName%%FileNumber%.json"

主要错误是 IF 条件行中缺少双引号.

但是还有其他错误和代码行可以改进.

请勿使用C:\Users\%USERNAME%,因为用户的配置文件目录也可以位于驱动器C:以外的其他驱动器上.改用预定义的环境变量USERPROFILE的值.

当前用户的documents文件夹路径可以包含1个或多个空格,例如,当用户名包含空格时.因此,始终将与用户配置文件相关的文件夹路径括在双引号中.

如果不能100%保证批处理过程的当前目录和新的当前目录位于同一驱动器上,则应始终将命令 CD /D一起使用,以也更改当前驱动器. /p>

请不要为环境变量赋一个等号附近的值,请参见为什么不输出带有'echo%var%'的字符串在命令行上使用"set var = text"后?解释此常见错误.

如果只应执行1条命令,则不要为命令 FOR IF 定义带括号的命令块,因为这两个命令最初只用于运行1条命令命令.

建议在CamelCase表示法中使用良好的变量名.搜索n很难找到该环境变量的所有出现,但是搜索FileNumber非常容易.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

  • cd /?
  • echo /?
  • for /?
  • goto /?
  • if /?
  • set /?

I am programming a batch file that can track lost android phones using the google maps geolocation API. I cannot understand what is wrong with my code. I followed everything I found on the internet but if I run the code, it returns "the syntax was incorrect" or something like that.It basically increments a filename if the basename exists. Can you show me what is wrong with my code and how do I correct it?

:MainProcessNew
cd C:\Users\%USERNAME%\Documents
for %%G IN (*.json) do (
set file = %%G
)
set "baseName=data"
set "n=0"

:loop
set /a n+=1
if exist "%baseName%%n%.json (
goto loop
)

echo.>"C:\Users\%USERNAME%\Documents\data%n%.json"

解决方案

Use this batch code:

:MainProcessNew
cd /D "%USERPROFILE%\Documents"
for %%G in (*.json) do set "FileName=%%G"

set "BaseName=data"
set "FileNumber=0"

:FileNameLoop
set /A FileNumber+=1
if exist "%BaseName%%FileNumber%.json" goto FileNameLoop

echo/>"%USERPROFILE%\Documents\%BaseName%%FileNumber%.json"

The main mistake is the missing double quote in IF condition line.

But there are also other mistakes and code lines which could be improved.

Don't use C:\Users\%USERNAME% because the profile directory of a user can be also on another drive than drive C:. Use instead the value of the predefined environment variable USERPROFILE.

The path to documents folder of the current user can contain 1 or more spaces, for example when the user name contains a space. Therefore always enclose user profile related folder paths in double quotes.

The command CD should be used always with /D to change also the current drive if it is not 100% guaranteed that current directory of batch process and new current directory are on same drive.

Never assign a value to an environment variable with spaces around equal sign, see Why is no string output with 'echo %var%' after using 'set var = text' on command line? for an explanation of this common mistake.

Don't define a command block with parentheses for the commands FOR and IF if just 1 command should be executed as those two commands are originally designed for running only 1 command.

It is advisable to use good variable names in CamelCase notation. It is hard to search for n to find all occurrences of that environment variable, but is very easy to search for FileNumber.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • cd /?
  • echo /?
  • for /?
  • goto /?
  • if /?
  • set /?

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

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