0

I use the commands below to rename files and move them to a directory:

ren credentials* passwords%random%.txt 
move E:/passwords*.txt E:/Passwords/

The file rename puts a random number like 1231 or something like this into new file name.
So I can identify it respectively no existing file is overwritten. But that is not the subject.

What I want to know is: How could I make a file numeration?

I would like to have the credentials* files in current directory moved to the passwords directory with passwordsX.txt as new file name whereby X should be the next free number according to existing passwords*.txt files.

So if there are already passwords1.txt and passwords2.txt in the passwords directory, the file credentialsX should be moved to the passwords directory with new name passwords3.txt and credentialsY as passwords4.txt.

Mofi
  • 38,783
  • 14
  • 62
  • 115
  • Perform a search, _unless you believe that nobody has ever asked about copying or moving files without overwrite and with name incrementation as happens in the GUI_! – Compo Dec 24 '17 at 11:52
  • One such [example](https://superuser.com/a/350633). – Compo Dec 24 '17 at 12:21

1 Answers1

0

Here is a comment batch file for this task asked and answered already often on Stack Overflow.

@echo off
if not exist "credentials*" goto :EOF

setlocal EnableExtensions DisableDelayedExpansion
set "NextNumber=0"

for %%I in (E:\Passwords\passwords*.txt) do call :GetHighestNumber "%%~nI"
for %%I in (credentials*) do call :MoveFile "%%I"

endlocal
goto :EOF


rem GetHighestNumber is a subroutine which must be called with a passwords*
rem file name without file extension. It determines the number of the current
rem passwords file and compares it with currently highest file number to get
rem finally the currently highest number in all existing passwords*.txt files.

:GetHighestNumber
set "FileName=%~1"

rem Get from file name without file extension the characters after passwords.
set "FileNumber=%FileName:~9%"

rem Ignore the file passwords.txt if existing by chance.
if not defined FileNumber goto :EOF

rem Delete the environment variable FileNumber if it contains any other
rem character than digits to ignore files like passwords12_bak.txt.
for /F "delims=0123456789" %%N in ("%FileNumber%") do set "FileNumber="

rem Has the passwords*.txt file a different string matched by wildcard *
rem than a decimal number, ignore this file and exit this subroutine.
if not defined FileNumber goto :EOF

rem Remove leading zeros to avoid getting the decimal number
rem interpreted as octal number on integer comparison below.

:RemoveLeading0
if not "%FileNumber:~0,1%" == "0" goto CompareNumbers
set "FileNumber=%FileNumber:~1%"
if defined FileNumber goto RemoveLeading0
set "FileNumber=0"

rem The number could be greater than 2147483647 in which case
rem the integer comparison would be not correct because Windows
rem command interpreter does not support larger values.

:CompareNumbers
if %FileNumber% GTR %NextNumber% set "NextNumber=%FileNumber%"
goto :EOF


rem Subroutine MoveFile increases the next file number by 1 and
rem moves the credentials* file to E:\Passwords\passwords*.txt
rem with the incremented file number in file name.

:MoveFile
set /A NextNumber+=1
move %1 "E:\Passwords\passwords%NextNumber%.txt"
goto :EOF

Please note that on Windows the directory separator is \ and not / as on Unix/Linux/Mac. / is used on Windows usually as begin of a parameter as it can be seen on the batch code above while on Unix/Linux/Mac - is used usually as begin of a parameter.

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.

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • move /?
  • rem /?
  • set /?
  • setlocal /?

Read also Where does GOTO :EOF return to? and this answer for details about the commands SETLOCAL and ENDLOCAL.

Mofi
  • 38,783
  • 14
  • 62
  • 115