user2034816
user2034816

Reputation: 97

Batch script move files without overwriting

I want a script so my file move from one folder to another and creates a new file, if any file already exists.

For example, I have a file test.csv in Downloads folder. When I run the below script if overrides the file if any file exists with the same name in downloads1 folder.

But I want, it shouldn't override the existing file but both the files should be there..may be it changes the name.adds 1,2 behind.

move C:\user\Downloads\*.csv C:\user\downloads1\

Also I know using /-Y will ask me I needs to overide. But I want to do this automatically.

move /-Y C:\user\Downloads\*.csv C:\user\downloads1\

Upvotes: 2

Views: 11783

Answers (3)

YenForYang
YenForYang

Reputation: 3284

Not sure if this only applies to certain versions of Windows, but on Windows 10 Pro 21H1,

C:\>move /?
Moves files and renames files and directories.

...

  /-Y                     Causes prompting to confirm you want to overwrite
                          an existing destination file.

The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.  Default is
to prompt on overwrites unless MOVE command is being executed from
within a batch script.

Note the emphasis:

Default is to prompt on overwrites unless MOVE command is being executed from within a batch script.

So if you are executing move from a batch script, you shouldn't come across your prompting issue (i.e. echo No| is not required).

Upvotes: 0

npocmaka
npocmaka

Reputation: 57252

The easiest way:

echo No|move /-Y  .\file1 .\file2

You can use also wildcards:

echo NO|move /-Y  "C:\user\Downloads\*.csv" "C:\user\downloads1\"

Also in your case:

for %%f in ("C:\user\Downloads\*.csv") do (
  echo No|move /-Y  "%%~dpfnxf" "C:\user\downloads1\"
)

EDIT:

for %%f in ("C:\user\Downloads\*.csv") do (
  if exist "C:\user\downloads1\%%~nxf" (
      rem move "C:\user\downloads1\%%~nxf" "C:\user\downloads1\%%~nxf.bkp"
      move "C:\user\downloads1\%%~nxf" "C:\user\downloads1\%%~nf-%%N.%%~xf"
  )
  move /Y  "%%~dpfnxf" "C:\user\downloads1\"
)

Check also this : Copy files without overwrite

One more edit:

setlocal enableDelayedExpansion
for %%f in ("C:\user\Downloads\*.csv") do (
  set "moved="
  if exist "C:\user\downloads1\%%~nxf" (
    for /l %%N in (1,1,50) do (
     if not defined moved if not exist "C:\user\downloads1\%%~nxf.%%N" (
      move "C:\user\downloads1\%%~nxf" "C:\user\downloads1\%%~nxf.%%N"
      set moved=yes
     )
    )

  )
  move /Y  "%%~dpfnxf" "C:\user\downloads1\"
)
endlocal

not tested.

Upvotes: 5

MC ND
MC ND

Reputation: 70923

You can move first non existing files, then rename the remainig files in source directory and then copy to target.

@echo off
    setlocal enableextensions enabledelayedexpansion

    rem configure directories
    set "source=c:\user\Downloads"
    set "target=c:\user\Downloads1"

    rem move non existing files to target
    call :doMove    

    rem if we still have files
    if exist "%source%\*.csv" (

        rem generate a timestamp
        set timestamp=_%date:/=%_%time::=%
        set timestamp=!timestamp:,=!

        rem rename the remaining files with timestamp
        ren "%source%\*.csv" "*.!timestamp!.csv"

        rem and move the remainig files to target
        call :doMove
    )

    endlocal
    exit /b

:doMove
    robocopy "%source%" "%target%" "*.csv" /fp /njh /njs /ndl /xc /xn /xo /xx /mov
    goto :EOF

Upvotes: 1

Related Questions