使用命令提示符批处理文件移动并自动重命名重复文件 [英] Using command prompt batch files to move and automatically rename duplicate files

查看:460
本文介绍了使用命令提示符批处理文件移动并自动重命名重复文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,这就是我所拥有的:我创建了一个小的批处理文件,以将ping测试的结果记录到文本文件中.我要执行的是运行批处理文件,并将结果日志自动移动到桌面上的特定文件夹.然后,如果目标文件名存在,则相应地自动重命名.意思是,如果File1存在,则创建File2,File3,依此类推.这是我到目前为止的内容:

So, here's what I have: I created a small batch file to record the results of a ping test to a text file. What I want to do is run the batch file and move the results log to a specific folder on the Desktop automatically. Then, if the target filename exists, automatically rename accordingly. Meaning, if File1 exists, create File2, File3, so on and so forth. Here's what I have so far:

@echo off
color A
title Ping Test
:A
echo.
cls
ping google.com -n 4 > C:\Users\%username%\Desktop\pingresults.txt
cls
:Question
echo.
echo You can find the results in C:\Users\%username%\Desktop\pingresults.txt
echo Would you like to run this test again? (Y/N)
Set /p Choice=
if %choice% equ y goto :A
if %choice% equ n goto :Results
if %choice% neq y goto :Question
if %choice% neq n goto :Question
:Results
cls
echo Would you like to view the results of the test? (Y/N)
Set /p Choice=
if %choice% equ y goto :OpenResults
if %choice% equ n goto :Close
if %choice% neq y goto :Results
if %choice% neq n goto :Results
:Close
exit
:OpenResults
start C:\Users\%username%\Desktop\pingresults.txt

移动批处理文件如下所示:

And the move batch file looks like this:

@echo off
echo.
cd C:\Users\Diesel\Desktop
move pingresults.txt PingResults\
if exist pingresults.txt ren pingresults.txt=+1
Exit.

我不想覆盖现有文件,但要连续重命名.我在仅使用批处理文件的任何地方都找不到任何有用的文章,它们都说要使用vbs或php,这是我不熟悉的语言

I don't want to overwrite the existing file, but rename it in succession. I can't find any helpful articles anywhere using only batch files, they all say to use vbs or php, a language I'm not familiar with

推荐答案

要将文件重命名为file1 [file2][...]并将其移动到桌面文件夹中:

To rename files to file1 [file2][...] and move it in a desktop folder:

@ECHO Off &SETLOCAL
FOR %%a IN (*.txt) DO CALL:processFile "%%~a"
goto:eof

:processFile
SETLOCAL
:loop
SET /a fileCounter+=1
SET "fileName=%~n1%filecounter%%~x1"
IF EXIST "C:\Users\%username%\Desktop\%fileName%" GOTO:loop
ECHO MOVE "%~1" "C:\Users\%username%\Desktop\%fileName%"
ENDLOCAL
goto:eof

查看输出,并在move之前删除单词echo(如果看起来不错).

Look at the output and remove the word echo before move if it looks good.

这篇关于使用命令提示符批处理文件移动并自动重命名重复文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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