简体   繁体   中英

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

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

Im working on a Windows Server 2012 and 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 in here but I dont know if it works or what exactly it means I just saved it so I have at least something to show you guys..

@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 think I have to include the following: ren [drive:][path]TargetMask but Im not sure at all... I hope you can understand this. I already tried to look up my problem but just got more confused... Any help is greatly appreciated. Thank you

Windows 10 64-bit

How to move and rename the oldest (by last written time) file in a folder using cmd, pushd, for in do, set, move, and popd.

When you are happy the script will do what it says it will do make the following changes.

Change %userprofile%\\desktop to C:\\Another\\Location

Change pushd "%userprofile%\\desktop" to pushd c:\\test

Change echo move to move

Rob Vanderwoude NT FOR

script:

@rem How to move and rename the oldest (by last written time) file in a folder using cmd, pushd, for in do, set, move, and popd.
@rem Windows 10 64-bit
@echo off
setlocal enableextensions
pushd "%userprofile%\desktop"
FOR /f "delims=" %%g IN ('DIR /b /a-d /o-d /tw') DO (
SET zold=%%g  
SET zoldname=%%~ng
)
echo move /y %zold% %userprofile%\desktop\%zoldname%.bat
popd 
exit /b

cmd:

cmd /eq
pushd "%userprofile%\desktop"
FOR /f "delims=" %g IN ('DIR /b /a-d /o-d /tw') DO (
SET zold=%g  
SET zoldname=%~ng
)
echo move /y %zold% %userprofile%\desktop\%zoldname%.bat
popd 
exit /b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM