0

I am using the following script through windows cmd in automatically copying multiple files located/sourced from multiple subdirectories to a single directory:

cd /d "C:\example files\various\raw files"

for /r %d in (*) do copy "%d" "C:\example files\conso"

I put \y at the end of the script if I want to ovewrite duplicate files.

However, my dilemma is I want to not overwrite those files, instead automatically renaming them.

Is there any other command in the windows cmd that would try to copy those files and try to rename them if there are duplicates found?

2
  • 2
    You may adapt this to your needs
    – SΛLVΘ
    Nov 3, 2015 at 8:48
  • Hi @SalvoF, None of those scripts worked for me. It is a kind of complicated. By the way, I'm only a beginner in terms of cmd and scripting. Is there any other way aside from those suggestions. Thank you very much. Nov 23, 2015 at 10:02

1 Answer 1

0

Here is a batch file. I'm using cp command's backup feature. Program can be found under the \usr\local\wbin folder of this zipped file (UnxUtils project). Extract the executable in any folder in your %PATH%.

@echo off
setlocal EnableDelayedExpansion

rem Set VERSION_CONTROL variable, read by `cp' *nix command
set VERSION_CONTROL=t

rem Define origin and destination folders
set "_orig=C:\example files\various\raw files"
set "_dest=C:\example files\conso"

for /r "%_orig%" %%F in (*.*) do cp -b "%%F" "%_dest%"
for %%N in ("%_dest%\*.*") do (
    set _ext=%%~xN
    if "!_ext:~-1!" == "~" (
       set "_name=%%~nN"
       ren "%%N" "!_name:~0,-4!(!_ext:~-2,1!)!_name:~-4,4!"
    )
)
4
  • 1) I don't make any check on what's already in destination folder 2) Having more than 10 identical file names is not managed (that's unlikely I hope) 3) File names containing ! cause trouble
    – SΛLVΘ
    Nov 23, 2015 at 13:18
  • Hi @SalvoF, thanks for taking the time. I'll find a way to make your suggestion work. I have a hard time understanding programming languages :) I'm a financial auditor and I don't have much knowledge in IT programming. Nov 24, 2015 at 7:38
  • NP. I'm no pro either... but if you face difficulties try to ask. On a note, when I wrote "10 identical file names" I meant per file of course, there's no limit on processable files
    – SΛLVΘ
    Nov 24, 2015 at 7:47
  • Okay. Ill try to make it work. Nov 24, 2015 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.