-2

I need a code to rename one file after another with each run. I made a working code but it can be used only for limited amount of files. I would like it to be good for many many files (e.g. instead of copying if then command over and over again) Here is what I've got.

@ECHO OFF

set /p var1="<"ver.log (Had to use "" because otherwise it is hidden here)

set /a var1=%var1%+1

Echo "%var1%"> "%~dp0\ver.log"

If %var1% EQU 1 (
ren "%~dp0\chrom0.txt" 3.txt
ren "%~dp0\1.txt" chrom0.txt
)

If %var1% EQU 2 (
ren "%~dp0\chrom0.txt" 1.txt
ren "%~dp0\2.txt" chrom0.txt
)

If %var1% EQU 3 (
ren "%~dp0\chrom0.txt" 2.txt
ren "%~dp0\3.txt" chrom0.txt
@Echo "0"> "%~dp0\ver.log"
)

This works for 3 files. I can make it work for more but it is nonsense to use it for 50 or more files.

Please, help.

Invictus
  • 13
  • 3
  • 1
    You're welcome to write such a code, but I doubt that you'll be able to write a batch-file, being able to handle mouse clicks :-) – Dominique Sep 18 '18 at 14:21
  • By mouse clicks I mean running batch. Sorry for confusion. – Invictus Sep 18 '18 at 15:15
  • `ren "%~dp0%var1%.txt" chrom0.txt &echo %var1% in use &echo click enter after you save your work & pause & ren "%~dp0chrom0.txt" %var1%.txt` - from other hand, instead of pause run your app – penknife Sep 19 '18 at 16:55
  • penknife, thanks for idea. I got this: set /p var1= "%~dp0log.log" ren "%~dp0%var2%.txt chrom0.txt pause Got errors. 1) ren "D:\User_Folders\Desktop\Done Script - v.2\chrom0.txt" "2".txt The system cannot find the file specified. 2) ren "D:\User_Folders\Desktop\Done Script - v.2\3.txt chrom0.txt The syntax of the command is incorrect. Please, how do I fix this issue? – Invictus Sep 20 '18 at 13:30
  • Also, to reset counter to 1 I will need something like If %var1% EQU 1 ( ren "%~dp0\chrom0.txt" 1.txt Echo "1"> "%~dp0log.log" Please, is this correct? – Invictus Sep 20 '18 at 13:30

2 Answers2

0

My solution:

    @echo off
    setlocal
    pushd "%~dp0"
    rem to get "<" on SO type "&lt;"
    set /a "var1=0,var2=0"
    if exist "%~f0.last" set /p "var1="<"%~f0.last"
    :scan
    set /a "var1+=1"
    if %var1% gtr 500 set /a "var1=0,var2+=1"
    if %var2% gtr 2 ( echo Error: not found any file
      pause
      endlocal
      exit /b 2
      goto :eof )
    if not exist "%var1%.txt" if not exist chrom0.txt goto scan
    if exist chrom0.txt if exist "%var1%.txt" ( echo Error: app in use?
      pause
      endlocal
      exit /b 1
      goto :eof
    ) else echo Warning: "%var1%" as chrom0.txt in use
    if not exist chrom0.txt ren "%var1%.txt" chrom0.txt
    echo "%var1%" in use
    echo wait for exit app
    rem call "c:\Your\Limited\App.exe" chrom0.txt
    pause
    ren chrom0.txt "%var1%.txt"
    set /p "=%var1%">"%~f0.last" <nul
    rem work in loop
    if exist "%~f0.loop" goto scan
    popd
    endlocal
    exit /b 0
penknife
  • 101
  • 4
0

Thank you everyone. I used your tips and ended up with this script. :start

set /p var1=

if %var1% gtr 4 set /a "var1=1"

ren "%~dp0chrom0.txt" %var1%.txt

set /a var2=%var1%+1

if %var2% gtr 5 set /a "var2=1"

Echo "%var2%"> "%~dp0log.log"

ren "%~dp0%var2%.txt" chrom0.txt

Invictus
  • 13
  • 3