Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 使用命令提示“批处理文件”移动并自动重命名重复文件_Windows_Batch File_Cmd_Rename_Move - Fatal编程技术网

Windows 使用命令提示“批处理文件”移动并自动重命名重复文件

Windows 使用命令提示“批处理文件”移动并自动重命名重复文件,windows,batch-file,cmd,rename,move,Windows,Batch File,Cmd,Rename,Move,我创建了一个小的批处理文件,将ping测试的结果记录到一个文本文件中。我要做的是运行批处理文件并将结果日志自动移动到桌面上的特定文件夹。然后,如果目标文件名存在,则自动相应地重命名。也就是说,如果存在File1,则创建File2、File3等等。以下是我目前掌握的情况: @echo off color A title Ping Test :A echo. cls ping google.com -n 4 > C:\Users\%username%\Desktop\pingresults.t

我创建了一个小的批处理文件,将ping测试的结果记录到一个文本文件中。我要做的是运行批处理文件并将结果日志自动移动到桌面上的特定文件夹。然后,如果目标文件名存在,则自动相应地重命名。也就是说,如果存在File1,则创建File2、File3等等。以下是我目前掌握的情况:

@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
移动批处理文件如下所示:

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

我不想覆盖现有文件,但要连续重命名它。我在任何地方都找不到任何只使用批处理文件的有用文章,他们都说使用vbs或php,这是一种我不熟悉的语言,我有一对实用程序文件可以这样做,因此我可以从命令行或其他批处理文件调用。这将管理同一文件夹中文件的多个版本,在移入新文件之前,我会调用它

用途是:

archivefile 5 "c:\temp\songs" .txt [move]
其中5是要保留的版本数。 本例中的基本文件名为“
c:\temp\songs.txt
” 它会创建(在
c:\temp
中)5个文件:
songs\u 1.txt songs\u 2.txt songs\u 3.txt songs\u 4.txt songs\u 5.txt

如果指定
move
,则会移动原始
songs.txt
,否则会复制它

注意,如果您是从另一个批处理文件(您可能想要)使用它,则需要使用
调用
,否则它将不会返回到原始批处理文件(两个批处理文件都将退出,这不是您想要的):

您需要这对批处理文件位于当前目录或windows路径中。现在我想,archivefile_b.bat可能是主文件中的一个子例程,但它没有损坏,所以我不打算修复它

这是archivefile.bat:

@Echo off 
rem archivefile.bat - keep a number of archives of a file
rem paramters:  n rootfile extension [move|copy]
rem n = number of files to keep.
rem move|copy optional indicator to rename (move) the original file instead of copying it
rem archivefile 5 "c:\temp\songs" .txt [move]
rem 




if "%3"=="" goto usage

set _af_n=%1
set _af_root=%2
set _af_ext=%3

set _af_move=copy
set _af_moveX=%4
if "%_af_moveX%"=="move" set _af_move=move

set _af_i=
set _af_j=

rem make sure that the first parameter (n) is numeric
set /A _af_n=%_af_n%
if "%_af_n%"=="0" echo Error: number of copies must be numeric (%1) & goto usage

if not exist %_af_root%%_af_ext% echo Error: cannot locate main file: %_af_root%%_af_ext% & goto error


rem remove the oldest file
if exist %_af_root%_%_af_n%%_af_ext% del %_af_root%_%_af_n%%_af_ext%

rem move up the remainder
for /L %%i in (%_af_n%, -1, 1) do call archivefile_b %%i %_af_root% %_af_ext% 

rem copy the original

if "%_af_move%"=="move" goto moveIt
rem move not specified - make a copy
copy %_af_root%%_af_ext% %_af_root%_1%_af_ext%

goto moveCopyDone

:moveIt

rem need filename only for a rename
set _af_destfile=%_af_root%_1%_af_ext%
set _af_destfile=%_af_destfile:"=%
for %%i in (%_af_destfile%) do set _af_destfile=%%~ni%%~xi

ren %_af_root%%_af_ext% %_af_destfile%



:moveCopyDone



dir %_af_root%*%_af_ext%


goto done

:usage
echo usage: archivefile n rootfile extension
echo   - n = number of archives to keep
echo   - rootfile = the root filename
echo   - extension = file extension
echo.
echo example:
echo archivefile 5 "c:\Documents and Settings\gregh\My Documents\Rescued document" .txt 
goto done

:error
rem could maybe do something here? naa.
goto done

:done
这是archivefile_b.bat,它只管理其中一个文件:

rem @Echo off 
rem archivefile_b.bat - helper for the archivefile routine
rem  -- juggles ONE of the archive files.

rem paramters:  n rootfile extension
rem n = the index to move from n to n+1
rem archivefile 5 "c:\temp\songs" .txt
rem therefore renames c:\temp\songs_5.txt to c:\temp\songs_6.txt


set _afb_n=%1
set _afb_root=%2
set _afb_ext=%3


rem make sure that the first parameter (n) is numeric
set /A _afb_n=%_afb_n%
if "%_afb_n%"=="0" echo Error: (archivefile_b) number of copies must be numeric (%1) & goto done

set /A _afb_j=%_afb_n%+1

rem define the file names
set _afb_fn=%_afb_root%_%_afb_n%%_afb_ext%
set _afb_fj=%_afb_root%_%_afb_j%%_afb_ext%

rem remove the quotes
set _afb_fn=%_afb_fn:"=%
set _afb_fj=%_afb_fj:"=%

rem can only supply a filename to the ren command (not path)
for %%i in (%_afb_fj%) do set _afb_fj=%%~ni%%~xi

rem do the rename now
if exist "%_afb_fn%" ren "%_afb_fn%" "%_afb_fj%" 


:done

我有一对实用程序文件可以执行此操作,因此我可以从命令行或其他批处理文件调用。这将管理同一文件夹中文件的多个版本,在移入新文件之前,我会调用它

用途是:

archivefile 5 "c:\temp\songs" .txt [move]
其中5是要保留的版本数。 本例中的基本文件名为“
c:\temp\songs.txt
” 它会创建(在
c:\temp
中)5个文件:
songs\u 1.txt songs\u 2.txt songs\u 3.txt songs\u 4.txt songs\u 5.txt

如果指定
move
,则会移动原始
songs.txt
,否则会复制它

注意,如果您是从另一个批处理文件(您可能想要)使用它,则需要使用
调用
,否则它将不会返回到原始批处理文件(两个批处理文件都将退出,这不是您想要的):

您需要这对批处理文件位于当前目录或windows路径中。现在我想,archivefile_b.bat可能是主文件中的一个子例程,但它没有损坏,所以我不打算修复它

这是archivefile.bat:

@Echo off 
rem archivefile.bat - keep a number of archives of a file
rem paramters:  n rootfile extension [move|copy]
rem n = number of files to keep.
rem move|copy optional indicator to rename (move) the original file instead of copying it
rem archivefile 5 "c:\temp\songs" .txt [move]
rem 




if "%3"=="" goto usage

set _af_n=%1
set _af_root=%2
set _af_ext=%3

set _af_move=copy
set _af_moveX=%4
if "%_af_moveX%"=="move" set _af_move=move

set _af_i=
set _af_j=

rem make sure that the first parameter (n) is numeric
set /A _af_n=%_af_n%
if "%_af_n%"=="0" echo Error: number of copies must be numeric (%1) & goto usage

if not exist %_af_root%%_af_ext% echo Error: cannot locate main file: %_af_root%%_af_ext% & goto error


rem remove the oldest file
if exist %_af_root%_%_af_n%%_af_ext% del %_af_root%_%_af_n%%_af_ext%

rem move up the remainder
for /L %%i in (%_af_n%, -1, 1) do call archivefile_b %%i %_af_root% %_af_ext% 

rem copy the original

if "%_af_move%"=="move" goto moveIt
rem move not specified - make a copy
copy %_af_root%%_af_ext% %_af_root%_1%_af_ext%

goto moveCopyDone

:moveIt

rem need filename only for a rename
set _af_destfile=%_af_root%_1%_af_ext%
set _af_destfile=%_af_destfile:"=%
for %%i in (%_af_destfile%) do set _af_destfile=%%~ni%%~xi

ren %_af_root%%_af_ext% %_af_destfile%



:moveCopyDone



dir %_af_root%*%_af_ext%


goto done

:usage
echo usage: archivefile n rootfile extension
echo   - n = number of archives to keep
echo   - rootfile = the root filename
echo   - extension = file extension
echo.
echo example:
echo archivefile 5 "c:\Documents and Settings\gregh\My Documents\Rescued document" .txt 
goto done

:error
rem could maybe do something here? naa.
goto done

:done
这是archivefile_b.bat,它只管理其中一个文件:

rem @Echo off 
rem archivefile_b.bat - helper for the archivefile routine
rem  -- juggles ONE of the archive files.

rem paramters:  n rootfile extension
rem n = the index to move from n to n+1
rem archivefile 5 "c:\temp\songs" .txt
rem therefore renames c:\temp\songs_5.txt to c:\temp\songs_6.txt


set _afb_n=%1
set _afb_root=%2
set _afb_ext=%3


rem make sure that the first parameter (n) is numeric
set /A _afb_n=%_afb_n%
if "%_afb_n%"=="0" echo Error: (archivefile_b) number of copies must be numeric (%1) & goto done

set /A _afb_j=%_afb_n%+1

rem define the file names
set _afb_fn=%_afb_root%_%_afb_n%%_afb_ext%
set _afb_fj=%_afb_root%_%_afb_j%%_afb_ext%

rem remove the quotes
set _afb_fn=%_afb_fn:"=%
set _afb_fj=%_afb_fj:"=%

rem can only supply a filename to the ren command (not path)
for %%i in (%_afb_fj%) do set _afb_fj=%%~ni%%~xi

rem do the rename now
if exist "%_afb_fn%" ren "%_afb_fn%" "%_afb_fj%" 


:done

要将文件重命名为
file1[file2][…]
并将其移动到桌面文件夹中,请执行以下操作:

@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

查看输出并删除单词
echo
,如果它看起来不错,请在
move
之前将其删除。

要将文件重命名为
file1[file2][…]
并将其移动到桌面文件夹中,请执行以下操作:

@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
,如果它看起来不错的话。

对我来说是有意义的,它应该按照我需要的方式工作,但我的问题是:假设我需要移动的文件名为“Test”。在这段代码中,我应该在哪里用“文件名”替换“测试”?这对我来说很有意义,它应该按照我需要的方式工作,但我的问题是:假设我需要移动的文件名叫做“测试”。在这段代码中,我应该在哪里用“文件名”代替“测试”?