Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch script for renaming and then moving files from 1 folder to another

I have a file book.csv in folder C:\sam. I want to write .bat script renaming it first and then moving to C:\samy. Also the book.csv will be dynamic file name.

Edit

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; maybe 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 override. But I want to do this automatically.

move /-Y C:\user\Downloads\*.csv C:\user\downloads1\
like image 532
user2034816 Avatar asked Sep 04 '25 17:09

user2034816


1 Answers

@echo off
set /p new_name=set a new name   
 move /Y C:\sam\books.csv C:\samy\%new_name%.csv

or

@echo off 
 move /Y C:\sam\books.csv C:\samy\%%~1.csv

The first will ask the user for the new name.The second relies on command line argument e.g. movescript.bat new_name

EDIT After the second request in the comments bellow it appears that the ren command should be used after all:

  @echo off
  set /p new_name=set a new name   
  move /Y C:\sam\books*.csv C:\samy\
  cd /D C:\samy\
  ren book_????_??_??.csv %new_name%_????_??_??.csv

or

    @echo off 
    move /Y C:\sam\books*.csv C:\samy\
    cd /D C:\samy\
    ren book_????_??_??.csv %%~1_????_??_??.csv
like image 151
npocmaka Avatar answered Sep 07 '25 18:09

npocmaka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!