move (or copy) selection, rename incrementally when they exist in target folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ResonantStep
Posts: 4
Joined: 27 Apr 2019 06:32

move (or copy) selection, rename incrementally when they exist in target folder

#1 Post by ResonantStep » 30 Apr 2019 04:02

Following my moveto/copyto script viewtopic.php?f=3&t=9112
I try to implement "incremental rename" mecanism.
Renaming:
New text.txt to New text (2).txt, (3), (4) etc., still not sure if I should rename "New text (2).txt" to "New text (3)" or "New text (2) (2).txt" (2nd seemed more "logical")
I also started implementing a box (WBOX) for the user to choose either to overwrite or make new file (or choose for each item/file/folder if multiple selection).

But I have hard times understanding delayed expansion, and while it yet works for single file or folders it doesn't for multiple selection and it took me lots of tries.
For this example, the boxes are not important (already asked question about in the other thread) only the incremental mecanism and for / loop.
I can put target folder in a variable to save time while testing (and just choice instead the message box)
the script itself isn't so useful as it is but it will unlock many possibilities for me and other ideas.

Ideally I'd like to make the same with copy to.
So here's where I am at:

Code: Select all

@if (@CodeSection == @Batch) @then


@echo off

setlocal
set "cnt=0"
set "SelectionName='%~nx1'"
set "NewName=New"
set "incr=1"
set "WBOX=C:\Program Files\System Tools\System Utilities\wbox64\Wbox.exe"
cd /d "%~dp1"

	for %%A in (%*) do set /a cnt+=1
	if %cnt% GTR 1 set "SelectionName=these %cnt% items"
	for /F "delims=" %%a in ('CScript //nologo //E:JScript "%~F0" "Select the place where you want to move %SelectionName%, then click OK."') do set "TargetFolder=%%a"
	
	if "%TargetFolder%" == "" goto :eof
	if "%TargetFolder%" == "%~dp1" goto :eof
			
	setlocal enableDelayedExpansion
	for %%a in (%*) do (
		set "SRCName=%%~na"
		set "SRCNameX=%%~nxa"
		set "SRCPATH=%%~dpnxa"
		set "Extn=%%~xa"
		set "Target=%TargetFolder%\%%~nxa"
		if not exist !Target! (
			move /y "!SRCPATH!"  "%TargetFolder%"
		) else (
			if not "!SelectionName!" == "these !cnt! items" (
:WBOX1
				"%WBOX%" "Warning" "!SRCNameX! already exists at target location, do you want to overwrite it or make a new file ?" " Overwrite ; Make New File ; Cancel "
				goto Action_!errorlevel!
			) else (
				"%WBOX%" "Warning" "Some items already exist at target location, do you want to overwrite them or make new files ?" " Overwrite All ; Make New Files ; Choice by Item ; Cancel "
				if !errorlevel! LEQ 2 ( goto :Action_!errorlevel!)
::TO DO SINGLE CHOICE 3 FOR DUPLICATES 
				if "!errorlevel!" == "3" ( goto :WBOX1) else ( goto :Actionb_!errorlevel!)
			
:Action_1
	move /y "!SRCPATH!"  "%TargetFolder%"
	)
	goto :eof
:Action_2
	goto :increment

:Action_3
	goto :eof
	)
:Action_0
:Action_255
	goto :eof
	)
:Actionb_4
	goto :eof
	)
:Actionb_0
:Actionb_255
	goto :eof
	)
:increment
	set "baseName=!SRCName! ("
	cd /d %TargetFolder%
	for /f "delims=" %%F in ('
	  2^>nul dir /b /a-d "!baseName!%incr%)!Extn!"^
	   ^|findstr /xri /c:"!baseName![0-9]*).*"
	') do ( 
		set "name=%%~nF"
		set "name=!name:*!baseName!=!"
		set "name=!name:)=!"
		if !name! gtr !incr! set "incr=!name!"
	)
	set /a "incr+=1"
	set  "newName=!baseName!!incr!)!Extn!"
	set "NewTarget=%TargetFolder%\!newName!"

	if not exist !NewTarget! (
		move /y "!SRCPATH!"  "!NewTarget!"
	) else (
		set "baseName=!SRCName! ("
		set "baseName=!baseName:~0,-2!"
		for %%a in (%*) do (
		for /f "delims=" %%F in ('
			2^>nul dir /b /a-d "!baseName!!incr!)!Extn!"^
			 ^|findstr /xri /c:"!baseName![0-9]*).*"
			') do (
				set "name=%%~nF"
				set "name=!name:*!baseName!=!"
				set "name=!name:)=!"
				if !name! gtr !incr! set "incr=!name!"
			)
			set /a "incr+=1"
			set  "newName=!baseName! (!incr!)!Extn!"
			set "Target=%TargetFolder%\!newName!"
			move /y %%a  "!Target!"
		)
	)
)

goto :eof


@end


var shl = new ActiveXObject("Shell.Application");
var folder = shl.BrowseForFolder(0, WScript.Arguments(0), 0x00000050,17);
WScript.Stdout.WriteLine(folder ? folder.self.path : "");

Post Reply