Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 2
Message 1 of 2

Batch Scripting moving and renaming files

Hey Github community, 

 

I am very new to programming/coding and this is my very first question. I'll try my best to explain my problem (sry for my english).

I want to create a batch script which selects the oldest file in a folder, renames it, moves it to another folder and renames it again. 

 

Another way to explain it: 

- I have a folder with 15 files in it 

- The oldest file is called "orange" 

- I want the script to rename the "orange" file

- Now moving the file to another folder and finally rename it again

 

I got this so far from somewhere else but I dont know if it works I just saved it so I have at least something to show.. 

 

@echo off
cd c:\Test
for /F "delims=" %%a in ('dir /B /A:-D /O:D /T:W') do (
move "%%a" C:\Another\Location
goto continue
)
:continue

 

 I hope you can understand this. Any help will be grateful. Thanks!

 

1 Reply
Highlighted
Commander Lvl 2
Message 2 of 2

Re: Batch Scripting moving and renaming files

I found it here.

 

In your variation of the batch commands, this line represents what you are trying to do with your file:

move "%%a" C:\Another\Location

So to perform the additional steps, add more lines to the script.

1. Rename it.

ren "%%a" new_file.name

2. Now it has a new name, so you use the new name when you move and rename it in the new location.

move new_file.name c:\another\location\another_new.name

Note that I've combined the steps of "move to another location" and "rename it again". (Use "move /?" to see all of the move command's features).

 

So this might be the full script:

@echo off
cd c:\Test
for /F "delims=" %%a in ('dir /B /A:-D /O:D /T:W') do (
ren "%%a" new_file.name
move new_file.name C:\Another\Location\another_new.name
goto continue
)
:continue

 

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!